Notebook
In [1]:
otroci = {</span>
    <span class="s">"Adam"</span><span class="p">:</span> <span class="p">[</span><span class="s">"Matjaž"</span><span class="p">,</span> <span class="s">"Cilka"</span><span class="p">,</span> <span class="s">"Daniel"</span><span class="p">,</span> <span class="s">"Erik"</span><span class="p">],</span>
    <span class="s">"Aleksander"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Alenka"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Barbara"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Cilka"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Daniel"</span><span class="p">:</span> <span class="p">[</span><span class="s">"Elizabeta"</span><span class="p">,</span> <span class="s">"Hans"</span><span class="p">],</span>
    <span class="s">"Erik"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Elizabeta"</span><span class="p">:</span> <span class="p">[</span><span class="s">"Ludvik"</span><span class="p">,</span> <span class="s">"Jurij"</span><span class="p">,</span> <span class="s">"Barbara"</span><span class="p">,</span> <span class="s">"Herman"</span><span class="p">,</span> <span class="s">"Mihael"</span><span class="p">],</span>
    <span class="s">"Franc"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Herman"</span><span class="p">:</span> <span class="p">[</span><span class="s">"Margareta"</span><span class="p">],</span>
    <span class="s">"Hans"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Jožef"</span><span class="p">:</span> <span class="p">[</span><span class="s">"Alenka"</span><span class="p">,</span> <span class="s">"Aleksander"</span><span class="p">,</span> <span class="s">"Petra"</span><span class="p">],</span>
    <span class="s">"Jurij"</span><span class="p">:</span> <span class="p">[</span><span class="s">"Franc"</span><span class="p">,</span> <span class="s">"Jožef"</span><span class="p">],</span>
    <span class="s">"Ludvik"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Margareta"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Matjaž"</span><span class="p">:</span> <span class="p">[</span><span class="s">"Viljem"</span><span class="p">],</span>
    <span class="s">"Mihael"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Petra"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Tadeja"</span><span class="p">:</span> <span class="p">[],</span>
    <span class="s">"Viljem"</span><span class="p">:</span> <span class="p">[</span><span class="s">"Tadeja"</span><span class="p">],</span>
<span class="p">}

def je_zenska(ime):
    return ime[-1] == "a"

def koliko_sester(ime):
    sester = 0
    for druzina in otroci.values():
        if ime in druzina:
            break
    else:
        druzina = [ime]
    for clovek in druzina:
        if je_zenska(clovek):
            sester += 1
    if je_zenska(ime):
        sester -= 1
    return sester


def najvec_sester():
    s = []
    for clovek in otroci:
        s.append((koliko_sester(clovek), clovek))
    return max(s)[1]

print(najvec_sester())
Aleksander

In [4]:
def najvec_sester_v_druzini(druzina):
    print([je_zenska(ime) for ime in druzina])
    
print(najvec_sester_v_druzini(["Maja", "Aleksander", "Petra"]))
[True, False, True]
None

In [7]:
def najvec_sester_v_druzini(druzina):
    return sum([je_zenska(ime) for ime in druzina])
    
print(najvec_sester_v_druzini(["Maja", "Aleksander", "Petra"]))
2

In [9]:
def najvec_sester_v_druzini(druzina):
    zensk = sum(je_zenska(ime) for ime in druzina)
    if zensk == len(druzina):
        return zensk - 1, druzina[0]
    else:
        moski = [ime for ime in druzina if not je_zenska(ime)]
        return zensk, moski[0]
    
print(najvec_sester_v_druzini(["Maja", "Aleksander", "Petra"]))
print(najvec_sester_v_druzini(["Maja", "Aleksander", "Petra", "Jože"]))
print(najvec_sester_v_druzini(["Maja", "Aleksandra", "Petra"]))
(2, 'Aleksander')
(2, 'Aleksander')
(2, 'Maja')

In [10]:
[najvec_sester_v_druzini(druzina) for druzina in otroci.values()]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-10-1586b4bdb666> in <module>()
----> 1 [najvec_sester_v_druzini(druzina) for druzina in otroci.values()]

