aboutsummaryrefslogtreecommitdiff
path: root/challenge-060/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2024-09-18 19:56:42 -0400
committerDave Jacoby <jacoby.david@gmail.com>2024-09-18 19:56:42 -0400
commitf86f5e2fec16020c1d86f9028fb0f61cfeac106e (patch)
tree0fd388a696b51ffde5a7bfe8519a74e1caf42461 /challenge-060/paulo-custodio/python/ch-2.py
parentff8719c86653d5ad3121955e9494a0010527c2b9 (diff)
parent0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05 (diff)
downloadperlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.gz
perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.bz2
perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-060/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-060/paulo-custodio/python/ch-2.py50
1 files changed, 50 insertions, 0 deletions
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)))