aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-28 21:18:34 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-28 21:18:34 +0100
commit85dfd261a678406d6d22eb45168fd3fb4d93c3a3 (patch)
treef09f05415676c5b8e4973085bc25ebbdb86a3c0c
parent502be42efd777d4125a2dff0fbc73d3b10af3db4 (diff)
downloadperlweeklychallenge-club-85dfd261a678406d6d22eb45168fd3fb4d93c3a3.tar.gz
perlweeklychallenge-club-85dfd261a678406d6d22eb45168fd3fb4d93c3a3.tar.bz2
perlweeklychallenge-club-85dfd261a678406d6d22eb45168fd3fb4d93c3a3.zip
Add Python solution to challenge 160
-rw-r--r--challenge-160/paulo-custodio/perl/ch-1.pl4
-rw-r--r--challenge-160/paulo-custodio/perl/ch-2.pl6
-rw-r--r--challenge-160/paulo-custodio/python/ch-1.py44
-rw-r--r--challenge-160/paulo-custodio/python/ch-2.py40
4 files changed, 89 insertions, 5 deletions
diff --git a/challenge-160/paulo-custodio/perl/ch-1.pl b/challenge-160/paulo-custodio/perl/ch-1.pl
index 958713a46a..db13fb71c3 100644
--- a/challenge-160/paulo-custodio/perl/ch-1.pl
+++ b/challenge-160/paulo-custodio/perl/ch-1.pl
@@ -2,12 +2,12 @@
# Challenge 160
#
-# TASK #1 › Four Is Magic
+# TASK #1 > Four Is Magic
# Submitted by: Mohammad S Anwar
# You are given a positive number, $n < 10.
#
# Write a script to generate english text sequence starting with the English
-# cardinal representation of the given number, the word ‘is’ and then the
+# cardinal representation of the given number, the word 'is' and then the
# English cardinal representation of the count of characters that made up the
# first word, followed by a comma. Continue until you reach four.
#
diff --git a/challenge-160/paulo-custodio/perl/ch-2.pl b/challenge-160/paulo-custodio/perl/ch-2.pl
index 9036422a55..e72e3c27f5 100644
--- a/challenge-160/paulo-custodio/perl/ch-2.pl
+++ b/challenge-160/paulo-custodio/perl/ch-2.pl
@@ -2,15 +2,15 @@
# Challenge 160
#
-# TASK #2 › Equilibrium Index
+# TASK #2 > Equilibrium Index
# Submitted by: Mohammad S Anwar
# You are give an array of integers, @n.
#
# Write a script to find out the Equilibrium Index of the given array, if found.
#
# For an array A consisting n elements, index i is an equilibrium index if the
-# sum of elements of subarray A[0…i-1] is equal to the sum of elements of
-# subarray A[i+1…n-1].
+# sum of elements of subarray A[0...i-1] is equal to the sum of elements of
+# subarray A[i+1...n-1].
#
#
# Example 1:
diff --git a/challenge-160/paulo-custodio/python/ch-1.py b/challenge-160/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..1c0534868e
--- /dev/null
+++ b/challenge-160/paulo-custodio/python/ch-1.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+
+# Challenge 160
+#
+# TASK #1 > Four Is Magic
+# Submitted by: Mohammad S Anwar
+# You are given a positive number, $n < 10.
+#
+# Write a script to generate english text sequence starting with the English
+# cardinal representation of the given number, the word 'is' and then the
+# English cardinal representation of the count of characters that made up the
+# first word, followed by a comma. Continue until you reach four.
+#
+#
+# Example 1:
+# Input: $n = 5
+# Output: Five is four, four is magic.
+#
+# Example 2:
+# Input: $n = 7
+# Output: Seven is five, five is four, four is magic.
+#
+# Example 3:
+# Input: $n = 6
+# Output: Six is three, three is five, five is four, four is magic.
+
+import sys
+from num2words import num2words
+
+def sequence(n=1):
+ out = []
+ while n != 4:
+ num_en = num2words(n)
+ length = len(num_en)
+ len_en = num2words(length)
+
+ out.append(f"{num_en} is {len_en}")
+ n = length
+
+ out.append("four is magic.")
+ out[0] = out[0].capitalize()
+ return ", ".join(out)
+
+print(sequence(int(sys.argv[1])))
diff --git a/challenge-160/paulo-custodio/python/ch-2.py b/challenge-160/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..18b3916dc1
--- /dev/null
+++ b/challenge-160/paulo-custodio/python/ch-2.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+
+# Challenge 160
+#
+# TASK #2 > Equilibrium Index
+# Submitted by: Mohammad S Anwar
+# You are give an array of integers, @n.
+#
+# Write a script to find out the Equilibrium Index of the given array, if found.
+#
+# For an array A consisting n elements, index i is an equilibrium index if the
+# sum of elements of subarray A[0...i-1] is equal to the sum of elements of
+# subarray A[i+1...n-1].
+#
+#
+# Example 1:
+# Input: @n = (1, 3, 5, 7, 9)
+# Output: 3
+#
+# Example 2:
+# Input: @n = (1, 2, 3, 4, 5)
+# Output: -1 as no Equilibrium Index found.
+#
+# Example 3:
+# Input: @n = (2, 4, 2)
+# Output: 1
+
+import sys
+
+def equilibrium_index(n):
+ for i in range(1, len(n) - 1):
+ left = sum(n[0:i])
+ right = sum(n[i + 1:])
+ if left == right:
+ return i
+ if left > right:
+ return -1
+ return -1
+
+print(equilibrium_index(list(map(int, sys.argv[1:]))))