Riešenie úloh 17. cvičenia


  1. Riešenie:

    >>> a = '7 3'
    >>> int(a)
    ...
    ValueError: invalid literal for int() with base 10: '7 3'
    >>> int([a])
    ...
    TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
    >>> open(a)
    ...
    FileNotFoundError: [Errno 2] No such file or directory: '7 3'
    >>> int(a[0]) / 0
    ...
    ZeroDivisionError: division by zero
    >>> a.a
    ...
    AttributeError: 'str' object has no attribute 'a'
    >>> a = x
    ...
    NameError: name 'x' is not defined
    >>> assert 0, a
    ...
    AssertionError: 7 3
    >>> float(a[0])**int(a.replace(' ', '9'))
    ...
    OverflowError: (34, 'Result too large')
    

  1. Riešenie:

    def cele(hodnota):
        try:
            return int(hodnota)
        except (ValueError, TypeError):
            return 0
    

  1. Riešenie:

    def desatinne(retazec):
        try:
            float(retazec)
            return True
        except (ValueError, TypeError):
            return False
    

  1. Riešenie:

    def zoznam(ret):
        zoz = []
        for prvok in ret[1:-1].split(','):
            try:
                zoz.append(int(prvok))
            except (ValueError, TypeError):
                try:
                    zoz.append(float(prvok))
                except (ValueError, TypeError):
                    pass
        return zoz
    

  1. Riešenie:

    def sucet(post):
        if  len(post) == 0:
            return None
        vysl = post[0]
        for prvok in post[1:]:
            try:
                vysl += type(vysl)(prvok)
            except TypeError:
                pass
        return vysl
    

  1. Riešenie:

    def iba_cisla(meno_suboru):
        vysl = []
        try:
            with open(meno_suboru) as subor:
                for slovo in subor.read().split():
                    try:
                        vysl.append(int(slovo))
                    except ValueError:
                        pass
        except FileNotFoundError:
            return None
        return vysl
    

  1. Riešenie:

    def daj_cislo(meno_suboru, index, vynimka=None):
        try:
            with open(meno_suboru) as subor:
                return int(subor.read().split('\n')[index])
        except FileNotFoundError:
            if vynimka is not None:
                raise vynimka('neexistujuci subor')
        except (IndexError, TypeError):
            if vynimka is not None:
                raise vynimka('chybne zadany index')
        except ValueError:
            if vynimka is not None:
                raise vynimka('chybne cislo v zadanom riadku')
    

  1. Riešenie:

    def rgb(r, g, b):
        assert isinstance(r, int) and 0<=r<=255, 'chybny prvy parameter r'
        assert isinstance(g, int) and 0<=g<=255, 'chybny druhy parameter g'
        assert isinstance(b, int) and 0<=b<=255, 'chybny treti parameter b'
        return f'#{r:02x}{g:02x}{b:02x}'
    

  1. Riešenie:

    def sustavy(retazec):
        vysl = []
        for i in range(17):
            try:
                vysl.append(int(retazec, i))
            except ValueError:
                vysl.append(None)
        return vysl
    

  1. Riešenie:

    class TelefonnyZoznam:
        def __init__(self, meno_suboru=None):
            try:
                self.citaj(meno_suboru)
            except ValueError:
                pass
    
        def pridaj(self, meno, telefon):
            if not isinstance(meno, str):
                raise TypeError('meno musi byt retazec')
            if not isinstance(telefon, str):
                raise TypeError('telefon musi byt retazec')
            for i, (m, t) in enumerate(self.zoznam):
                if m == meno:
                    self.zoznam[i] = meno, telefon
                    return
            self.zoznam.append((meno, telefon))
    
        def zisti(self, meno):
            for m, t in self.zoznam:
                if m == meno:
                    return telefon
            raise KeyError(f'{meno!r} nie je v zozname')
    
        def citaj(self, meno_suboru):
            self.zoznam = []
            try:
                with open(meno_suboru) as subor:
                    for riadok in subor:
                        m, t = riadok.split()
                        self.pridaj(m, t)
            except (TypeError, ValueError, FileNotFoundError):
                raise ValueError(f'chybny subor {meno_suboru!r}')
    
        def zapis(self, meno_suboru):
            try:
                with open(meno_suboru, 'w') as subor:
                    for m, t in self.zoznam:
                        print(m, t, file=subor)
            except (TypeError, PermissionError):
                raise ValueError('chybny subor {meno_suboru!r}')
    
        def vypis(self):
            for m, t in self.zoznam:
                print(m, t)