diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-12-01 07:45:12 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-01 07:45:12 +0000 |
| commit | 1cecd3f4f216beb3dc1dd42aaec78ca8c19b7a2c (patch) | |
| tree | 1b246a3fd2b173c3f55bc6deae38115567b0d1a4 /challenge-245/ianrifkin/python | |
| parent | acb8d73e92d94f1bd8d98d77450018ffc3c15e40 (diff) | |
| parent | 9a35be85626dccbd46b106c4a40be8e762b4173f (diff) | |
| download | perlweeklychallenge-club-1cecd3f4f216beb3dc1dd42aaec78ca8c19b7a2c.tar.gz perlweeklychallenge-club-1cecd3f4f216beb3dc1dd42aaec78ca8c19b7a2c.tar.bz2 perlweeklychallenge-club-1cecd3f4f216beb3dc1dd42aaec78ca8c19b7a2c.zip | |
Merge pull request #9169 from ianrifkin/ianrifkin-challenge-245
Ianrifkin challenge 245
Diffstat (limited to 'challenge-245/ianrifkin/python')
| -rw-r--r-- | challenge-245/ianrifkin/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-245/ianrifkin/python/ch-2.py | 69 |
2 files changed, 101 insertions, 0 deletions
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:]) |
