### alessandrosolbiati's blog

By alessandrosolbiati, 3 years ago,

TEMPLATE FUNCTIONS:

stdin = lambda type_ = "int", sep = " ": list(map(eval(type_), raw_input().split(sep)))
"""
>>> stdin()
1 2 3 4
>>>[1, 2, 3, 4]
"""

joint = lambda sep = " ", *args: sep.join(str(i) if type(i) != list else sep.join(map(str, i)) for i in args)
"""
>>> joint(" ", [1, 2])
'1 2'
>>> joint(" ", [1, 2], [2, 3])
'1 2 2 3'
"""

def iters(): return map(int(raw_input()))

PROBLEMS:

==================

http://codeforces.com/contest/988/problem/B

n = int(input())
a = sorted((input() for _ in range(n)), key=lambda x: len(x))
v = all(a[i] in a[i+1] for i in range(n-1))
print('YES\n'+"\n".join(a) if v else 'NO')

==================

http://codeforces.com/contest/994/problem/A

R = lambda: (int, input().split())
n, a, b = R(), R(), R()
print(' '.join(map(str, (x for x in a if x in b))))

==================

http://codeforces.com/contest/994/problem/B

R = lambda: map(int, input().split())
n, k = R(),
v, t = [], [0]*n
for p, c, i in sorted(zip(R(), R(), range(n))):
t[i] = sum(v)+c
v += [c]
v = sorted(v)[::-1]
if len(v) > k:
v.pop()
print(' '.join(map(str, t)))

==================

http://codeforces.com/contest/754/problem/A

n, a = int(input()), list(map(int, input().split()))
x = next((i for i, ai in enumerate(a) if ai), None) # find index of first non-zero elements in a
if x is None:
print('NO')
elif sum(a):
print('YES', 1, '1 {}'.format(n), sep='\n')
else:
print('YES', 2, '1 {}'.format(x + 1), '{} {}'.format(x + 2, n), sep='\n')

==================

http://codeforces.com/contest/996/problem/B

import math n, a, m, res = int(input()), [*(map(int,input().split()))], 10e9,-1 k = [ math.ceil((ai-i)/n) for i,ai in enumerate(a) ] print(k.index(min(k))+1)

==================

https://codeforces.com/contest/1082/problem/B

_, string = raw_input(), raw_input()
start, end, res = 0, 0, 0
for char in string:
if char == "G": start += 1
else: end, start = start, 0
res = max(res, start + end + 1)
print min(res, string.count("G"))

==================

https://codeforces.com/contest/1082/problem/B

x = int(raw_input())
print " ".join(map(str, [x, x])) if x != 1 else -1

==================

https://codeforces.com/contest/1082/problem/B

n, k = map(int, raw_input().split())
numbers = map(int, raw_input().split())
res, numbers = [], [0] + sorted(list(set(numbers)))
for i in xrange(min(len(numbers) - 1, k)): print (numbers[i + 1] - numbers[i])
for _ in xrange(k - len(numbers) + 1): print 0

==================

https://codeforces.com/contest/1092/problem/A

for _ in iters():
n, k = stdin()
print joint('', ([chr(ord('a') + i%k) for i in xrange(n)]))

==================

*find more here https://github.com/SolbiatiAlessandro/pyComPro

• +40

 » 3 years ago, # |   0 Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).
 » 3 years ago, # |   +9 v = sorted(v)[::-1] My eyes are bleeding.
•  » » 3 years ago, # ^ |   +3 Why? Seems like legit way to say 'sort in decreasing order'
•  » » » 3 years ago, # ^ |   +34 Well, the purpose of this blog is to show some elegant python and not introduce obvious overkills :)It can be easily reduced to v.sort(reverse = True)
•  » » » » 3 years ago, # ^ |   +1 even if it might not be good for readability, the slicing expression is NOT an overkill performance wise: on a string of Len 200, using the slicing notation is on average 10 times faster than any other reversion method.You can read more about it here (Timing):https://stackoverflow.com/a/27843760
•  » » » » » 3 years ago, # ^ |   0 It seems that this SO question is not related to the problem. We are not performing string reverses in pure form. We are sorting integers by descending order by using standard function argument or by sorting in ascending and then reversing it.As reverse operation is O(N) and sorting is O(N·log(N)) it is not possible to make execution time few times worse or few times better just by varying methods of reversing.It would be great to make some benchmarking comparing sorted()[::-1] and sort(reverse = True).
•  » » » » » » 3 years ago, # ^ |   +3 You are right, it's not directly related. Nonetheless, I run some basic benchmark and slicing notation still looks around 10% faster than "reverse = True" argument Interesting! def testA(a): return sorted(a, reverse=True) def testB(a): return sorted(a)[::-1] a = "aaabbbccc" * 5 if __name__ == '__main__': import timeit print(timeit.timeit("testA(a)", setup="from __main__ import testA, a")) # 4.49819493294 (reverse=True) print(timeit.timeit("testB(a)", setup="from __main__ import testB, a")) # 4.18429493904 ([::-1])
•  » » » » » » » 3 years ago, # ^ |   +5 I swapped testA and testB and got another result
•  » » » » » » » » 3 years ago, # ^ |   0 maybe because I am using Python2?
•  » » » » » » » » » 3 years ago, # ^ | ← Rev. 2 →   +8 No, benchmarking python seems to be strongly biassed.Swapping benchmarking order and running different benchmarks leads to completely different results. Seems like running time almost randomly gets something from -20%/+20% on running time, that depends on order of operations and start of benchmarking time. I've tried different tests locally and in codeforces/customtest, but all of them suffered from such biasses. Strangely, sometimes customtest says that execution time of performing benchmarks of almost the same size (104 + 1 instead of 104) leads to almost 2x execution time.That comes to some really funny things like generating 2M random ints takes more time than generating 2M random ints exactly the same way and sorting some blocks of them (that may turn out not to be so funny when you will receive TL in running contest). I am not sure how should we benchmark python to get reliable results.
 » 3 years ago, # | ← Rev. 3 →   +13 Here's another submission by kevinsogo on this problem -> http://codeforces.com/contest/989/problem/A that I found really cool. a = raw_input() print 'Yes' if set('ABC') in map(set, zip(a, a[1:], a[2:])) else 'No'
 » 3 years ago, # |   0 correct way to print array and iterable in python 3: l = [1, 2, 3, 4, 5] print(*l, sep=' ') print(*range(5))
 » 3 years ago, # |   +8 lambda: (int, input().split()) lambda: map(int, input().split())? key=lambda x: len(x) key=len v = sorted(v)[::-1] v.sort(reverse=True)
 » 3 years ago, # |   0 I feel you should have used CF API rather than scraping HTMLOne of the potential way is to iterate through every contest and get the list of submissions through contest.status .It returns a list of Submission objects . Only downside is that you'll need to somehow create a whitelist of the user IDs .
 » 3 years ago, # |   0 Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).
 » 3 years ago, # |   0 Very nice python code short and strong but some overkills might confuse newbies :)
 » 3 years ago, # | ← Rev. 4 →   +1 Your second code isn't OK.I checked it.It gave me WA in test case 1.Correct code : R = lambda : list(map(int,input().split())) n,a,b = R(),R(),R() print(*(x for x in a if x in b))
 » 3 years ago, # |   0 Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).
 » 3 years ago, # |   0 I wish python will get faster in near future, because it's really good!!I'm using C++ for about a year but still get confused sometimes.
 » 3 years ago, # |   0 Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).
 » 3 years ago, # |   0 Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).
 » 3 years ago, # |   0 Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).
 » 3 years ago, # |   0 Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).