1import random
2from secret import clue
3
4def generate_valid_bw_or_number():
5 while True:
6 num = random.randint(1, 255)
7 if bin(num)[2:].count('0') <= 1:
8 return num
9
10def generate_valid_bw_and_number():
11 while True:
12 num = random.randint(1, 255)
13 if bin(num)[2:].count('1') <= 1:
14 return num
15
16def lucky():
17 used_args = set()
18 args = ['left_shift', 'right_shift', 'bw_and', 'bw_or']
19 address = 0
20
21 while len(used_args) < 4:
22 arg = random.choice(list(set(args) - used_args))
23 used_args.add(arg)
24
25 if arg == 'left_shift':
26 shift = random.randint(5, 255)
27 result = clue(left_shift = 6)
28 address |= (result >> 6)
29
30 elif arg == 'right_shift':
31 shift = random.randint(5, 255)
32 result = clue(right_shift = 5)
33 address |= (result << 5)
34
35 elif arg == 'bw_and':
36 number = generate_valid_bw_and_number()
37 result = clue(bw_and = 8)
38 address |= result
39
40 elif arg == 'bw_or':
41 number = generate_valid_bw_or_number()
42 result = clue(bw_or = 223)
43 address |= (result ^ 223)
44
45 return address
F
======================================================================
FAIL: test_lucky (test.TestLucky.test_lucky)
Test with various cases.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 30, in test_lucky
self.separate_test(255)
File "/tmp/test.py", line 24, in separate_test
self.assertEqual(solution.lucky(), value)
AssertionError: 235 != 255
----------------------------------------------------------------------
Ran 1 test in 0.003s
FAILED (failures=1)
n | n | 1 | import random | ||
1 | from secret import clue | 2 | from secret import clue | ||
n | 2 | import random | n | ||
3 | 3 | ||||
4 | def generate_valid_bw_or_number(): | 4 | def generate_valid_bw_or_number(): | ||
5 | while True: | 5 | while True: | ||
6 | num = random.randint(1, 255) | 6 | num = random.randint(1, 255) | ||
n | 7 | if bin(num).count('0') <= 1: | n | 7 | if bin(num)[2:].count('0') <= 1: |
8 | return num | 8 | return num | ||
9 | 9 | ||||
10 | def generate_valid_bw_and_number(): | 10 | def generate_valid_bw_and_number(): | ||
11 | while True: | 11 | while True: | ||
12 | num = random.randint(1, 255) | 12 | num = random.randint(1, 255) | ||
n | 13 | if bin(num).count('1') <= 1: | n | 13 | if bin(num)[2:].count('1') <= 1: |
14 | return num | 14 | return num | ||
15 | 15 | ||||
16 | def lucky(): | 16 | def lucky(): | ||
17 | used_args = set() | 17 | used_args = set() | ||
18 | args = ['left_shift', 'right_shift', 'bw_and', 'bw_or'] | 18 | args = ['left_shift', 'right_shift', 'bw_and', 'bw_or'] | ||
19 | address = 0 | 19 | address = 0 | ||
20 | 20 | ||||
21 | while len(used_args) < 4: | 21 | while len(used_args) < 4: | ||
22 | arg = random.choice(list(set(args) - used_args)) | 22 | arg = random.choice(list(set(args) - used_args)) | ||
23 | used_args.add(arg) | 23 | used_args.add(arg) | ||
24 | 24 | ||||
25 | if arg == 'left_shift': | 25 | if arg == 'left_shift': | ||
26 | shift = random.randint(5, 255) | 26 | shift = random.randint(5, 255) | ||
n | 27 | result = clue(left_shift = shift) | n | 27 | result = clue(left_shift = 6) |
28 | address |= result >> shift | 28 | address |= (result >> 6) | ||
29 | |||||
29 | elif arg == 'right_shift': | 30 | elif arg == 'right_shift': | ||
30 | shift = random.randint(5, 255) | 31 | shift = random.randint(5, 255) | ||
n | 31 | result = clue(right_shift = shift) | n | 32 | result = clue(right_shift = 5) |
32 | address |= result << shift | 33 | address |= (result << 5) | ||
34 | |||||
33 | elif arg == 'bw_and': | 35 | elif arg == 'bw_and': | ||
34 | number = generate_valid_bw_and_number() | 36 | number = generate_valid_bw_and_number() | ||
n | 35 | result = clue(bw_and = number) | n | 37 | result = clue(bw_and = 8) |
36 | address |= result | 38 | address |= result | ||
n | n | 39 | |||
37 | elif arg == 'bw_or': | 40 | elif arg == 'bw_or': | ||
38 | number = generate_valid_bw_or_number() | 41 | number = generate_valid_bw_or_number() | ||
n | 39 | result = clue(bw_or = number) | n | 42 | result = clue(bw_or = 223) |
40 | address |= result | 43 | address |= (result ^ 223) | ||
41 | 44 | ||||
42 | return address | 45 | return address | ||
t | t | 46 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|