Compare commits

...

3 commits

Author SHA1 Message Date
Gérald L
6a53ef0852 add utils function + docstring 2021-01-29 17:54:43 +01:00
Gérald L
72d874151c Add game explanation 2021-01-29 17:54:12 +01:00
Gérald L
7f22bdf78d Ignore idea 2021-01-29 17:53:55 +01:00
3 changed files with 77 additions and 0 deletions

3
.gitignore vendored
View file

@ -1,3 +1,6 @@
# Idea
.idea
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/

BIN
marinbad/Marienbad.pdf Normal file

Binary file not shown.

74
marinbad/func.py Normal file
View file

@ -0,0 +1,74 @@
def toBin(integer):
"""
Convert a decimal integer into a binary"
:param integer: the integer to be converted
:type integer int
:return: int
"""
return int(str(bin(integer))[2:])
def toDec(binVal):
"""
Convert a binary value to a decimal value
:param binVal: The bin value to be converted
:type binVal: int, str
:return: int
"""
return int(str(binVal), 2)
def toBinArray(array):
"""
Turn an array of decimal into an array of binary
:param array:
:return:
"""
return [toBin(el) for el in array]
def computeS(array):
"""
Sum all values in an array
:param array: the array to compute on
:type array: list
:return: int
"""
return sum(array)
def nimSomme(integer):
"""Return an array with 0 if element is even, 1 otherwise"""
return [int(el) % 2 for el in str(integer)]
def isSafe(array):
"""
Return True if value computed from the array is zero, False otherwise
:param array: the array to compute
:type array: list
:return: bool
"""
def list2str(array):
a = ""
for el in array:
a += str(el)
return a
binString = list2str([int(el) % 2 for el in list2str(array)])
return toDec(binString) == 0
array = [3, 5, 7]
binArr = toBinArray(array)
print("Bin Array: ", binArr)
S = computeS(binArr)
print("S: ", S)
nimS = nimSomme(S)
print("Somme de NIM: ", nimS)
safe = isSafe(nimS)
print("Safe: ", safe)