Riešenie úloh 5. cvičenia


  1. Riešenie:

    def obdlznik(sirka, znak='*'):
        print(znak * sirka)
        print(znak + ' ' * (sirka-2) + znak)
        print(znak * sirka)
    

  1. Riešenie:

    def riadok(n, text=''):
        if text != '':
            text = ' ' + text + ' '
        pol1 = (n - len(text)) // 2
        pol2 = n - len(text) - pol1
        print('*' * pol1 + text + '*' * pol2)
    

  1. Riešenie:

    def nsn(a, b):
        sucin = a * b
        while b != 0:
            a, b = b, a % b
        return sucin // a
    

  1. Riešenie:

    def fib_medzi(od, do):
        a, b = 0, 1
        while a <= do:
            if a >= od:
                print(a, end=' ')
            a, b = b, a+b
        print()
    

  1. Riešenie:

    def je_prvocislo(cislo):
        delitel = 2
        while delitel**2 <= cislo and cislo % delitel:
            delitel += 1
        return cislo % delitel != 0
    
    def dvojicky(od, do):
        while True:
            while not je_prvocislo(od):
                od += 1
            if od+2 > do:
                 break
            if je_prvocislo(od+2):
                print(od, od+2)
            od += 2
    

  1. Riešenie:

    import random
    
    def hadanie(od, do):
        cislo = random.randint(od, do)
        print('Myslím si číslo, uhádni ho!')
        for i in range(1, 11):
            tip = int(input('tvoj tip: '))
            if tip == cislo:
                print(f'Uhadol si na {i}. pokus. Gratulujem.')
                return
            if tip < cislo:
                print('*** pridaj')
            else:
                print('*** uber')
        print('Neuhádol si ani na 10 pokusov.')
        print(f'Myslel som si číslo {cislo}.')
    

  1. Riešenie:

    import tkinter
    
    def koleso(x, y, r=15, farba='blue'):
        canvas.create_oval(x-r, y-r, x+r, y+r, fill=farba)
    
    def doska(x, y, a=100, b=20, farba='red'):
        canvas.create_rectangle(x-a/2, y-b, x+a/2, y, fill=farba)
    
    def vozik(x, y):
        doska(x, y)
        koleso(x-30, y)
        koleso(x+30, y)
    
    def velky_vozik(x, y):
        doska(x, y, 150, 40, 'green')
        koleso(x-35, y, 25, 'orange')
        koleso(x+35, y, 25, 'orange')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    vozik(200, 100)
    velky_vozik(150, 200)
    vozik(300, 210)
    

  1. Riešenie:

    import tkinter
    from random import randrange, randint
    
    def kruhy(x, y):
        for r in reversed(range(5, 51, 5)):
            farba = f'#{randrange(256**3):06x}'
            canvas.create_oval(x-r, y-r, x+r, y+r, fill=farba)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    for i in range(10):
        kruhy(randint(50, 330), randint(50, 210))
    

  1. Riešenie:

    import tkinter
    from random import randrange, randint
    
    def nahodna_farba():
        return f'#{randrange(256**3):06x}'
    
    def dom(x, y, vel1, vel2):
        canvas.create_rectangle(x, y, x+vel1, y-vel1, fill=nahodna_farba())
        canvas.create_polygon(x, y-vel1, x+vel1, y-vel1, x+vel1/2, y-vel1-vel2, fill=nahodna_farba(), outline='black')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 10, 150
    while x < 330:
        v = randint(20, 50)
        dom(x, y, v, randint(v//2, v))
        x += v
    

  1. Riešenie:

    import tkinter
    from random import randint
    from math import sqrt
    
    def vzd(x1, y1, x2, y2):
        return sqrt((x1-x2) ** 2 + (y1-y2) ** 2)
    
    def kresli_bodku(x, y, farba):
        canvas.create_oval(x-3, y-3, x+3, y+3, fill=farba, width=0)
    
    def farebne_bodky(r, x1, y1, x2, y2, x3, y3):
        for i in range(10000):
            x = randint(10, 370)
            y = randint(10, 250)
            pocet = 0
            if vzd(x, y, x1, y1) < r:
                pocet += 1
            if vzd(x, y, x2, y2) < r:
                pocet += 1
            if vzd(x, y, x3, y3) < r:
                pocet += 1
    
            if pocet % 2 == 0:
                kresli_bodku(x, y, 'navy')
            else:
                kresli_bodku(x, y, 'yellow')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    farebne_bodky(80, 120, 120, 180, 110, 160, 170)
    

  1. Riešenie:

    import tkinter
    from random import randrange
    
    def nahodna_farba():
        return f'#{randrange(256**3):06x}'
    
    def rgb(r, g, b):
        return f'#{r:02x}{g:02x}{b:02x}'
    
    a, x0, y0 = 30, 5, 5
    
    def stv(riadok, stlpec, farba='white'):
        x = x0 + stlpec * a
        y = y0 + riadok * a
        canvas.create_rectangle(x, y, x+a, y+a, fill=farba, width=0)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    for i in range(8):
        for j in range(12):
            if i == j:
                stv(i, j)
            else:
                stv(i, j, nahodna_farba())
    

    druhá podúloha:

    for i in range(8):
        for j in range(12):
            stv(i, j, rgb(255, (i+j)*14, 0))
    

  1. Riešenie:

    import tkinter
    from math import sin, cos, radians
    
    x0, y0 = 190, 130
    
    def n_uholnik(n, x0, y0, r):
        x1, y1 = x0 + r, y0
        for i in range(1, n+1):
            uhol = 360/n * i
            x2 = x0 + r * cos(radians(uhol))
            y2 = y0 + r * sin(radians(uhol))
            canvas.create_line(x1, y1, x2, y2)
            x1, y1 = x2, y2
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n_uholnik(3, 50, 50, 45)
    n_uholnik(4, 150, 50, 45)
    n_uholnik(5, 250, 50, 45)
    
    n_uholnik(6, 50, 150, 45)
    n_uholnik(7, 150, 150, 45)
    n_uholnik(8, 250, 150, 45)
    

  1. Riešenie:

    import tkinter
    from math import sin, cos, radians
    
    x0, y0 = 190, 130
    
    def n_hviezda(n, x0, y0, r, k=2):
        x1, y1 = x0 + r, y0
        for i in range(1, n+1):
            uhol = k*360/n * i
            x2 = x0 + r * cos(radians(uhol))
            y2 = y0 + r * sin(radians(uhol))
            canvas.create_line(x1, y1, x2, y2)
            x1, y1 = x2, y2
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n_hviezda(5, 50, 50, 45)
    n_hviezda(7, 150, 50, 45)
    n_hviezda(7, 250, 50, 45, 3)
    
    n_hviezda(9, 50, 150, 45)
    n_hviezda(9, 150, 150, 45, 4)
    n_hviezda(11, 250, 150, 45, 4)
    

  1. Riešenie:

    import tkinter
    from math import sin, cos, radians
    
    def n_spirala(n, x0, y0, r):
        r0 = 5
        x1, y1 = x0 + r0, y0
        uhol = 0
        while r0 <= r:
            uhol += 360/n
            x2 = x0 + r0 * cos(radians(uhol))
            y2 = y0 + r0 * sin(radians(uhol))
            canvas.create_line(x1, y1, x2, y2)
            x1, y1 = x2, y2
            r0 += 2
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n_spirala(5, 190, 130, 125)
    

    alebo

    n_spirala(3, 50, 50, 45)
    n_spirala(4, 150, 50, 45)
    n_spirala(5, 250, 50, 45)
    
    n_spirala(6, 50, 150, 45)
    n_spirala(7, 150, 150, 45)
    n_spirala(8, 250, 150, 45)
    

  1. Riešenie:

    import tkinter
    from math import sin, cos, radians
    
    def horna(x0, y0, r):
        x1, y1 = x0 - r, y0
        uhol = 180
        for i in range(18):
            uhol += 360/36
            x2 = x0 + r * cos(radians(uhol))
            y2 = y0 + r * sin(radians(uhol))
            canvas.create_line(x1, y1, x2, y2)
            x1, y1 = x2, y2
    
    def dolna(x0, y0, r):
        x1, y1 = x0 + r, y0
        uhol = 0
        for i in range(18):
            uhol += 360/36
            x2 = x0 + r * cos(radians(uhol))
            y2 = y0 + r * sin(radians(uhol))
            canvas.create_line(x1, y1, x2, y2)
            x1, y1 = x2, y2
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    horna(30, 100, 30)
    dolna(90, 100, 30)
    horna(150, 100, 30)
    dolna(210, 100, 30)
    horna(270, 100, 30)
    dolna(330, 100, 30)
    
    for i in range(6):
        horna(30+60*i, 200, 30)
    

  1. Riešenie:

    import tkinter
    from math import sin, cos, radians
    
    x0, y0 = 190, 130
    
    def rucicka(uhol, dlzka, hrubka, farba):
        x = x0 + dlzka * cos(radians(uhol))
        y = y0 + dlzka * sin(radians(uhol))
        canvas.create_line(x0, y0, x, y, width=hrubka, fill=farba)
    
    def hodinky(hod, min, sek):
        canvas.create_oval(x0-100, y0-100, x0+100, y0+100, fill='white')
        rucicka((hod-3+min/60)*30, 60, 10, 'gray')
        rucicka((min-15)*6, 70, 6, 'black')
        rucicka((sek-15)*6, 80, 2, 'red')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    hodinky(8, 55, 10)
    
    ##from time import localtime
    ##
    ##while True:
    ##    canvas.delete('all')
    ##    h, m, s = localtime()[3:6]
    ##    hodinky(h, m, s)
    ##    canvas.update()
    ##    canvas.after(1000)