From bafffe33c0839de0c554cb27f1ffe562429cc882 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 22 Nov 2022 18:50:46 +0000 Subject: - Added guest contributions by Eric Cheung. --- challenge-192/eric-cheung/python/ch-1.py | 10 +++++++ challenge-192/eric-cheung/python/ch-2.py | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100755 challenge-192/eric-cheung/python/ch-1.py create mode 100755 challenge-192/eric-cheung/python/ch-2.py diff --git a/challenge-192/eric-cheung/python/ch-1.py b/challenge-192/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..8d5c289448 --- /dev/null +++ b/challenge-192/eric-cheung/python/ch-1.py @@ -0,0 +1,10 @@ + +## nInputNum = 5 ## Example 1 +nInputNum = 4 ## Example 2 +## nInputNum = 6 ## Example 3 + +strBinFormat = bin(nInputNum)[2:] +strBinFlipFormat = "".join(["0" if strCharLoop == "1" else "1" for strCharLoop in strBinFormat]) +nOuputDecFormat = int(strBinFlipFormat, 2) + +print (str(nOuputDecFormat)) diff --git a/challenge-192/eric-cheung/python/ch-2.py b/challenge-192/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..cf44c47f5b --- /dev/null +++ b/challenge-192/eric-cheung/python/ch-2.py @@ -0,0 +1,49 @@ + +import sys +import numpy as np + +def IsEqui(arrCheck): + if arrCheck[0] == arrCheck[1] and arrCheck[1] == arrCheck[2]: + return True + + return False + +def GetMove(arrMove, nFromIndx, nToIndx, nMoveNum): + arrMove[nFromIndx] = arrMove[nFromIndx] - 1 + arrMove[nToIndx] = arrMove[nToIndx] + 1 + + print ("Move #" + str(nMoveNum) + ": " + ", ".join([str(nLoop) for nLoop in arrMove])) + +arrInputList = [1, 0, 5] ## Example 1 +## arrInputList = [0, 2, 0] ## Example 2 +## arrInputList = [0, 3, 0] ## Example 3 + +if sum(arrInputList) % 3 != 0: + print (-1) + sys.exit() + +arrTempList = [nLoop for nLoop in arrInputList] +nCountStep = 0 + +while not IsEqui(arrTempList): + + nArgMaxIndx = np.argmax(arrTempList) + nCountStep = nCountStep + 1 + + if nArgMaxIndx == 0: + if arrTempList[1] > arrTempList[2]: + GetMove(arrTempList, 1, 2, nCountStep) + else: + GetMove(arrTempList, 0, 1, nCountStep) + elif nArgMaxIndx == 1: + if arrTempList[0] > arrTempList[2]: + GetMove(arrTempList, 1, 2, nCountStep) + else: + GetMove(arrTempList, 1, 0, nCountStep) + elif nArgMaxIndx == 2: + if arrTempList[0] >= arrTempList[1]: + GetMove(arrTempList, 2, 1, nCountStep) + else: + GetMove(arrTempList, 1, 0, nCountStep) + +print (nCountStep) -- cgit