Riešenie úloh 4. cvičenia


  1. Riešenie:

    x = int(input('zadaj km pre prvý deň: '))
    y = int(input('zadaj cieľové km: '))
    den = 1
    while x < y:
        x *= 1.1           # alebo x += x * 0.1
        den += 1
    print(f'na {den}. deň prebehne {x:.2f} km')
    

  1. Riešenie:

    n = int(input('zadaj číslo: '))
    while n != 1:
        print(n, end=', ')
        if n % 2 == 0:
            n //= 2
        else:
            n = 3 * n + 1
    print(1)
    

  1. Riešenie:

    sucet = 0
    x = n = 1
    while x != 0:
        x = float(input(f'zadaj {n}. číslo: '))
        sucet += x
        n += 1
    print('súčet všetkých prečítaných čísel je', sucet)
    

  1. Riešenie:

    cislo = int(input('zadaj číslo: '))
    print(cislo, end=' ')
    delitel = 2
    znak = '='
    while cislo > 1:
        if cislo % delitel == 0:
            print(znak, delitel, end=' ')
            cislo //= delitel
            znak = '*'
        else:
            delitel += 1
    print()
    

  1. Riešenie:

    cislo = int(input('zadaj číslo: '))
    cs = 0
    while cislo:
        cifra = cislo % 10
        print(cifra)
        cs += cifra
        cislo = cislo // 10         # cislo //= 10
    print('ciferný súčet =', cs)
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    cislo = int(input('zadaj číslo: '))
    x, y = 350, 150
    while cislo:
        cifra = cislo % 10
        canvas.create_rectangle(x-18, y-18, x+18, y+18, fill='light blue')
        canvas.create_text(x, y, text=cifra, font='arial 30')
        cislo //= 10
        x -= 40
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    cislo = int(input('zadaj číslo: '))
    x, y = 350, 150
    while cislo:
        cifra = cislo % 8
        canvas.create_rectangle(x-18, y-18, x+18, y+18, fill='light blue')
        canvas.create_text(x, y, text=cifra, font='arial 30')
        cislo //= 8
        x -= 40
    

  1. Riešenie A:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n = 13
    for i in range(n):
        for j in range(n):
            x = j * 20 + 100
            y = i * 20 + 12
            if i == n//2 or j == n//2:
                farba = 'red'
            else:
                farba = 'white'
            canvas.create_oval(x-8, y-8, x+8, y+8, fill=farba)
    

    Riešenie B:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n = 13
    for i in range(n):
        for j in range(n):
            x = j * 20 + 100
            y = i * 20 + 12
            if i == j or i == n-1-j:
                farba = 'red'
            else:
                farba = 'white'
            canvas.create_oval(x-8, y-8, x+8, y+8, fill=farba)
    

  1. Riešenie:

    print('zadávaj výšky žiakov')
    dobre = True
    i = 1
    predch = 0
    while True:
        vyska = input(f'   výška {i}. žiaka: ')
        if vyska == '':
            break
        vyska = int(vyska)
        if vyska < predch:
            dobre = False
        predch = vyska
        i += 1
    
    if dobre:
        print('všetci žiaci sú zoradení správne')
    else:
        print('žiaci nie sú správne zoradení')
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 190, 130
    r = 120
    k = 6
    i = 0
    for rr in range(r, 14, -3):
        if i % k:
            farba = 'black'
        else:
            farba = 'gray'
        canvas.create_oval(x-rr, y-rr, x+rr, y+rr, outline=farba)
        i += 1
        print(rr, farba)
    

  1. Riešenie:

    import tkinter
    from random import randint
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    k = 21
    y = 20
    for i in range(10):
        sucet = 0
        x = 20
        while sucet < k:
            h = randint(1, 4)
            canvas.create_oval(x-10, y-10, x+10, y+10, fill='white')
            canvas.create_text(x, y, text=h, font='arial 16 bold')
            sucet += h
            x += 25
        x += 80
        if sucet == k:
            canvas.create_text(340, y, text='HURA', font='arial 12 bold', fill='green')
        else:
            canvas.create_text(340, y, text='ŠKODA', font='arial 12 bold', fill='red')
        y += 25
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas(width=300, height=300)
    canvas.pack()
    
    for i in range(4000):
        x = random.randrange(300)
        y = random.randrange(300)
        if 75 < x < 225 and 75 < y < 225:
            farba = 'red'
        else:
            farba = 'blue'
        canvas.create_oval(x - 5, y - 5, x + 5, y + 5, fill=farba, width=0)
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas(width=300, height=300)
    canvas.pack()
    
    for i in range(4000):
        x = random.randrange(300)
        y = random.randrange(300)
        if x <= y and 300 - x >= y:
            farba = 'red'
        elif x > y and 300 - x >= y:
            farba = 'blue'
        elif x <= y and 300 - x < y:
            farba = 'green'
        else:
            farba = 'yellow'
        canvas.create_oval(x - 5, y - 5, x + 5, y + 5, fill=farba, width=0)
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas(width=300, height=300)
    canvas.pack()
    
    x0, y0 = 180, 130
    r = 110
    
    for i in range(4000):
        x = random.randrange(300)
        y = random.randrange(300)
        if (x-x0)**2 + (y-y0)**2 <= r**2:
            farba = 'red'
        else:
            farba = 'blue'
        canvas.create_oval(x - 5, y - 5, x + 5, y + 5, fill=farba, width=0)
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas(width=300, height=300)
    canvas.pack()
    
    x0, y0 = 180, 130
    r = 110
    cervenych = 0
    
    for i in range(10000):
        x = random.randrange(300)
        y = random.randrange(300)
        if x**2 + y**2 <= 300**2:
            farba = 'red'
            cervenych += 1
        else:
            farba = 'blue'
        canvas.create_oval(x - 2, y - 2, x + 2, y + 2, fill=farba, width=0)
    print('podiel =', cervenych / 10000 * 4)
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n = 7
    for i in range(n):
        x = random.randint(10, 370)
        y = random.randint(10, 240)
        canvas.create_oval(x-3, y-3, x+3, y+3, fill='red')
        if i == 0:
            x1 = x2 = x
            y1 = y2 = y
        else:
            if x < x1:
                x1 = x
            if x > x2:
                x2 = x
            if y < y1:
                y1 = y
            if y > y2:
                y2 = y
    canvas.create_rectangle(x1, y1, x2, y2, outline='blue')
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    for i in range(10000):
        x = random.randint(10, 350)
        y = random.randint(10, 250)
        if y < 90:
            farba = 'black'
        elif y < 170:
            farba = 'red'
        else:
            farba = 'gold'
        canvas.create_oval(x-5, y-5, x+5, y+5, fill=farba, width=0)
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    for i in range(10000):
        x = random.randint(10, 350)
        y = random.randint(10, 250)
        if y < 130:
            if x < y:
                farba = 'blue'
            else:
                farba = 'white'
        else:
            if 130-x > y-130:
                farba = 'blue'
            else:
                farba = 'red'
        canvas.create_oval(x-5, y-5, x+5, y+5, fill=farba, width=0)
    

  1. Riešenie:

    import random
    
    k = 20
    mam = int(input('začínam so sumou: '))
    print('štart ', end='')
    for i in range(1000):
        c1 = random.randint(1, k)
        c2 = random.randint(1, k)
        c3 = random.randint(1, k)
        if c1 == c2 == c3:
            print('+100', end='')
            mam += 99
        elif c1 == c2 or c2 == c3 or c1 == c3:
            print('+5', end='')
            mam += 4
        else:
            print('-1', end='')
            mam -= 1
        if mam <= 0:
            break
    print()
    print('zostalo mi', mam, 'euro')
    

  1. Riešenie:

    suma = int(input('zadaj cislo: '))
    for b in 100, 50, 20, 10, 5, 2, 1:
        n = suma // b
        suma %= b
        if n:
            print(n, 'krát hodnota', b)
    

  1. Riešenie:

    cislo = float(input('zadaj číslo: '))
    x = 0
    od, do = 0, cislo
    while abs(2**x - cislo) > 0.0001:
        x = (od + do) / 2
        if 2**x > cislo:
            do = x
        else:
            od = x
    print('logaritmus', cislo, 'je', x)
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    a = xa, ya = 10, 100
    b = xb, yb = 250, 10
    c = xc, yc = 300, 250
    d1 = d2 = d3 = 100
    while d1 >= 30 or d2 >= 30 or d3 >= 30:
        farba = f'#{random.randrange(256**3):06x}'
        canvas.create_polygon(a, b, c, fill=farba)
        a = (xa+xb)/2, (ya+yb)/2
        b = (xb+xc)/2, (yb+yc)/2
        c = xc, yc = (xc+xa)/2, (yc+ya)/2
        xa, ya = a
        xb, yb = b
        d1 = ((xa-xb)**2 + (ya-yb)**2) ** .5
        d2 = ((xc-xb)**2 + (yc-yb)**2) ** .5
        d3 = ((xa-xc)**2 + (ya-yc)**2) ** .5