<ipython-input-10-1586b4bdb666> in <listcomp>(.0)
----> 1 [najvec_sester_v_druzini(druzina) for druzina in otroci.values()]

<ipython-input-9-d72043a89c40> in najvec_sester_v_druzini(druzina)
      2     zensk = sum(je_zenska(ime) for ime in druzina)
      3     if zensk == len(druzina):
----> 4         return zensk - 1, druzina[0]
      5     else:
      6         moski = [ime for ime in druzina if not je_zenska(ime)]

IndexError: list index out of range
In [11]:
def najvec_sester_v_druzini(druzina):
    zensk = sum(je_zenska(ime) for ime in druzina)
    if not zensk:
        return 0, ""
    if zensk == len(druzina):
        return zensk - 1, druzina[0]
    else:
        moski = [ime for ime in druzina if not je_zenska(ime)]
        return zensk, moski[0]
    
print(najvec_sester_v_druzini(["Maja", "Aleksander", "Petra"]))
print(najvec_sester_v_druzini([]))v
(2, 'Aleksander')
(0, '')

In [12]:
[najvec_sester_v_druzini(druzina) for druzina in otroci.values()]
Out[12]:
[(0, ''),
 (0, ''),
 (1, 'Matjaž'),
 (0, ''),
 (0, ''),
 (0, ''),
 (0, ''),
 (1, 'Hans'),
 (0, ''),
 (0, ''),
 (2, 'Urša'),
 (1, 'Ludvik'),
 (0, ''),
 (2, 'Aleksander'),
 (0, ''),
 (0, ''),
 (0, ''),
 (0, ''),
 (0, 'Tadeja'),
 (0, 'Margareta')]
In [13]:
max([najvec_sester_v_druzini(druzina) for druzina in otroci.values()])
Out[13]:
(2, 'Urša')
In [14]:
max(najvec_sester_v_druzini(druzina) for druzina in otroci.values())
Out[14]:
(2, 'Urša')
In [ ]:
 
In [15]:
max([najvec_sester_v_druzini(druzina) for druzina in otroci.values()])[1]
Out[15]:
'Urša'
In [16]:
def najvec_sester(ime):
    druzina = otroci[ime]
    naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
    for otrok in druzina:
        sester, ime = najvec_sester(otrok)
        if sester > naj_sester:
            naj_sester = sester
            naj_ime = ime
    return naj_ime

najvec_sester("Adam")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-76b10d53dad5> in <module>()
      9     return naj_ime
     10 
---> 11 najvec_sester("Adam")

<ipython-input-16-76b10d53dad5> in najvec_sester(ime)
      3     naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
      4     for otrok in druzina:
----> 5         sester, ime = najvec_sester(otrok)
      6         if sester > naj_sester:
      7             naj_sester = sester

<ipython-input-16-76b10d53dad5> in najvec_sester(ime)
      3     naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
      4     for otrok in druzina:
----> 5         sester, ime = najvec_sester(otrok)
      6         if sester > naj_sester:
      7             naj_sester = sester

<ipython-input-16-76b10d53dad5> in najvec_sester(ime)
      3     naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
      4     for otrok in druzina:
----> 5         sester, ime = najvec_sester(otrok)
      6         if sester > naj_sester:
      7             naj_sester = sester

ValueError: need more than 0 values to unpack
In [17]:
def najvec_sester(ime):
    druzina = otroci[ime]
    naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
    for otrok in druzina:
        sester, ime = najvec_sester(otrok)
        if sester > naj_sester:
            naj_sester = sester
            naj_ime = ime
    return naj_sester, naj_ime

najvec_sester("Adam")
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-17-5f80fb4e5f6f> in <module>()
      9     return naj_sester, naj_ime
     10 
---> 11 najvec_sester("Adam")

<ipython-input-17-5f80fb4e5f6f> in najvec_sester(ime)
      3     naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
      4     for otrok in druzina:
----> 5         sester, ime = najvec_sester(otrok)
      6         if sester > naj_sester:
      7             naj_sester = sester

<ipython-input-17-5f80fb4e5f6f> in najvec_sester(ime)
      3     naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
      4     for otrok in druzina:
