diff options
| -rw-r--r-- | challenge-245/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-245/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-245/jeanluc2020/perl/ch-1.pl | 50 | ||||
| -rwxr-xr-x | challenge-245/jeanluc2020/perl/ch-2.pl | 74 | ||||
| -rwxr-xr-x | challenge-245/jeanluc2020/python/ch-1.py | 42 | ||||
| -rwxr-xr-x | challenge-245/jeanluc2020/python/ch-2.py | 74 |
6 files changed, 242 insertions, 0 deletions
diff --git a/challenge-245/jeanluc2020/blog-1.txt b/challenge-245/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..3f87b3fcdb --- /dev/null +++ b/challenge-245/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-245-1.html diff --git a/challenge-245/jeanluc2020/blog-2.txt b/challenge-245/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..ae1707d73e --- /dev/null +++ b/challenge-245/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-245-2.html diff --git a/challenge-245/jeanluc2020/perl/ch-1.pl b/challenge-245/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..55400d0010 --- /dev/null +++ b/challenge-245/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +# 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. + +use strict; +use warnings; + +sort_language( ['perl', 'c', 'python'], [2, 1, 3] ); +sort_language( ['c++', 'haskell', 'java'], [1, 3, 2] ); + +sub sort_language { + my ($lang, $popularity) = @_; + print "Input: (" . join(", ", @$lang) . ")\n (" . join(", ", @$popularity) . ")\n"; + my $hash = {}; + my $index = 0; + foreach my $elem (@$lang) { + $hash->{$elem} = $popularity->[$index]; + $index++; + } + print "Output: (" . join(", ", sort { $hash->{$a} <=> $hash->{$b} } keys %$hash) . ")\n"; +} + diff --git a/challenge-245/jeanluc2020/perl/ch-2.pl b/challenge-245/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..f35a03c03f --- /dev/null +++ b/challenge-245/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/perl +# 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. + +use strict; +use warnings; +use Algorithm::Combinatorics qw(permutations subsets); + +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); + +sub largest_of_three { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $result; + my $iter = subsets(\@ints); + while(my $s = $iter->next) { + next unless @$s; + my $iter2 = permutations($s); + while(my $p = $iter2->next) { + my $num = join("", @$p); + next unless $num; + next unless $num % 3 == 0; + $result = $num unless defined $result; + $result = $num if $num > $result; + } + } + if(defined $result) { + print "Output: $result\n"; + } else { + print "Output: -1\n"; + } +} + 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]); + |
