def euler14(limit):
cache, kolNums = {1: 1}, []
for n in range(2, limit):
while n not in cache:
kolNums.append(n)
n = 3 * n + 1 if n % 2 else n // 2
res = cache[n]
while kolNums:
res += 1
cache[kolNums.pop()] = res
print(*max(cache.items(), key=lambda e: e[1]))
#!/usr/bin/python3
import re
ptrn = re.compile(r'^\s*user_pref\(([^\)]+)\);').search
unic = set()
with open("user.js", "r") as fi, open("user_nodup.js", "w") as fo:
for s in fi:
m = ptrn(s)
if m:
data = m.group(0)
if data in unic:
print(s, end="") # duplicate
continue
unic.add(data)
fo.write(s)
import re
d = {"a b c": "x", "a b": "y", "a": "z"}
data = "a b c a b a a a b c"
print(re.sub(r'a( b( c)?)?', lambda m: d[m.group()], data))
print(re.sub(r'a b c|a b|a', lambda m: d[m.group()], data)) # можно и так