----> 5         sester, ime = najvec_sester(otrok)
      6         if sester > naj_sester:
      7             naj_sester = sester

<ipython-input-17-5f80fb4e5f6f> in najvec_sester(ime)
      3     naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
      4     for otrok in druzina:
----> 5         sester, ime = najvec_sester(otrok)
      6         if sester > naj_sester:
      7             naj_sester = sester

<ipython-input-17-5f80fb4e5f6f> in najvec_sester(ime)
      3     naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
      4     for otrok in druzina:
----> 5         sester, ime = najvec_sester(otrok)
      6         if sester > naj_sester:
      7             naj_sester = sester

<ipython-input-17-5f80fb4e5f6f> in najvec_sester(ime)
      3     naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
      4     for otrok in druzina:
----> 5         sester, ime = najvec_sester(otrok)
      6         if sester > naj_sester:
      7             naj_sester = sester

<ipython-input-17-5f80fb4e5f6f> in najvec_sester(ime)
      3     naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
      4     for otrok in druzina:
----> 5         sester, ime = najvec_sester(otrok)
      6         if sester > naj_sester:
      7             naj_sester = sester

<ipython-input-17-5f80fb4e5f6f> in najvec_sester(ime)
      1 def najvec_sester(ime):
----> 2     druzina = otroci[ime]
      3     naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
      4     for otrok in druzina:
      5         sester, ime = najvec_sester(otrok)

KeyError: 'Urša'
In [18]:
otroci["Aleksander"] = []
In [19]:
def najvec_sester(ime):
    druzina = otroci[ime]
    naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
    for otrok in druzina:
        sester, ime = najvec_sester(otrok)
        if sester > naj_sester:
            naj_sester = sester
            naj_ime = ime
    return naj_sester, naj_ime

najvec_sester("Adam")
Out[19]:
(2, 'Aleksander')
In [20]:
def najvec_sester0(ime):
    druzina = otroci[ime]
    naj_sester, naj_ime = najvec_sester_v_druzini(druzina)
    for otrok in druzina:
        sester, ime = najvec_sester0(otrok)
        if sester > naj_sester:
            naj_sester = sester
            naj_ime = ime
    return naj_sester, naj_ime

def najvec_sester(ime):
    return najvec_sester0(ime)[1]

najvec_sester("Adam")
Out[20]:
'Aleksander'
In [21]:
def najvec_sester_v_druzini(druzina):
    zensk = sum(je_zenska(ime) for ime in druzina)
    return zensk - (zensk == len(druzina))
    
def najvec_sester(ime):
    druzina = otroci[ime]
    naj_sester = najvec_sester_v_druzini(druzina)
    for otrok in druzina:
        sester = najvec_sester(otrok)
        if sester > naj_sester:
            naj_sester = sester
    return naj_sester

najvec_sester("Adam")
Out[21]:
2
In [22]:
def najvec_sester_v_druzini(druzina):
    zensk = sum(je_zenska(ime) for ime in druzina)
    return zensk - (zensk == len(druzina))
    
def najvec_sester(ime):
    druzina = otroci[ime]
    naj_sester = najvec_sester_v_druzini(druzina)
    return max([najvec_sester(otrok) for otrok in druzina] + [naj_sester])

najvec_sester("Adam")
Out[22]:
2
trenutna trenutna.leva, trenutna.desna for soba in [trenutna.leva, trenutna.desna]: rezultat = rekurzivni klic (soba) zbiraš: - seštevaš, unije, preseke - primerjaš ... recimo maksimum - any, all (or, and)
In [23]:
def palindrom(s):
    return len(s) < 2    or    s[0] == s[-1] and palindrom(s[1:-1])
In [24]:
palindrom("abcba")
Out[24]:
True
In [25]:
palindrom("abcb")
Out[25]:
False
In [26]:
palindrom("aa")
Out[26]:
True
In [27]:
palindrom("aaa")
Out[27]:
True
In [28]:
palindrom("a")
Out[28]:
True
In [29]:
palindrom("")
Out[29]:
True
Iz dvojiškega v desetiško: k zadnji števki + 2 x iz dvojiškega v desetiško(vse razen števke)
In [30]:
def decimalno(s):
    return int(s[-1]) + 2 * decimalno(s[:-1])
