### Naloga 1
def cas_v_sekunde(s):
h, m, s = s.split(":")
return 3600*int(h ) + 60*int(m) + int(s)
def sekunde_v_cas(s):
return "%02i:%02i:%02i" % (s/3600, s%3600/60, s%60)
def razlika_casov(s1, s2):
return sekunde_v_cas(cas_v_sekunde(s2) - cas_v_sekunde(s1))
### Naloga 2
def naj_prileg(s, sub):
najpos = najuj = -1
for pos in range(len(s)-len(sub)+1):
uj = 0
for c1, c2 in zip(s[pos:], sub):
if c1 == c2:
uj += 1
if uj > najuj:
najpos, najuj = pos, uj
return najpos, najuj, s[najpos:najpos+len(sub)]
### Naloga 3
def beri_skupine(fname):
users = {}
for line in file(fname):
group_name, password, group_id, group_list = line.strip().split(":")
for user in group_list.split(","):
if not user in users:
users[user] = [group_name]
else:
users[user].append(group_name)
return users
### Naloga 4
import re
re_ident = re.compile("[A-Za-z_]\w*")
keywords = ["and", "del", "from", "not", "while", "as", "elif", "global", "or", "with", "assert", "else", "if",
"pass", "yield", "break", "except", "import", "print", "class", "exec", "in", "raise", "continue",
"finally", "is", "return", "def", "for", "lambda", "try"]
def imena(fname):
spremenljivke = set()
for line in file(fname):
line = line.strip().split("#")[0]
spremenljivke.update(re_ident.findall(line))
return sorted(spremenljivke.difference(keywords))
### Naloga 5
def najvec_n(l, n):
nl = []
for e in l:
if nl.count(e) < n:
nl.append(e)
return nl