3 # Generate youtube signature algorithm from test cases
8 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<",
9 "J:|}][{=+-_)(*&;%$#@>MNBVCXZASDFGH^KLPOIUYTREWQ0987654321mnbvcxzasdfghrklpoiuytej"),
10 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>.<",
11 "!?;:|}][{=+-_)(*&^$#@/MNBVCXZASqFGHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytr"),
12 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
13 "ertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!/#$%^&*()_-+={[|};?@"),
14 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?/>.<",
15 "{>/?;}[.=+-_)(*&^%$#@!MqBVCXZASDFwHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytr"),
16 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?>.<",
17 "<.>?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWe098765432rmnbvcxzasdfghjklpoiuyt1"),
18 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
19 "D.>/?;}[{=+_)(*&^%$#!MNBVCXeAS<FGHJKLPOIUYTREWZ0987654321mnbvcxzasdfghjklpoiuytrQ"),
20 ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
21 "Q>/?;}[{=+-(*<^%$#@!MNBVCXZASDFGHKLPOIUY8REWT0q&7654321mnbvcxzasdfghjklpoiuytrew9"),
24 def find_matching(wrong, right):
25 idxs = [wrong.index(c) for c in right]
27 return ('s[%d]' % i for i in idxs)
30 def _genslice(start, end, step):
31 starts = '' if start == 0 else str(start)
32 ends = ':%d' % (end+step)
33 steps = '' if step == 1 else (':%d' % step)
34 return 's[%s%s%s]' % (starts, ends, steps)
37 for i, prev in zip(idxs[1:], idxs[:-1]):
41 yield _genslice(start, prev, step)
44 if i - prev in [-1, 1]:
53 yield _genslice(start, i, step)
55 def _assert_compress(inp, exp):
56 res = list(compress(inp))
58 print('Got %r, expected %r' % (res, exp))
60 _assert_compress([0,2,4,6], ['s[0]', 's[2]', 's[4]', 's[6]'])
61 _assert_compress([0,1,2,4,6,7], ['s[:3]', 's[4]', 's[6:8]'])
62 _assert_compress([8,0,1,2,4,7,6,9], ['s[8]', 's[:3]', 's[4]', 's[7:5:-1]', 's[9]'])
64 def gen(wrong, right, indent):
65 code = ' + '.join(find_matching(wrong, right))
66 return 'if len(s) == %d:\n%s return %s\n' % (len(wrong), indent, code)
70 return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
75 if __name__ == '__main__':