Предизвикателства > Осем-измерна битова баница с късмет > Решения > Решението на Станислава Желязкова

Резултати
0 точки от тестове
0 точки от учител

0 точки общо

0 успешни теста
1 неуспешни теста
Код

 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)

Дискусия
История

nn1import random
1from secret import clue2from secret import clue
n2import randomn
33
4def generate_valid_bw_or_number():4def generate_valid_bw_or_number():
5    while True:5    while True:
6        num = random.randint(1, 255)6        num = random.randint(1, 255)
n7        if bin(num).count('0') <= 1:n7        if bin(num)[2:].count('0') <= 1:
8            return num8            return num
99
10def generate_valid_bw_and_number():10def generate_valid_bw_and_number():
11    while True:11    while True:
12        num = random.randint(1, 255)12        num = random.randint(1, 255)
n13        if bin(num).count('1') <= 1:n13        if bin(num)[2:].count('1') <= 1:
14            return num14            return num
1515
16def lucky():16def 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 = 019    address = 0
2020
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)
n27            result = clue(left_shift = shift)n27            result = clue(left_shift = 6) 
28            address |= result >> shift28            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)
n31            result = clue(right_shift = shift)n32            result = clue(right_shift = 5)
32            address |= result << shift33            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()
n35            result = clue(bw_and = number)n37            result = clue(bw_and = 8)
36            address |= result38            address |= result
nn39 
37        elif arg == 'bw_or':40        elif arg == 'bw_or':
38            number = generate_valid_bw_or_number()41            number = generate_valid_bw_or_number()
n39            result = clue(bw_or = number)n42            result = clue(bw_or = 223) 
40            address |= result43            address |= (result ^ 223) 
4144
42    return address45    return address
tt46 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op