aboutsummaryrefslogtreecommitdiff
path: root/challenge-159/eric-cheung/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-159/eric-cheung/python')
-rwxr-xr-xchallenge-159/eric-cheung/python/ch-1.py71
-rwxr-xr-xchallenge-159/eric-cheung/python/ch-2.py41
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-159/eric-cheung/python/ch-1.py b/challenge-159/eric-cheung/python/ch-1.py
new file mode 100755
index 0000000000..82de733403
--- /dev/null
+++ b/challenge-159/eric-cheung/python/ch-1.py
@@ -0,0 +1,71 @@
+## Remarks
+## https://rosettacode.org/wiki/Four_is_magic#Python
+
+from collections import OrderedDict
+
+numbers = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}
+
+numbers = OrderedDict(sorted(numbers.items(), key = lambda t: t[0], reverse = True))
+
+def string_representation(i: int) -> str:
+ """
+ Return the english string representation of an integer
+ """
+ if i == 0:
+ return "zero"
+
+ words = ['negative'] if i < 0 else []
+
+ working_copy = abs(i)
+
+ for key, value in numbers.items():
+ if key <= working_copy:
+ times = int(working_copy / key)
+
+ if key >= 100:
+ words.append(string_representation(times))
+
+ words.append(value)
+ working_copy -= times * key
+
+ if working_copy == 0:
+ break
+
+ return " ".join(words)
+
+
+def next_phrase(i: int):
+ """
+ Generate all the phrases
+ """
+ while not i == 4: # Generate phrases until four is reached
+ str_i = string_representation(i)
+ len_i = len(str_i)
+
+ yield str_i, "is", string_representation(len_i)
+
+ i = len_i
+
+ ## The Last Phrase
+ yield string_representation(i), "is", "magic"
+
+
+def magic(i: int) -> str:
+ phrases = []
+
+ for phrase in next_phrase(i):
+ phrases.append(" ".join(phrase))
+
+ return f'{", ".join(phrases)}.'.capitalize()
+
+
+## Driver Code
+
+## nInput = 5 ## Example 1:
+## nInput = 7 ## Example 2:
+nInput = 6 ## Example 3:
+
+strMsg = magic(nInput)
+
+print (strMsg)
+
diff --git a/challenge-159/eric-cheung/python/ch-2.py b/challenge-159/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..220bf69b97
--- /dev/null
+++ b/challenge-159/eric-cheung/python/ch-2.py
@@ -0,0 +1,41 @@
+## Remarks
+## https://www.geeksforgeeks.org/equilibrium-index-of-an-array/
+
+## Python Program to Find Equilibrium Index of an Array
+
+## Function to Find the Equilibrium Index
+def equilibrium(arr):
+ leftsum = 0
+ rightsum = 0
+
+ n = len(arr)
+
+ ## Check for Indexes one by one until an equilibrium index is found
+ for i in range(n):
+ leftsum = 0
+ rightsum = 0
+
+ ## Get Left Sum
+ for j in range(i):
+ leftsum = leftsum + arr[j]
+
+ ## Get Right Sum
+ for j in range(i + 1, n):
+ rightsum = rightsum + arr[j]
+
+ ## If Left Sum and Right Sum are same, then we are done
+ if leftsum == rightsum:
+ return i
+
+ ## return -1 if no equilibrium index is found
+ return -1
+
+
+## Driver Code
+arr = [1, 3, 5, 7, 9] ## Example 1:
+## arr = [1, 2, 3, 4, 5] ## Example 2:
+## arr = [2, 4, 2] ## Example 3:
+
+print (equilibrium(arr))
+
+## This code is contributed by Abhishek Sharama