Zapiski s ponavljanja pred izpitom
Zahteve zaključka
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())
In [4]:
def najvec_sester_v_druzini(druzina):
print([je_zenska(ime) for ime in druzina])
print(najvec_sester_v_druzini(["Maja", "Aleksander", "Petra"]))
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"]))
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"]))
In [10]:
[najvec_sester_v_druzini(druzina) for druzina in otroci.values()]
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
In [12]:
[najvec_sester_v_druzini(druzina) for druzina in otroci.values()]
Out[12]:
In [13]:
max([najvec_sester_v_druzini(druzina) for druzina in otroci.values()])
Out[13]:
In [14]:
max(najvec_sester_v_druzini(druzina) for druzina in otroci.values())
Out[14]:
In [ ]:
In [15]:
max([najvec_sester_v_druzini(druzina) for druzina in otroci.values()])[1]
Out[15]:
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")
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")
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]:
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]:
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]:
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]:
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]:
In [25]:
palindrom("abcb")
Out[25]:
In [26]:
palindrom("aa")
Out[26]:
In [27]:
palindrom("aaa")
Out[27]:
In [28]:
palindrom("a")
Out[28]:
In [29]:
palindrom("")
Out[29]:
In [30]:
def decimalno(s):
return int(s[-1]) + 2 * decimalno(s[:-1])
In [31]:
decimalno("101010")
In [35]:
def decimalno(s):
if not s:
return 0
return int(s[-1]) + 2 * decimalno(s[:-1])
In [36]:
decimalno("101010")
Out[36]:
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")
Out[44]:
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]:
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]:
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]:
In [53]:
binarno(1)
Out[53]:
In [54]:
binarno(0)
Out[54]:
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]:
In [57]:
binarno(0)
Out[57]:
In [58]:
binarno(1)
Out[58]:
In [59]:
def binarno(n):
niz = ""
while n > 0:
niz = str(n % 2) + niz
n //= 2
return niz
In [60]:
binarno(42)
Out[60]:
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]:
In [64]:
binarno(0)
Out[64]:
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]:
In [78]:
dict.fromkeys(range(8), None)
Out[78]:
In [ ]:
Zadnja sprememba: torek, 17. februar 2015, 12.13