From a82fe1af01de90b3ef40cf057cac87063168bb0a Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 6 Jul 2023 02:45:38 +0100 Subject: - Added solutions by Robert DiCicco. - Added solutions by Ulrich Rieke. - Added solutions by Laurent Rosenfeld. - Added solutions by Thomas Kohler. - Added solutions by Simon Proctor. - Added solutions by Mark Anderson. - Added solutions by W. Luis Mochan. - Added solutions by Jorg Sommrey. - Added solutions by David Ferrone. - Added solutions by Andread Voegele. - Added solutions by E. Choroba. - Added solutions by Peter Campbell Smith. - Added solutions by Steven Wilson. - Added solutions by Avery Adams. - Added solutions by Lubos Kolouch. - Added solutions by Roger Bell_West. --- challenge-224/eric-cheung/python/ch-1.py | 17 +++++++ challenge-224/eric-cheung/python/ch-2.py | 87 ++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100755 challenge-224/eric-cheung/python/ch-1.py create mode 100755 challenge-224/eric-cheung/python/ch-2.py (limited to 'challenge-224/eric-cheung/python') diff --git a/challenge-224/eric-cheung/python/ch-1.py b/challenge-224/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..d094896b54 --- /dev/null +++ b/challenge-224/eric-cheung/python/ch-1.py @@ -0,0 +1,17 @@ + +## Example 1 +## strSource = "abc" +## strTarget = "xyz" + +## Example 2 +## strSource = "scriptinglanguage" +## strTarget = "perl" + +## Example 3 +strSource = "aabbcc" +strTarget = "abc" + +arrSrcSplit = [charLoop for charLoop in strSource] +arrTargetSplit = [charLoop for charLoop in strTarget] + +print (set(arrTargetSplit).issubset(set(arrSrcSplit))) diff --git a/challenge-224/eric-cheung/python/ch-2.py b/challenge-224/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..8f0bbc99ee --- /dev/null +++ b/challenge-224/eric-cheung/python/ch-2.py @@ -0,0 +1,87 @@ + +## Remarks +## https://www.geeksforgeeks.org/string-with-additive-sequence-set-2/ + +## python3 code to implement the approach + +## Variable to store the result +bResult = False +arrStr = [] + +## Function to check the additive sequence +def check_additive(strIn, subStrIn): + global bResult + global arrStr + + ## If the end is reached and vector consists of more than 2 numbers, then one of the possible solution is found + if (subStrIn == len(strIn) and len(arrStr) > 2): + + ## Mark the bResult as true to indicate the solution is found and to avoid for trying the rest of the combinations + bResult = True + return + + nInt_01, nInt_02, nInt_03 = 0, 0, 0 + + if (len(arrStr) >= 2): + ## Store the previous two numbers of the sequence to check the additive sequence property for the next number + nInt_02 = arrStr[len(arrStr) - 1] + nInt_01 = arrStr[len(arrStr) - 2] + + for nIndxLoop in range(subStrIn, len(strIn)): + + ## Generate the number + nInt_03 = nInt_03 * 10 + ord(strIn[nIndxLoop]) - ord("0") + + ## Try all the possible ways to generate the first two numbers + ## i.e. if vector consists of less than two numbers and no solution is found yet + if (len(arrStr) < 2): + arrStr.append(nInt_03) + check_additive(strIn, nIndxLoop + 1) + + ## Pop the value to try for the other combination + arrStr.pop() + + ## If the number generated so far is not equal the sum of previous two numbers in the sequence + ## then it cannot be a part of additive sequence hence no need to proceed further + + return + + if (nInt_03 != nInt_01 + nInt_02): + return + + ## If the number generated so far is equal to the sum of previous two numbers + ## then it can be a part of additive sequence + ## push it into vector and check for remaining string + + ## Store it in the vector + arrStr.append(nInt_03) + + ## Recur for remaining string + check_additive(strIn, nIndxLoop + 1) + + ## If unable to find solution + ## pop it and try for other combination + arrStr.pop() + + return + +## Function to check if additive sequence +def IsAdditiveSeq(strInputFunc): + global bResult + + ## In order to form additive sequence, the length of the string must be at least three + if (len(strInputFunc) <= 2): + return False + + bResult = False + check_additive(strInputFunc, 0) + return bResult + +## Driver Code +if __name__ == "__main__": + + ## strInput = "112358" ## Example 1 + ## strInput = "12345" ## Example 2 + strInput = "199100199" ## Example 3 + + print (IsAdditiveSeq(strInput)) -- cgit