Riešenie úloh 18. cvičenia


  1. Riešenie:

    import turtle
    import random
    
    class MojaTurtle(turtle.Turtle):
        def __init__(self):
            super().__init__()
            self.speed(0)
            self.pu()
            self.setpos(random.randint(-250, 250), random.randint(-250, 250))
            self.pd()
    
        def domcek(self, dlzka):
            for uhol in 90, 90, 90, 30, 120, -60:
                self.fd(dlzka)
                self.rt(uhol)
    
    class MojaTurtle1(MojaTurtle):
        def fd(self, dlzka):
            while dlzka >= 5:
                self.lt(60)
                super().fd(5)
                self.rt(120)
                super().fd(5)
                self.lt(60)
                dlzka -= 5
            super().fd(dlzka)
    
    turtle.delay(0)
    for i in range(30):
        MojaTurtle1().domcek(30)
    

  1. Riešenie:

    import turtle
    import random
    
    class MojaTurtle(turtle.Turtle):
        def __init__(self):
            super().__init__()
            self.speed(0)
            self.pu()
            self.setpos(random.randint(-250, 250), random.randint(-250, 250))
            self.pd()
    
        def domcek(self, dlzka):
            for uhol in 90, 90, 90, 30, 120, -60:
                self.fd(dlzka)
                self.rt(uhol)
    
    class MojaTurtle0(MojaTurtle):
        def rt(self, uhol):
            super().rt(uhol + random.randint(-5, 5))
    
        def lt(self, uhol):
            super().lt(uhol + random.randint(-5, 5))
    
    class MojaTurtle1(MojaTurtle0):
        def fd(self, dlzka):
            while dlzka >= 5:
                self.lt(60)
                super().fd(5)
                self.rt(120)
                super().fd(5)
                self.lt(60)
                dlzka -= 5
            super().fd(dlzka)
    
    class MojaTurtle2(MojaTurtle0):
        def fd(self, dlzka):
            super().fd(dlzka)
            self.rt(180 - random.randint(-3, 3))
            super().fd(dlzka)
            self.rt(180 - random.randint(-3, 3))
            super().fd(dlzka)
    
    turtle.delay(0)
    for i in range(20):
        MojaTurtle0().domcek(50)  # MojaTurtle1... MojaTurtle2...
    

  1. Riešenie:

    >>> (3 + 4) * 5 + 2 ** (100 // 5)
    1048611
    >>> (3).__add__(4).__mul__(5).__add__((2).__pow__((100).__floordiv__(5)))
    1048611
    >>> (7).__pow__(8).__str__().__len__().__add__('xy'.__rmul__(8).__len__().__add__(1)).__mul__(13)
    312
    >>> (len(str(7 ** 8)) + len(8 * 'xy') + 1) * 13
    312
    

  1. Riešenie:

    class Cas:
        def __init__(self, hodiny=0, minuty=0, sekundy=0):
            self.sek = abs(3600 * hodiny + 60 * minuty + sekundy)
    
        def __str__(self):
            return f'{self.sek//3600}:{self.sek//60%60:02}:{self.sek%60:02}'
    
        __repr__ = __str__
    
        def _pom(self, iny):           # pomocná metóda
            if isinstance(iny, int):
                return iny
            if isinstance(iny, Cas):
                return iny.sek
            iny += (0, 0, 0)
            return iny[0] * 3600 + iny[1] * 60 + iny[2]
    
        def __add__(self, iny):
            return Cas(0, 0, self.sek + self._pom(iny))
    
        __radd__ = __add__
    
        def __sub__(self, iny):
            return Cas(0, 0, self.sek - self._pom(iny))
    
        def __rsub__(self, iny):
            return Cas(0, 0, -self.sek + self._pom(iny))
    
        def __gt__(self, iny):
            return self.sek > iny.sek
    
        def __eq__(self, iny):
            return self.sek == iny.sek
    

  1. Riešenie:

    def teraz():
        import time
        h, m, s = time.localtime()[3:6]
        return Cas(h, m, s)
    

  1. Riešenie:

    zoznam = []
    for i in range(8):
        zoznam.append(Cas(0, 0, random.randrange(86000)))
    print(zoznam)
    zoznam1 = sorted(zoznam)
    print(zoznam1)
    

    napríklad:

    [9:47:23, 1:54:38, 19:48:32, 10:45:36, 11:12:29, 3:06:02, 19:31:59, 23:47:08]
    [1:54:38, 3:06:02, 9:47:23, 10:45:36, 11:12:29, 19:31:59, 19:48:32, 23:47:08]
    

  1. Riešenie:

    def mnozina1(n):
        return set(range(0, n + 1, 3)) & (set(range(1, n + 1, 5)) | set(range(2, n + 1, 5)))
    
    def mnozina2(n1, n2):
        return mnozina1(n2) - set(range(n1))
        return set(range((n1 + 2) // 3 * 3, n2 + 1, 3)) & \
               (set(range((n1 + 4) // 5 * 5 + 1, n2 + 1, 5)) |
                set(range((n1 + 4) // 5 * 5 + 2, n2 + 1, 5)))
    

  1. Riešenie:

    def len_v_jednom(retazec1, retazec2):
        return set(retazec1) ^ set(retazec2)
    

  1. Riešenie:

    def vsetky_rozne(postupnost):
        return len(set(postupnost)) == len(postupnost)
    

  1. Riešenie:

    def rozdel(mnozina):
        m1, m2 = set(), set()
        for p in mnozina:
            if isinstance(p, (int, float)):
                m1.add(p)
            elif isinstance(p, str):
                m2.add(p)
        return m1, m2
    

  1. Riešenie:

    def bez_parnych(mnozina):
        for p in list(mnozina):
            if isinstance(p, int) and p % 2 == 0:
                mnozina.remove(p)
    

  1. Riešenie:

    def len_raz(retazec):
        m1, m2 = set(), set()
        for z in retazec:
            if z in m1:
                m2.add(z)
            else:
                m1.add(z)
        return m1 - m2
    

  1. Riešenie:

    def opakuje_sa(meno_suboru):
        m1, m2 = set(), set()
        with open(meno_suboru, encoding='utf-8') as subor:
            for slovo in subor.read().split():
                if slovo in m1:
                    m2.add(slovo)
                else:
                    m1.add(slovo)
            return m2
    

  1. Riešenie:

    def vsetky(a, b, c, d):
        return [{a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}]
    

  1. Riešenie:

    def vsetky(mnozina):
        vysl = []
        p = tuple(mnozina)
        for i in range(len(p)):
            for j in range(i + 1, len(p)):
                    vysl.append({p[i], p[j]})
        return vysl
    

  1. Riešenie:

    def kartez_sucin(m1, m2):
        vysl = set()
        for i in m1:
            for j in m2:
                vysl.add((i, j))
        return vysl