Riešenie úloh 6. cvičenia


  1. Riešenie:

    def sucet(retazec):
        i = retazec.find('+')
        return int(retazec[:i]) + int(retazec[i+1:])
    

  1. Riešenie:

    def sucet(retazec):
        vysl = 0
        while True:
            i = retazec.find('+')
            if i > 0:
                vysl += int(retazec[:i])
                retazec = retazec[i+1:]
            else:
                return vysl + int(retazec)
    

  1. Riešenie:

    def postupnost(od, do, krok=1):
        vysl = ''
        for i in range(od, do, krok):
            if vysl != '':
                vysl += ' '
            vysl += str(i)
        return vysl
    

  1. Riešenie:

    def rozsekaj(text, sirka):
        vysl = nn = ''
        for i in range(0, len(text), sirka):
            vysl += nn + text[i:i+sirka]
            nn = '\n'
        return vysl
    

  1. Riešenie:

    def stvorec(n, znak):
        return znak*n+'\n' + (n-2)*(znak+' '*(n-2)+znak+'\n') + znak*n
    

  1. Riešenie:

    def vyhod_duplikaty(retazec):
        vysl = pred = ''
        for znak in retazec:
            if znak != pred:
                vysl += znak
                pred = znak
        return vysl
    

  1. Riešenie:

    def ozatvorkuj(retazec, podretazec):
        return retazec.replace(podretazec, '('+podretazec+')')
    

  1. Riešenie:

    def prevrat(retazec):
        vysl = ''
        for znak in retazec:
            vysl = znak + vysl
        return vysl
    

  1. Riešenie:

    def male(retazec, i):
        return retazec[:i] + retazec[i].lower() +retazec[i+1:]
    
    def velke(retazec, i):
        return retazec[:i] + retazec[i].upper() +retazec[i+1:]
    

  1. Riešenie:

    def riadky(retazec):
        n = 1
        while retazec:
            i = (retazec+'\n').find('\n')
            print(f'{n}.', retazec[:i])
            retazec = retazec[i+1:]
            n += 1
    

  1. Riešenie:

    def posun_znak(znak, p):
        if 'a' <= znak <= 'z':
            return chr((ord(znak)-97+p)%26 + 97)
        return znak
    

  1. Riešenie:

    def zakoduj(text, posun):
        vysl = ''
        for znak in text:
            vysl += posun_znak(znak, posun)
        return vysl
    

  1. Riešenie:

    def je_palindrom(retazec):
        retazec = retazec.lower().replace(' ', '')
        for i in range(len(retazec)//2):
            if retazec[i] != retazec[-i-1]:
                return False
        return True
    

    alebo

    def je_palindrom(retazec):
        retazec = retazec.lower().replace(' ', '')
        return retazec == retazec[::-1]
    

  1. Riešenie:

    def pocet(retazec, podretazec):
        vysl = 0
        d = len(podretazec)
        for i in range(len(retazec)-d+1):
            if retazec[i:i+d] == podretazec:
                vysl += 1
        return vysl
    

  1. Riešenie:

    def usporiadaj(h1, h2, h3):
        if h1 > h2:
            h1, h2 = h2, h1
        if h2 > h3:
            h2, h3 = h3, h2
        if h1 > h2:
            h1, h2 = h2, h1
        return f'{h1} {h2} {h3}'
    

  1. Riešenie:

    from random import choice
    
    def nazov(n):
        p1 = choice('aeiouy')
        p2 = choice('bcdfghjklmnpqrstvwxz')
        return p1.upper() + n*p2 + p1
    
    for i in range(5):
        print(nazov(2))
        print(nazov(3))
    

  1. Riešenie:

    import tkinter
    from random import randrange
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 50, 130
    for i in range(0x2654, 0x265A):
        farba = f'#{randrange(256**3):06x}'
        canvas.create_text(x, y, text=chr(i), font='arial 50', fill=farba)
        x += 60
    

  1. Riešenie:

    import tkinter
    
    def stvorce(vel, retazec):
        x, y = 10, 100
        while retazec:
            i = (retazec+' ').find(' ')
            farba = retazec[:i]
            retazec = retazec[i+1:]
            canvas.create_rectangle(x, y, x+vel, y-vel, fill=farba)
            x += vel+3
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    stvorce(40, 'red blue purple red gold')
    

  1. Riešenie:

    import tkinter
    
    def stvorce(retazec):
        x, y = 10, 200
        povodny = retazec
        while True:
            if retazec == '':
                retazec = povodny
            i = retazec.find(' ')
            vel = int(retazec[:i])
            retazec = retazec[i+1:]
            if x+vel >= sirka:
                break
            i = (retazec+' ').find(' ')
            farba = retazec[:i]
            retazec = retazec[i+1:]
            canvas.create_rectangle(x, y, x+vel, y-vel, fill=farba)
            x += vel+3
    
    sirka = 450
    canvas = tkinter.Canvas(width=sirka)
    canvas.pack()
    
    stvorce('40 red 20 blue 60 purple 40 red 30 gold')
    

  1. Riešenie:

    import tkinter
    
    def kresli(retazec):
        x, y = 100, 100
        for znak in retazec:
            x1, y1 = x, y
            if znak == 's':
                y1 -= 10
            elif znak == 'v':
                x1 += 10
            elif znak == 'j':
                y1 += 10
            elif znak == 'z':
                x1 -= 10
            else:
                print('nerozumiem "' + znak + '"')
                return
            canvas.create_line(x, y, x1, y1)
            x, y = x1, y1
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    kresli('ssvvjjzzvvvvjjzzssjjjjzzssvvzzzzssvvjj')
    

  1. Riešenie:

    import tkinter
    
    def kresli(retazec):
        x, y = x1, y1 = 100, 100
        pd = True
        pocet = 1
        for znak in retazec:
            if '0' <= znak <= '9':
                pocet = int(znak)
            elif znak == 's':
                y1 -= 10 * pocet
                pocet = 1
            elif znak == 'v':
                x1 += 10 * pocet
                pocet = 1
            elif znak == 'j':
                y1 += 10 * pocet
                pocet = 1
            elif znak == 'z':
                x1 -= 10 * pocet
                pocet = 1
            elif znak == 'd':
                pd = True
            elif znak == 'h':
                pd = False
            else:
                print('nerozumiem "' + znak + '"')
                return
            if pd:
                canvas.create_line(x, y, x1, y1)
            x, y = x1, y1
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    kresli('4v4j4z4sh5vd'*5)