def rand_count(length, _seed=None, N=None): seed(_seed) if N is None: N = max(int(length / 5), 2) return [randint(-N, N) for _ in range(length)] def keskmised(jarjend): output = [] for i in range(0, len(jarjend)): if i != len(jarjend) - 1: output.append(0.5 * (jarjend[i] + jarjend[i + 1])) return output checker = make_checker(keskmised) checker([1, 1]) checker([300, 400]) checker([20, 21]) checker([1, 1, 3, 5, 9]) checker([1, 2, 30, 500]) checker( [2.5, 1.5, 0.5, 0], description= "Erijuht, ei tohiks kasutada round'i - {function}({args}) == {expected}") checker([], description="Erijuht, järjend tühi - {function}({args}) == {expected}") checker([1], description= "Erijuht, järjendis 1 element - {function}({args}) == {expected}")
Task description (in Estonian): 1. Sõne laiendamine (5p) Kirjuta funktsioon laienda, mis võtab argumendiks sõne ja sama pika naturaalarvulisti, ning tagastab uue sõne, kus igat esialgse sõne sümbolit on korratud vastavalt samal positsioonil olevale arvule etteantud arvulistis. Võib eeldada, et etteantud sõne ja listi pikkused on võrdsed. Näide: laienda("hernes", [1,2,0,3,1,1]) peab tagastama "heennnes". """ from grader import * from KT2_util import make_checker def laienda(sone, korduseid): assert (len(sone) == len(korduseid)) return "".join(taht * x for taht, x in zip(sone, korduseid)) checker = make_checker(laienda) checker("hernes", [1, 2, 0, 3, 1, 1]) checker("KaPsa hautis ", [3, 4, 1, 1, 0, 5, 1, 2, 1, 2, 1, 2, 1]) checker("T", [5]) checker("a", [0]) checker("TeReMaaIlm", list(range(10, 20))) checker("", [], description="Erijuht, tühi sõne - {function}({args}) == {expected}")
def mood(lst): parim = 0 parim_count = 0 for el in lst: count = 0 for a in lst: if a == el: count += 1 if count > parim_count or (count == parim_count and el < parim): parim = el parim_count = count return parim def rand_count(length, _seed=None, N = None): seed(_seed) if N is None: N = max(int(length / 5), 2) return [randint(-N, N) for _ in range(length)] checker = make_checker(mood) checker([1]) checker([1, 1, 1, 3]) checker([1, 2, 2, 3, 2, 1, -2, 3]) checker([-10, 17, 13, 17, -10, 21], description="Erijuht, võrdsete esinemiste arvu korral tagasta vähim - {function}({args}) == {expected}") checker([17, -10, 13, -10, 17, 21], description="Erijuht, võrdsete esinemiste arvu korral tagasta vähim - {function}({args}) == {expected}") for i in range(5): checker(rand_count(20, i))
for j in range(0, len(b)): listike.append(0) output.append(listike) # täidame maatriksi for i in range(0, len(a)): ca = a[i].lower() for j in range(0, len(b)): cb = b[j].lower() #print("%s - %s" % (ca, cb)) if (ca < cb): output[i][j] = -1 #print("%s[%d] < %s[%d]" % (ca, i, cb, j)) elif (ca > cb): output[i][j] = 1 #print("%s[%d] > %s[%d]" % (ca, i, cb, j)) else: output[i][j] = 0 #print("%s[%d] = %s[%d]" % (ca, i, cb, j)) return output checker = make_checker(järgnevusmaatriks) checker("kala", "uba") checker("KALA", "uba") checker("uba", "KALA") checker("bbb", "abc") checker("BBB", "abc") checker("a", "Z") checker("", "")
1, 2, 5 ja 1 + 1 + 2 + 2 + 5 + 5 + 5 = 21. """ from grader import * from KT2_util import make_checker from random import * def korduvate_summa(array): return sum(filter(lambda x: array.count(x) > 1, array)) def rand_count(length, _seed=None, N=None): seed(_seed) if N is None: N = max(int(length / 5), 2) return [randint(-N, N) for _ in range(length)] checker = make_checker(korduvate_summa) checker([1, 1, 1, 1]) checker([1, 1, 1, 1, 2, 2, 3]) checker([1, 0, -4, 2, 3, 5, -5, 2, -2, 5, 1, 5, 4]) checker([], description="Erijuht, tühi järjend - {function}({args}) == {expected}") checker( [1, 2, 3, 4], description="Erijuht, korduvaid pole - {function}({args}) == {expected}") for i in range(5): checker(rand_count(20, i))
def loenda(sone): if sone == "": return [] tulemus = [] count = 0 prev_taht = sone[0] for taht in sone: if taht == prev_taht: count += 1 else: tulemus.append(count) count = 1 prev_taht = taht tulemus.append(count) return tulemus def suvaline_sona(length, _seed=None): seed(_seed) return "".join(choice(ascii_lowercase) * randrange(1, 20) for _ in range(length)) checker = make_checker(loenda) checker("tere") checker("ttttt") checker("kaaapsauussssss") checker("3332219999999994444") checker("", description="Erijuht, tühi sõne - {function}({args}) == {expected}") for i in range(4): checker(suvaline_sona(10, i))
koosneb arvudest, mis on saadud listi a esimese elemendi korrutamisel listi b elementidega, teisel real on listi a teise elemendi korrutised listi b elementidega jne. Näide: korrutustabel([5,2,8,4], [2,4,1]) peab tagastama [[10, 20, 5], [4, 8, 2], [16, 32, 8], [8, 16, 4]] """ from KT2_util import make_checker def korrutustabel(A, B): tulemus = [] for a in A: r = [] for b in B: r.append(a * b) tulemus.append(r) return tulemus checker = make_checker(korrutustabel) checker([], []) checker([1], [5]) checker([10], [2, 5, 10]) checker([2, 5, 10], [3, 33]) checker([0.5, 2.5, 7.125, 9.5, 0.0], [3, 33, 55, 99, 22], description="Erijuht, ei tohiks kasutada round, int tegemist) - {function}({args}) == {expected}")
for j in range(0, len(b)): listike.append(0) output.append(listike) # täidame maatriksi for i in range(0, len(a)): ca = a[i].lower() for j in range(0, len(b)): cb = b[j].lower() #print("%s - %s" % (ca, cb)) if(ca < cb): output[i][j] = -1 #print("%s[%d] < %s[%d]" % (ca, i, cb, j)) elif(ca > cb): output[i][j] = 1 #print("%s[%d] > %s[%d]" % (ca, i, cb, j)) else: output[i][j] = 0 #print("%s[%d] = %s[%d]" % (ca, i, cb, j)) return output checker = make_checker(järgnevusmaatriks) checker("kala", "uba") checker("KALA", "uba") checker("uba", "KALA") checker("bbb", "abc") checker("BBB", "abc") checker("a", "Z") checker("", "")
from grader import * from KT2_util import make_checker def suurenda(maatriks): # kole kood tulemus = [] for rida in maatriks: uus_rida = [] for element in rida: uus_rida.append(element) uus_rida.append(element) tulemus.append(uus_rida) tulemus.append(uus_rida) return tulemus checker = make_checker(suurenda) checker([[1, 2, 3], [4, 5, 6]]) checker([[]]) checker([[1]]) checker([[1, 2.5, 3], [5, 3, 1], [4, 4, 1]]) random_tests = [[[1, 8]], [[2]], [[4, 2], [1, 4]], [[8, 6]], [[5, 6, 8, 6], [3, 3, 1, 2], [2, 2, 7, 1], [6, 4, 9, 3]], [[5, 5, 3], [7, 8, 2], [9, 7, 3]]] for test_case in random_tests: checker(test_case)
poole hulka. Näiteks teine_pool_suurem([2, 3, -1, 4, 1]) peab tagastama True. """ from grader import * from KT2_util import make_checker def teine_pool_suurem(arr): kesk = len(arr) // 2 if len(arr) % 2 == 1: kesk += 1 return sum(arr[kesk:]) > sum(arr[:kesk]) checker = make_checker(teine_pool_suurem) checker([2, 3, -1, 4, 1]) checker([2, 3, 1, 4, 1]) checker([2, 2, 2, 3]) checker( [2, 2, 2, 2], description="Test, poolte summad võrdsed - {function}({args}) == {expected}" ) checker( [1, 2, 3, 4, 2], description="Test, poolte summad võrdsed - {function}({args}) == {expected}" ) checker([2, 2, 2, 2, 2]) checker( [10],
Kui listis on paaritu arv elemente, tuleks lugeda keskmine element listi esimese poole hulka. Näiteks teine_pool_suurem([2, 3, -1, 4, 1]) peab tagastama True. """ from grader import * from KT2_util import make_checker def teine_pool_suurem(arr): kesk = len(arr) // 2 if len(arr) % 2 == 1: kesk += 1 return sum(arr[kesk:]) > sum(arr[:kesk]) checker = make_checker(teine_pool_suurem) checker([2, 3, -1, 4, 1]) checker([2, 3, 1, 4, 1]) checker([2, 2, 2, 3]) checker([2, 2, 2, 2], description="Test, poolte summad võrdsed - {function}({args}) == {expected}") checker([1, 2, 3, 4, 2], description="Test, poolte summad võrdsed - {function}({args}) == {expected}") checker([2, 2, 2, 2, 2]) checker([10], description="Erijuht, tühja teise poole summa on 0 - {function}({args}) == {expected}") checker([-10], description="Erijuht, tühja teise poole summa on 0 - {function}({args}) == {expected}") checker([1.3, 3.2, 2.2, 2.3, 3.1, 1.4], description="Funktsioon peaks toime tulema ka ujukomaarvudega - {function}({args}) == {expected}")
tulemus = [] count = 0 prev_taht = sone[0] for taht in sone: if taht == prev_taht: count += 1 else: tulemus.append(count) count = 1 prev_taht = taht tulemus.append(count) return tulemus def suvaline_sona(length, _seed=None): seed(_seed) return "".join( choice(ascii_lowercase) * randrange(1, 20) for _ in range(length)) checker = make_checker(loenda) checker("tere") checker("ttttt") checker("kaaapsauussssss") checker("3332219999999994444") checker("", description="Erijuht, tühi sõne - {function}({args}) == {expected}") for i in range(4): checker(suvaline_sona(10, i))
järjendis esineb. Näide: korduvate_summa([1, 0, -4, 2, 3, 5, -5, 2, -2, 5, 1, 5, 4]) peab tagastama 21, sest arvud, mis esinevad näidatud jadas rohkem kui üks kord on 1, 2, 5 ja 1 + 1 + 2 + 2 + 5 + 5 + 5 = 21. """ from grader import * from KT2_util import make_checker from random import * def korduvate_summa(array): return sum(filter(lambda x: array.count(x) > 1, array)) def rand_count(length, _seed=None, N = None): seed(_seed) if N is None: N = max(int(length / 5), 2) return [randint(-N, N) for _ in range(length)] checker = make_checker(korduvate_summa) checker([1, 1, 1, 1]) checker([1, 1, 1, 1, 2, 2, 3]) checker([1, 0, -4, 2, 3, 5, -5, 2, -2, 5, 1, 5, 4]) checker([], description="Erijuht, tühi järjend - {function}({args}) == {expected}") checker([1, 2, 3, 4], description="Erijuht, korduvaid pole - {function}({args}) == {expected}") for i in range(5): checker(rand_count(20, i))
def vähenda(maatriks): tulemus = [] for r in range(0, len(maatriks), 2): rida = [] for c in range(0, len(maatriks[r]), 2): tul = 0 for i in range(4): tul += maatriks[r + i % 2][c + i // 2] rida.append(tul / 4.0) tulemus.append(rida) return tulemus checker = make_checker(vähenda) checker( [[1, 2], [3, 4]], description="Ruudukujuline 2x2 maatriks- {function}({args}) == {expected}") checker([[1, 2, 3, 4], [5, 6, 7, 8]], description= "Mitte-ruudukujuline maatriks - {function}({args}) == {expected}") checker([[1, 5, 2, 6, 3, 6], [1, 3, 2, 7, 3, 3], [4, 8, 5, 1, 1, 6], [4, 4, 9, 5, 6, 1]]) checker([[1, 5, 2, 6, 3, 6], [1, 3, 2, 7, 3, 3], [4, 8, 5, 1, 1, 6], [4, 4, 9, 5, 6, 1]]) checker([], description="Erijuht, tühi maatriks- {function}({args}) == {expected}") random_tests = [[[7, 5, 2, 6, 6, 9], [2, 8, 6, 3, 8, 7]],
from grader import * from KT2_util import make_checker def vähenda(maatriks): tulemus = [] for r in range(0, len(maatriks), 2): rida = [] for c in range(0, len(maatriks[r]), 2): tul = 0 for i in range(4): tul += maatriks[r+i%2][c+i//2] rida.append(tul / 4.0) tulemus.append(rida) return tulemus checker = make_checker(vähenda) checker([[1, 2], [3, 4]], description="Ruudukujuline 2x2 maatriks- {function}({args}) == {expected}") checker([[1, 2, 3, 4], [5, 6, 7, 8]], description="Mitte-ruudukujuline maatriks - {function}({args}) == {expected}") checker([[1,5,2,6,3,6], [1,3,2,7,3,3], [4,8,5,1,1,6], [4,4,9,5,6,1]]) checker([[1,5,2,6,3,6], [1,3,2,7,3,3], [4,8,5,1,1,6], [4,4,9,5,6,1]]) checker([], description="Erijuht, tühi maatriks- {function}({args}) == {expected}") random_tests = [ [[7, 5, 2, 6, 6, 9], [2, 8, 6, 3, 8, 7]], [[3, 1, 0, 9], [0, 5, 1, 7]],
[1,3,2,7,3,3], [4,8,5,1,1,6], [4,4,9,5,6,1] ]) peab tagastama [[1, 2, 3], [4, 5, 1]]. """ from grader import * from KT2_util import make_checker def vähenda_lihtsam(maatriks): return [[maatriks[r][c] for c in range(0, len(maatriks[r]), 2)] for r in range(0, len(maatriks), 2)] checker = make_checker(vähenda_lihtsam) checker([[1, 2], [3, 4]], description="Ruudukujuline 2x2 maatriks- {function}({args}) == {expected}") checker([[1, 2, 3, 4], [5, 6, 7, 8]], description="Mitte-ruudukujuline maatriks - {function}({args}) == {expected}") checker([[1,5,2,6,3,6], [1,3,2,7,3,3], [4,8,5,1,1,6], [4,4,9,5,6,1]]) checker([[1,5,2,6,3,6], [1,3,2,7,3,3], [4,8,5,1,1,6], [4,4,9,5,6,1]]) checker([], description="Erijuht, tühi maatriks- {function}({args}) == {expected}") random_tests = [ [[7, 5, 2, 6, 6, 9], [2, 8, 6, 3, 8, 7]], [[3, 1, 0, 9], [0, 5, 1, 7]],