In [31]:
decimalno("101010")
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-31-9088595fa905> in <module>()
----> 1 decimalno("101010")

<ipython-input-30-3a77a325a4e6> in decimalno(s)
      1 def decimalno(s):
----> 2     return int(s[-1]) + 2 * decimalno(s[:-1])

<ipython-input-30-3a77a325a4e6> in decimalno(s)
      1 def decimalno(s):
----> 2     return int(s[-1]) + 2 * decimalno(s[:-1])

<ipython-input-30-3a77a325a4e6> in decimalno(s)
      1 def decimalno(s):
----> 2     return int(s[-1]) + 2 * decimalno(s[:-1])

<ipython-input-30-3a77a325a4e6> in decimalno(s)
      1 def decimalno(s):
----> 2     return int(s[-1]) + 2 * decimalno(s[:-1])

<ipython-input-30-3a77a325a4e6> in decimalno(s)
      1 def decimalno(s):
----> 2     return int(s[-1]) + 2 * decimalno(s[:-1])

<ipython-input-30-3a77a325a4e6> in decimalno(s)
      1 def decimalno(s):
----> 2     return int(s[-1]) + 2 * decimalno(s[:-1])

<ipython-input-30-3a77a325a4e6> in decimalno(s)
      1 def decimalno(s):
----> 2     return int(s[-1]) + 2 * decimalno(s[:-1])

IndexError: string index out of range
In [35]:
def decimalno(s):
    if not s:
        return 0
    return int(s[-1]) + 2 * decimalno(s[:-1])
In [36]:
decimalno("101010")
Out[36]:
42
In [43]:
def decimalno(s):
    if not s:
        return 0
    r = decimalno(s[:-1])
    bom_vrnil = int(s[-1]) + 2 * r
    print("dobil sem", s)
    print("zadnja: ", s[-1])
    print("klical sem decimalno(", s[:-1], ")")
    print("dobil sem", r)
    print("vrnil bom", bom_vrnil)
    print("-" * 20)
    return bom_vrnil
In [44]:
decimalno("111010")
dobil sem 1
zadnja:  1
klical sem decimalno(  )
dobil sem 0
vrnil bom 1
--------------------
dobil sem 11
zadnja:  1
klical sem decimalno( 1 )
dobil sem 1
vrnil bom 3
--------------------
dobil sem 111
zadnja:  1
klical sem decimalno( 11 )
dobil sem 3
vrnil bom 7
--------------------
dobil sem 1110
zadnja:  0
klical sem decimalno( 111 )
dobil sem 7
vrnil bom 14
--------------------
dobil sem 11101
zadnja:  1
klical sem decimalno( 1110 )
dobil sem 14
vrnil bom 29
--------------------
dobil sem 111010
zadnja:  0
klical sem decimalno( 11101 )
dobil sem 29
vrnil bom 58
--------------------

