aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-245/ianrifkin/README.md43
-rw-r--r--challenge-245/ianrifkin/python/ch-1.py32
-rw-r--r--challenge-245/ianrifkin/python/ch-2.py69
3 files changed, 144 insertions, 0 deletions
diff --git a/challenge-245/ianrifkin/README.md b/challenge-245/ianrifkin/README.md
index 86cce400cb..9c60e93a61 100644
--- a/challenge-245/ianrifkin/README.md
+++ b/challenge-245/ianrifkin/README.md
@@ -33,6 +33,18 @@ With that I return an array of the languages in sort order using the inputted la
@lang[@pop_sort_idx];
```
+I also solved this task only slightly differently with Python. I first created a list combining the `popularity` and `lang` lists using `zip`:
+```
+sorted_langs_with_pop = sorted(zip(popularity,lang))
+```
+
+I then use that to create the desired output of just the languages in the sort order:
+```
+[lang for _,lang in sorted_langs_with_pop]
+```
+
+That could have been in one line but I thought it was clearer in the two distinct steps.
+
## Task 2: Largest of Three
```
@@ -85,5 +97,36 @@ foreach my $num_2_try (sort { $b <=> $a } @numbers_to_try) {
The above loop will return the first number (the largest number) divisible by 3. If it doesn't find one then after this loop there is a `return -1` so that the default behavoir matches the requirements.
+
+Again with this task I wanted to see how to solve it using Python. It is a fairly similar approach but I of course don't have the Perl modules available; instead I used `itertools:permutations` which is native to Python.
+
+`itertools:permutations` can have a length to its output but I don't see a way for it to provide a dynamic length like we need. Instead I loop through the list length so that I can run the permutations for each length:
+```
+for i in range(len(nums)):
+ num_of_digits = i+1
+ perms = list(permutations(nums, num_of_digits))
+```
+
+That creates a list of tuples, but I need the tuples to be combined into actual numbers to try:
+```
+ for group_of_digits in perms:
+ combined_digits = int("".join(map(str, group_of_digits)))
+ numbers_to_try.append(combined_digits)
+```
+
+Now I have an unsorted list of numbers to try, so next comes the sorting:
+```
+numbers_to_try.sort(reverse=True)
+```
+
+The final step, like in the Perl solution, is to return the first (largest) number divisible by 3, otherwise return -1:
+```
+for num2try in numbers_to_try:
+ if num2try % 3 == 0:
+ return num2try
+return -1
+```
+
+
---
The full code with comments is available at https://github.com/manwar/perlweeklychallenge-club/tree/master/challenge-245/ianrifkin
diff --git a/challenge-245/ianrifkin/python/ch-1.py b/challenge-245/ianrifkin/python/ch-1.py
new file mode 100644
index 0000000000..54d125e553
--- /dev/null
+++ b/challenge-245/ianrifkin/python/ch-1.py
@@ -0,0 +1,32 @@
+#!/usr/local/bin/python3
+import sys
+
+# Task 1: Sort Language
+
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-245/#TASK1 for more information on this challenge
+
+# You are given two array of languages and its popularity.
+# Write a script to sort the language based on popularity.
+
+def main(argv):
+ # Example 1
+ lang = ('perl', 'c', 'python')
+ popularity = (2, 1, 3)
+ print( sort_languages(lang, popularity) )
+ #Output: ('c', 'perl', 'python')
+
+ # Example 2
+ lang = ('c++', 'haskell', 'java')
+ popularity = (1, 3, 2)
+ print( sort_languages(lang, popularity) )
+ #Output: ('c++', 'java', 'haskell')
+
+def sort_languages(lang,popularity):
+ # Create a single list based on the two inputted lists
+ sorted_langs_with_pop = sorted(zip(popularity,lang))
+ # Created a sorted list using the above list but only with the languages
+ sorted_langs = [lang for _,lang in sorted_langs_with_pop]
+ return sorted_langs
+
+if __name__ == "__main__":
+ main(sys.argv[1:])
diff --git a/challenge-245/ianrifkin/python/ch-2.py b/challenge-245/ianrifkin/python/ch-2.py
new file mode 100644
index 0000000000..8af7ea3f1e
--- /dev/null
+++ b/challenge-245/ianrifkin/python/ch-2.py
@@ -0,0 +1,69 @@
+#!/usr/local/bin/python3
+import sys, argparse
+import math
+from itertools import permutations
+
+# Task 2: Largest of Three
+
+# You are given an array of integers >= 0.
+
+# Write a script to return the largest number formed by concatenating some of the given integers in any order which is also multiple of 3. Return -1 if none found.
+
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-245/#TASK2 for more information on this challenge
+
+def main(argv):
+ argParser = argparse.ArgumentParser()
+ argParser.add_argument("-n", "--nums", nargs='+', type=int, help="space seperated list of positive integers e.g. -n 10 30 4 5")
+ args = argParser.parse_args()
+
+ if args.nums:
+ nums = args.nums
+ print( largest(nums) )
+ else:
+ # Example 1
+ nums = [8, 1, 9]
+ print( largest(nums) )
+ #Output: 981
+
+ # Example 2
+ nums = [8, 6, 7, 1, 0]
+ print( largest(nums) )
+ #Output: 8760
+
+ # Example 3
+ nums = [1]
+ print( largest(nums) )
+ #Output: -1
+
+
+def largest(nums):
+ numbers_to_try = []
+ for i in range(len(nums)):
+ num_of_digits = i+1
+ perms = list(permutations(nums, num_of_digits))
+
+ for group_of_digits in perms:
+ combined_digits = int("".join(map(str, group_of_digits)))
+ numbers_to_try.append(combined_digits)
+
+ numbers_to_try.sort(reverse=True)
+
+ for num2try in numbers_to_try:
+ if num2try % 3 == 0:
+ return num2try
+ return -1
+
+
+ # # Use array of potential numbers in numerical descending sort order
+ # # to determine if any are divisible by 3
+ # foreach my $num_2_try (sort { $b <=> $a } @numbers_to_try) {
+ # # return the first (biggest) number found
+ # return $num_2_try unless $num_2_try % 3;
+ # }
+ # return -1 #default return value of -1 if no number found
+
+
+
+
+if __name__ == "__main__":
+ main(sys.argv[1:])