-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2019-BIO-3.py
More file actions
58 lines (51 loc) · 1.88 KB
/
2019-BIO-3.py
File metadata and controls
58 lines (51 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Block-chain
from functools import lru_cache
# Depth-first search
alphabet = "ABCDEFGHIJKLMNOPQRST"
alphabet_dict = {}
for i in range(len(alphabet)):
alphabet_dict[alphabet[i]] = i
def to_letters(chain):
result = ""
for n in chain:
result += alphabet[n]
return result
def from_letters(chain):
result = []
for letter in chain:
result.append(alphabet_dict[letter])
return result
@lru_cache(maxsize=None)
def block_chains(chain:tuple, length:int):
if(len(chain) == length):
# print(to_letters(chain), ": Found a block-chain!")
return 1 # Found a blockchain!
else:
min_first = length # First of two adjacent letters, any larger letter after indicates 2-in-a-row
min_second = length # Second of two adjacent letters, any larger letter after indicates 3-in-a-row
for block in chain:
# Check first number, find lowest
if(block < min_first):
min_first = block
elif(block > min_first):
# Check second number larger than first, find lowest
if (block < min_second):
min_second = block
elif (block > min_second):
# Third in a row
# print(to_letters(chain), ": Three-in-a-row - dead end")
return 0
# print(to_letters(chain), ": Any letter up to", to_letters([min_second]))
chains = 0
for i in range(min_second):
# Third < min_second, to prevent 3-in-a-row
if(not i in chain):
# Possible chain
chains += block_chains(chain + (i,), length)
return chains
while True:
chain = from_letters(input("Chain: "))
for i in range(len(chain)):
chain[i] = int(chain[i])
length = int(input("No. of letters: "))
print("Answer:", block_chains(tuple(chain), length))