- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfairRations.py
More file actions
Latest commit
35 lines (23 loc) · 846 Bytes
/
Copy pathfairRations.py
File metadata and controls
35 lines (23 loc) · 846 Bytes
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
ODD='O'
EVEN='E'
defcheckOdd(array: int) ->bool:
returnarray&1
defis_solvable(loavesInHand: [int]) ->bool:
returnnotcheckOdd(sum(loavesInHand))
defnumberToEO(num: int) ->str:
returnODDifcheckOdd(num) elseEVEN
defconvert(loaves: [int]) ->str:
return''.join([numberToEO(x) forxinloaves])
defrsolve(oeloaves: str) ->int:
ifoeloaves.count(ODD) ==0:
return0
ifoeloaves[0] ==EVEN:
returnrsolve(oeloaves[1:])
ifoeloaves[0] ==ODDandoeloaves[1] ==ODD:
return2+rsolve(oeloaves[2:])
ifoeloaves[0] ==ODDandoeloaves[1] ==EVEN:
return2+rsolve(ODD+oeloaves[2:])
defsolve(oeloaves: str) ->int:
Es=oeloaves.split(ODD)
relevantEs=Es[1::2]
returnsum([2*len(x)+2forxinrelevantEs])