diff options
Diffstat (limited to 'challenge-245/jeanluc2020/python')
| -rwxr-xr-x | challenge-245/jeanluc2020/python/ch-1.py | 42 | ||||
| -rwxr-xr-x | challenge-245/jeanluc2020/python/ch-2.py | 74 |
2 files changed, 116 insertions, 0 deletions
diff --git a/challenge-245/jeanluc2020/python/ch-1.py b/challenge-245/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..98da163a99 --- /dev/null +++ b/challenge-245/jeanluc2020/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/#TASK1 +# +# Task 1: Sort Language +# ===================== +# +# You are given two array of languages and its popularity. +# +# Write a script to sort the language based on popularity. +# Example 1 +# +# Input: @lang = ('perl', 'c', 'python') +# @popularity = (2, 1, 3) +# Output: ('c', 'perl', 'python') +# +# Example 2 +# +# Input: @lang = ('c++', 'haskell', 'java') +# @popularity = (1, 3, 2) +# Output: ('c++', 'java', 'haskell') +# +############################################################ +## +## discussion +## +############################################################ +# +# The solution that jumps right into my face is to fill the data +# into a hash table, using lang as the key and popularity as the +# value. Then sort the keys by their values and put the result into +# the output. + +def sort_language(lang: list, popularity: list): + print("Input: (", ", ".join(str(x) for x in lang), "), (", ", ".join(str(x) for x in popularity), ")") + hash = {} + for i in range(len(lang)): + hash[lang[i]] = popularity[i] + print("Output: (", ", ".join(str(x) for x in sorted(hash, key=hash.get)), ")") + +sort_language( ['perl', 'c', 'python'], [2, 1, 3] ); +sort_language( ['c++', 'haskell', 'java'], [1, 3, 2] ); + diff --git a/challenge-245/jeanluc2020/python/ch-2.py b/challenge-245/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..5b11711684 --- /dev/null +++ b/challenge-245/jeanluc2020/python/ch-2.py @@ -0,0 +1,74 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/#TASK2 +# +# 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. +# +## Example 1 +## +## Input: @ints = (8, 1, 9) +## Output: 981 +## +## 981 % 3 == 0 +# +## Example 2 +## +## Input: @ints = (8, 6, 7, 1, 0) +## Output: 8760 +# +## Example 3 +## +## Input: @ints = (1) +## Output: -1 +# +############################################################ +## +## discussion +## +############################################################ +# +# While all examples in the description use single-digit numbers, +# there is nothing that would require this. So in order to catch all +# solutions, we need all possible permutations of all subsets of the +# array and of the numbers created out of those we need the biggest +# one that is divisible by 3. + +from itertools import chain, combinations, permutations + +def powerset(iterable): + "powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" + s = list(iterable) + return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) + +def largest_of_three(ints: list): + print("Input: (", ", ".join(str(x) for x in ints), ")") + result = int() + for myset in powerset(ints): + for c in permutations(myset, len(myset)): + v = str("".join(str(x) for x in c)) + # print("-> ", v) + if len(v) > 0: + # print("Considering", v, "as a value") + value = int(v) + if value % 3 == 0: + if len(str(result)) == 0: + result = value + if value > result: + result = value + if len(str(result)) == 0: + print("Output: -1") + else: + print("Output:", str(result)) + +largest_of_three([8, 1, 9]); +largest_of_three([8, 6, 7, 1, 0]); +largest_of_three([1]); +largest_of_three([8, 60, 7]); +largest_of_three([80, 6, 7]); + |
