diff --git a/marinbad/func.py b/marinbad/func.py new file mode 100644 index 0000000..8200b3b --- /dev/null +++ b/marinbad/func.py @@ -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)