aboutsummaryrefslogtreecommitdiff
path: root/challenge-060
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-18 19:59:32 +0100
committerGitHub <noreply@github.com>2024-09-18 19:59:32 +0100
commit85073f31dae3decce780d1fd66cdcb79c82c7f65 (patch)
tree318d3bc02836f4cde215f7f15e9ec875c74a1490 /challenge-060
parentbfe139d559d498bcf1ae48cc08520bfc99ee9b85 (diff)
parent51c035e04c5ae5f9f02555206f57eb675a737617 (diff)
downloadperlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.gz
perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.bz2
perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.zip
Merge pull request #10860 from pauloscustodio/master
Add Python solutions
Diffstat (limited to 'challenge-060')
-rw-r--r--challenge-060/paulo-custodio/perl/ch-1.pl4
-rw-r--r--challenge-060/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-060/paulo-custodio/python/ch-1.py43
-rw-r--r--challenge-060/paulo-custodio/python/ch-2.py50
4 files changed, 96 insertions, 3 deletions
diff --git a/challenge-060/paulo-custodio/perl/ch-1.pl b/challenge-060/paulo-custodio/perl/ch-1.pl
index 6aac747b1a..aa47f1375b 100644
--- a/challenge-060/paulo-custodio/perl/ch-1.pl
+++ b/challenge-060/paulo-custodio/perl/ch-1.pl
@@ -2,13 +2,13 @@
# Challenge 060
#
-# TASK #1 › Excel Column
+# TASK #1 > Excel Column
# Reviewed by: Ryan Thompson
# Write a script that accepts a number and returns the Excel Column Name it
# represents and vice-versa.
#
# Excel columns start at A and increase lexicographically using the 26 letters
-# of the English alphabet, A..Z. After Z, the columns pick up an extra “digit”,
+# of the English alphabet, A..Z. After Z, the columns pick up an extra "digit",
# going from AA, AB, etc., which could (in theory) continue to an arbitrary
# number of digits. In practice, Excel sheets are limited to 16,384 columns.
#
diff --git a/challenge-060/paulo-custodio/perl/ch-2.pl b/challenge-060/paulo-custodio/perl/ch-2.pl
index 6ec49647e7..ba89e73e05 100644
--- a/challenge-060/paulo-custodio/perl/ch-2.pl
+++ b/challenge-060/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 060
#
-# TASK #2 › Find Numbers
+# TASK #2 > Find Numbers
# Reviewed by: Ryan Thompson
# Write a script that accepts list of positive numbers (@L) and two positive
# numbers $X and $Y.
diff --git a/challenge-060/paulo-custodio/python/ch-1.py b/challenge-060/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..10efd3a1fc
--- /dev/null
+++ b/challenge-060/paulo-custodio/python/ch-1.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+# Challenge 060
+#
+# TASK #1 > Excel Column
+# Reviewed by: Ryan Thompson
+# Write a script that accepts a number and returns the Excel Column Name it
+# represents and vice-versa.
+#
+# Excel columns start at A and increase lexicographically using the 26 letters
+# of the English alphabet, A..Z. After Z, the columns pick up an extra "digit",
+# going from AA, AB, etc., which could (in theory) continue to an arbitrary
+# number of digits. In practice, Excel sheets are limited to 16,384 columns.
+#
+# Example
+# Input Number: 28
+# Output: AB
+#
+# Input Column Name: AD
+# Output: 30
+
+import re
+import sys
+
+def col2num(col):
+ num = 0
+ for digit in col:
+ num = 26*num + ord(digit)-ord('A') + 1
+ return num
+
+def num2col(num):
+ col = ""
+ while num > 0:
+ digit = (num-1) % 26
+ num = int((num-1) / 26)
+ col = chr(ord('A')+digit) + col
+ return col
+
+arg = sys.argv[1]
+if re.search(r'^\d+$', arg):
+ print(num2col(int(arg)))
+else:
+ print(col2num(arg))
diff --git a/challenge-060/paulo-custodio/python/ch-2.py b/challenge-060/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..5a159ce266
--- /dev/null
+++ b/challenge-060/paulo-custodio/python/ch-2.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+
+# Challenge 060
+#
+# TASK #2 > Find Numbers
+# Reviewed by: Ryan Thompson
+# Write a script that accepts list of positive numbers (@L) and two positive
+# numbers $X and $Y.
+#
+# The script should print all possible numbers made by concatenating the numbers
+# from @L, whose length is exactly $X but value is less than $Y.
+#
+# Example
+# Input:
+#
+# @L = (0, 1, 2, 5);
+# $X = 2;
+# $Y = 21;
+# Output:
+#
+# 10, 11, 12, 15, 20
+
+import sys
+
+def combine1(combin, prefix, n, digits):
+ if len(prefix) == n:
+ num = int(prefix)
+ if not num in combin:
+ combin.add(num)
+ else:
+ for digit in digits:
+ combine1(combin, prefix+digit, n, digits)
+
+def combine(digits):
+ combin = set()
+ for n in range(1, len(digits)+1):
+ combine1(combin, "", n, digits)
+ nums = sorted(list(combin))
+ return nums
+
+def numbers(X, Y, L):
+ nums = combine(L)
+ nums = list(filter(lambda x:len(str(x))==X and x<Y, nums))
+ return nums
+
+X = int(sys.argv[1])
+Y = int(sys.argv[2])
+L = sys.argv[3:]
+nums = numbers(X, Y, L)
+print(", ".join(map(str, nums)))