Search
def my_find(a, b): 'funkce najde retezec b v retezci a' if len(a) < len(b): return -1 # zcela jiste v a neni b for i in range(0, (len(a)-len(b))+1): print("{} {}".format(i, a[i:i+len(b)])) if a[i:i+len(b)] == b: return i return -1 def my_replace(a, b, c): 'funkce nahradi v a retezec b retezcem c' x = my_find(a, b) if x < 0: return -1 return a[:x] + c + a[x+len(b):] a = my_find("Ahoj", "oj") # a = 2 print(a) a = my_find("Ahoj svete jak se mas", "svete") print(a) b = my_replace("Ahoj svete jak se mas", "svete", "XXX") print(b)
def nacti_data(filename): 'funkce pro nacteni rady celych cisel ze souboru' f = open(filename, 'r') a = f.readline() f.close() # uzavreni souboru return list(map(int, a.split())) def najdi_nejvetsi(pole): 'hledam nejvetsi cislo z pole kladnych celych cisel' maximum = -1 index = 0 for i in range(len(pole)): if pole[i] > maximum: maximum = pole[i] index = i return maximum, index def najdi_druhe_nejvetsi(pole): 'hledam druhe nejvetsi cislo z pole kladnych cisel' (a, b) = najdi_nejvetsi(pole) c = pole[:] c[b] = 0 return najdi_nejvetsi(c) a = nacti_data("data.txt") print(a) b = najdi_nejvetsi(a) print(b) c = najdi_druhe_nejvetsi(a) print(c)
def tisk_2d(pole): 'tisk 2D matice celych cisel' for i in range(len(pole)): for j in range(len(pole[i])): print(str(pole[i][j]).rjust(3), end=" ") print() f = open("sloupec.txt", "r"); a = [] for line in f: # print(line) a.append(int(line)) print(a) f.close() # nacteni 2D matice f = open("matice.txt", "r") b = [] for line in f: b.append(list(map(int, line.split()))) # print(b) tisk_2d(b)