Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
rev_order = lambda s: ' '.join(s.split()[::-1])def first_uniq(text):
reject = set(re.findall(r'(.)(?=.*?\1)', text))
iterator = (c for c in text if c not in reject)
return next(iterator, '')
import gc
gc.disable()perl -e 'while (1) {my $x; $x=\$x;}'if delElement == self.head:
self.head = self.head.next
# gc в питоне удалит объект, если на него никто не ссылается
# (reference count)
# или же: del delElement - что скажут знатоки?
return Truefrom itertools import groupby
def non_repeating(line):
for k, g in groupby(line):
g_list = list(g)
if len(g_list) == 1:
return g_list[0]
return None
def non_repeating(line):
return next((k for k, g in groupby(line) if next(g,False) and not next(g,False)), None)import collections
def first_non_repeated_character(str):
cntr = collections.Counter(str)
return next((x for x in str if cntr[x] == 1), None)
>>> import collections
>>> def t(str):
... cntr = collections.Counter(str)
... return next((x for x in str if cntr[x] == 1), None)
...
>>> t('test')
'e'
>>> from timeit import Timer
>>> Timer("t('1 2 3 45')", "from __main__ import t").timeit()
12.961874008178711
>>> non_repeating('test')
't'def first_uniq2(line):
i = 0
for c in line:
i += 1
if c not in line[i:] and c not in line[:i-1]:
return c
def first_uniq3(line):
i = 0
checked = set()
for c in line:
i += 1
if c in checked:
continue
if c not in line[i:]:
return c
checked.add( c )
def first_unique(s):
d = {}
v = len(s)
for i, c in enumerate(s):
if c in d:
d[c] = v
else:
d[c] = i
ind = min(d.values())
return None if ind == v else s[ind]
Я хочу работать в Google! Телефонное интервью (часть 3, питоноводческая)