alessandrosolbiati's blog

By alessandrosolbiati, 6 years ago, In English

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.me/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.me/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.me/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.me/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.me/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.me/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.me/contest/1082/problem/B

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

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

https://codeforces.me/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.me/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

| Write comment?
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).

»
6 years ago, # |
  Vote: I like it +9 Vote: I do not like it
v = sorted(v)[::-1]

My eyes are bleeding.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Why? Seems like legit way to say 'sort in decreasing order'

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it +34 Vote: I do not like it

      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)
      
      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        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

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          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).

          • »
            »
            »
            »
            »
            »
            6 years ago, # ^ |
              Vote: I like it +3 Vote: I do not like it

            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])
            
            • »
              »
              »
              »
              »
              »
              »
              6 years ago, # ^ |
                Vote: I like it +5 Vote: I do not like it

              I swapped testA and testB and got another result


              proof
              • »
                »
                »
                »
                »
                »
                »
                »
                6 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                maybe because I am using Python2?

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  6 years ago, # ^ |
                  Rev. 2   Vote: I like it +8 Vote: I do not like it

                  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.

»
6 years ago, # |
Rev. 3   Vote: I like it +13 Vote: I do not like it

Here's another submission by kevinsogo on this problem -> http://codeforces.me/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'
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

correct way to print array and iterable in python 3:

l = [1, 2, 3, 4, 5]
print(*l, sep=' ')
print(*range(5))
»
6 years ago, # |
  Vote: I like it +8 Vote: I do not like it

lambda: (int, input().split())

lambda: map(int, input().split())?

key=lambda x: len(x)

key=len

v = sorted(v)[::-1]

v.sort(reverse=True)

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I feel you should have used CF API rather than scraping HTML

One 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 .

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).

»
6 years ago, # |
Rev. 4   Vote: I like it +1 Vote: I do not like it

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))
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by alessandrosolbiati (previous revision, new revision, compare).