diff options
| -rw-r--r-- | challenge-260/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-260/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-260/jeanluc2020/perl/ch-1.pl | 66 | ||||
| -rwxr-xr-x | challenge-260/jeanluc2020/perl/ch-2.pl | 80 | ||||
| -rwxr-xr-x | challenge-260/jeanluc2020/python/ch-1.py | 63 | ||||
| -rwxr-xr-x | challenge-260/jeanluc2020/python/ch-2.py | 74 |
6 files changed, 285 insertions, 0 deletions
diff --git a/challenge-260/jeanluc2020/blog-1.txt b/challenge-260/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..7444890fe3 --- /dev/null +++ b/challenge-260/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-260-1.html diff --git a/challenge-260/jeanluc2020/blog-2.txt b/challenge-260/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..b3832e47f6 --- /dev/null +++ b/challenge-260/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-260-2.html diff --git a/challenge-260/jeanluc2020/perl/ch-1.pl b/challenge-260/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..a369814766 --- /dev/null +++ b/challenge-260/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/#TASK1 +# +# Task 1: Unique Occurrences +# ========================== +# +# You are given an array of integers, @ints. +# +# Write a script to return 1 if the number of occurrences of each value in the +# given array is unique or 0 otherwise. +# +## Example 1 +## +## Input: @ints = (1,2,2,1,1,3) +## Output: 1 +## +## The number 1 occurred 3 times. +## The number 2 occurred 2 times. +## The number 3 occurred 1 time. +## +## All occurrences are unique, therefore the output is 1. +# +## Example 2 +## +## Input: @ints = (1,2,3) +## Output: 0 +# +## Example 3 +## +## Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9) +## Output: 1 +# +############################################################ +## +## discussion +## +############################################################ +# +# First, we calculate how often each value occurs in @ints and +# store that information in a $map. Then we check if all values +# in $map are unique: if we find one that isn't, we can set +# $unique to 0. + +use strict; +use warnings; +use Data::Dumper; + +unique_occurrences(1,2,2,1,1,3); +unique_occurrences(1,2,3); +unique_occurrences(-2,0,1,-2,1,1,0,1,-2,9); + +sub unique_occurrences { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $map = {}; + map { $map->{$_}++ } @ints; + my $seen = {}; + my $unique = 1; + foreach my $key (keys %$map) { + if($seen->{$map->{$key}}) { + $unique = 0; + } + $seen->{$map->{$key}}++; + } + print "Output: $unique\n"; +} diff --git a/challenge-260/jeanluc2020/perl/ch-2.pl b/challenge-260/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..2664bb558d --- /dev/null +++ b/challenge-260/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,80 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/#TASK2 +# +# Task 2: Dictionary Rank +# ======================= +# +# You are given a word, $word. +# +# Write a script to compute the dictionary rank of the given word. +# +## Example 1 +## +## Input: $word = 'CAT' +## Output: 3 +## +## All possible combinations of the letters: +## CAT, CTA, ATC, TCA, ACT, TAC +## +## Arrange them in alphabetical order: +## ACT, ATC, CAT, CTA, TAC, TCA +## +## CAT is the 3rd in the list. +## Therefore the dictionary rank of CAT is 3. +# +## Example 2 +## +## Input: $word = 'GOOGLE' +## Output: 88 +# +## Example 3 +## +## Input: $word = 'SECRET' +## Output: 255 +# +############################################################ +## +## discussion +## +############################################################ +# +# We first create all possible permutations, for which we use an +# iterator-based solution from Algorithm::Permute. Then we keep +# all permutations, removing duplicates (Algorithm::Permute will +# for example generate the word "GOOGLE" twice, once for each "O" +# in the first and the second of two possible positions). Then we +# just walk the sorted list, counting the index while walking, and +# returning 1+index once we found the original word. + +use strict; +use warnings; +use Algorithm::Permute; + +dictionary_rank('CAT'); +dictionary_rank('GOOGLE'); +dictionary_rank('SECRET'); + +sub dictionary_rank { + my $word = shift; + print "Input: '$word'\n"; + my @chars = split //, $word; + my @permutations = (); + my $p_iterator = Algorithm::Permute->new ( \@chars ); + my $seen = {}; + while(my @perm = $p_iterator->next) { + my $w = join("", @perm); + push @permutations, $w unless $seen->{$w}; + $seen->{$w} = 1; + } + my @sorted = sort @permutations; + print join(", ", @sorted) . "\n"; + my $i = 0; + while($i <= $#sorted) { + if($sorted[$i] eq $word) { + $i++; + print "Output: $i\n"; + last; + } + $i++; + } +} diff --git a/challenge-260/jeanluc2020/python/ch-1.py b/challenge-260/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..02fcf8cce6 --- /dev/null +++ b/challenge-260/jeanluc2020/python/ch-1.py @@ -0,0 +1,63 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/#TASK1 +# +# Task 1: Unique Occurrences +# ========================== +# +# You are given an array of integers, @ints. +# +# Write a script to return 1 if the number of occurrences of each value in the +# given array is unique or 0 otherwise. +# +## Example 1 +## +## Input: @ints = (1,2,2,1,1,3) +## Output: 1 +## +## The number 1 occurred 3 times. +## The number 2 occurred 2 times. +## The number 3 occurred 1 time. +## +## All occurrences are unique, therefore the output is 1. +# +## Example 2 +## +## Input: @ints = (1,2,3) +## Output: 0 +# +## Example 3 +## +## Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9) +## Output: 1 +# +############################################################ +## +## discussion +## +############################################################ +# +# First, we calculate how often each value occurs in @ints and +# store that information in a $map. Then we check if all values +# in $map are unique: if we find one that isn't, we can set +# $unique to 0. + +def unique_occurrences(ints: list) -> None: + print("Input: (", ", ".join(str(x) for x in ints), ")", sep="") + map: dict = {} + for i in ints: + if i in map: + map[i] += 1 + else: + map[i] = 1 + seen: dict = {} + unique = 1 + for key in map: + if map[key] in seen: + unique = 0 + seen[map[key]] = 1 + print(f"Output: {unique}") + +unique_occurrences([1,2,2,1,1,3]); +unique_occurrences([1,2,3]); +unique_occurrences([-2,0,1,-2,1,1,0,1,-2,9]); + diff --git a/challenge-260/jeanluc2020/python/ch-2.py b/challenge-260/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..f22d88c044 --- /dev/null +++ b/challenge-260/jeanluc2020/python/ch-2.py @@ -0,0 +1,74 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/#TASK2 +# +# Task 2: Dictionary Rank +# ======================= +# +# You are given a word, $word. +# +# Write a script to compute the dictionary rank of the given word. +# +## Example 1 +## +## Input: $word = 'CAT' +## Output: 3 +## +## All possible combinations of the letters: +## CAT, CTA, ATC, TCA, ACT, TAC +## +## Arrange them in alphabetical order: +## ACT, ATC, CAT, CTA, TAC, TCA +## +## CAT is the 3rd in the list. +## Therefore the dictionary rank of CAT is 3. +# +## Example 2 +## +## Input: $word = 'GOOGLE' +## Output: 88 +# +## Example 3 +## +## Input: $word = 'SECRET' +## Output: 255 +# +############################################################ +## +## discussion +## +############################################################ +# +# We first create all possible permutations, for which we use an +# iterator-based solution from Algorithm::Permute. Then we keep +# all permutations, removing duplicates (Algorithm::Permute will +# for example generate the word "GOOGLE" twice, once for each "O" +# in the first and the second of two possible positions). Then we +# just walk the sorted list, counting the index while walking, and +# returning 1+index once we found the original word. + +import itertools + +def dictionary_rank (word: str) -> None: + chars = list(word) + print(f"Input: {word}") + permutations = list(itertools.permutations(chars)) + unique_permutations: list = [] + seen: dict = {} + for perm in permutations: + w = "".join(perm) + if w not in seen: + unique_permutations.append(w) + seen[w] = 1 + sorted_array = sorted(unique_permutations) + i=0 + while i < len(sorted_array): + if sorted_array[i] == word: + i += 1 + print(f"Output: {i}") + return + i += 1 + +dictionary_rank('CAT'); +dictionary_rank('GOOGLE'); +dictionary_rank('SECRET'); + |