Out[44]:
58
In [45]:
def binarno(n):
    if n == 0:
        return "0"
    zadnja = str(n % 2)
    pred_tem = binarno(n // 2)
    return (pred_tem + zadnja)
In [46]:
binarno(42)
Out[46]:
'0101010'
In [49]:
def binarno(n):
    if n == 0:
        return "0"
    zadnja = str(n % 2)
    pred_tem = binarno(n // 2)
    return (pred_tem + zadnja)[:-1]
In [50]:
binarno(42)
Out[50]:
'0'
In [51]:
def binarno(n):
    if n == 0:
        return "0"
    zadnja = str(n % 2)
    pred_tem = binarno(n // 2)
    return (pred_tem + zadnja).lstrip("0")
In [52]:
binarno(42)
Out[52]:
'101010'
In [53]:
binarno(1)
Out[53]:
'1'
In [54]:
binarno(0)
Out[54]:
'0'
In [55]:
def binarno(n):
    if n <= 1:
        return str(n)
    zadnja = str(n % 2)
    pred_tem = binarno(n // 2)
    return (pred_tem + zadnja)
In [56]:
binarno(42)
Out[56]:
'101010'
In [57]:
binarno(0)
Out[57]:
'0'
In [58]:
binarno(1)
Out[58]:
'1'
In [59]:
def binarno(n):
    niz = ""
    while n > 0:
        niz = str(n % 2) + niz
        n //= 2
    return niz
    
In [60]:
binarno(42)
Out[60]:
'101010'
In [61]:
binarno(0)
Out[61]:
''
In [62]:
def binarno(n):
    if n == 0:
        return "0"
    niz = ""
    while n > 0:
        niz = str(n % 2) + niz
        n //= 2
    return niz
    
In [63]:
binarno(42)
Out[63]:
'101010'
In [64]:
binarno(0)
Out[64]:
'0'
In [65]:
def palindrom(s):
    for i in range(len(s) // 2):
        if s[i] != s[- i - 1]:
            return False
    return True
In [66]:
def palindrom(s):
    return s == s[::-1]
In [67]:
class Zelva:
    def naprej(self, x):
        pass

Včasih definiraš samo razred in metode. Tu ne bo super.

Včasih dobiš razred in definiraš naslednike ... ali definiraš oboje, razred in naslednike.

In [70]:
class PosebnaZelva(Zelva):
    # Ta zelva zna vse, kar zna zelva
    # Recimo, da gre naprej za dvakrat več kot "običajna" želva
    def naprej(self, x):
        print("hop")
        super().naprej(2 * x) # super zato, da pokličem podedovano, ne svoje
        # self.naprej(2 * x)

S super pokličem to, kar sem povozil.

In [71]:
class PosebnaZelva(Zelva):
    # Ta zelva zna vse, kar zna zelva
    # Recimo, da gre naprej za dvakrat več kot "običajna" želva
    def bolj_naprej(self, x):
        print("hop")
        self.naprej(2 * x) # kličem podedovani naprej
In [72]:
class Blok:
    def __init__(self, nadstropij):
        self.stanovalci = [None] * nadstropij
        
    def vseli(self, nadstropje, ime):
        if self.stanovalci[nadstropje] != None:
            return False
        self.stanovalci[nadstropje] = ime
        return True
    
    def stanovalec(self, nadstropje):
        # if self.stanovalci[nadstropje] != None: ---> Ni potrebno!
        #    return self.stanovalci[nadstropje]
        # else:
        #    return None
        return self.stanovalci[nadstropje]
    
    def izseli(self, nadstropje):
        self.stanovalci[nadstropje] = None
        
    def koliko_nadstropij(self):
        return len(self.stanovalci)
    
    def kamorkoli(self, ime):
        for i in range(len(stanovalci) - 1, -1, -1):
            if self.stanovalci[i] == None:
                self.stanovalci[i] = ime
                return True
        return False
    
In [74]:
# S slovarji - čeprav mi ni prav všeč ;)

class Blok:
    def __init__(self, nadstropij):
        self.stanovalci = {}
        for i in range(nadstropij):
            self.stanovalci[i] = None
        
    def vseli(self, nadstropje, ime):
        if self.stanovalci[nadstropje] != None:
            return False
        self.stanovalci[nadstropje] = ime
        return True
    
    def stanovalec(self, nadstropje):
        # if self.stanovalci[nadstropje] != None: ---> Ni potrebno!
        #    return self.stanovalci[nadstropje]
        # else:
        #    return None
        return self.stanovalci[nadstropje]
    
    def izseli(self, nadstropje):
        self.stanovalci[nadstropje] = None
        
    def koliko_nadstropij(self):
        return len(self.stanovalci)
    
    def kamorkoli(self, ime):
        for i in range(len(stanovalci) - 1, -1, -1):
            if self.stanovalci[i] == None:
                self.stanovalci[i] = ime
                return True
        return False
    
In [77]:
dict.fromkeys(["a", "b", "c"], 42)
Out[77]:
{'b': 42, 'c': 42, 'a': 42}
In [78]:
dict.fromkeys(range(8), None)
Out[78]:
{0: None, 1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None}
In [ ]:
 
Last modified: Tuesday, 17 February 2015, 12:13 PM