diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2023-04-11 21:10:15 +0200 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2023-04-11 21:10:15 +0200 |
| commit | b491511604b2c3d433677cd26eb31c1fbd45eb2e (patch) | |
| tree | 177259239c2834b9c9b523583de163553aa00336 | |
| parent | 0040b8b93d96c422f23486eef3f731019a289845 (diff) | |
| download | perlweeklychallenge-club-b491511604b2c3d433677cd26eb31c1fbd45eb2e.tar.gz perlweeklychallenge-club-b491511604b2c3d433677cd26eb31c1fbd45eb2e.tar.bz2 perlweeklychallenge-club-b491511604b2c3d433677cd26eb31c1fbd45eb2e.zip | |
Add solution 212.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
| -rw-r--r-- | challenge-212/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-212/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-212/jeanluc2020/perl/ch-1.pl | 80 | ||||
| -rwxr-xr-x | challenge-212/jeanluc2020/perl/ch-2.pl | 103 |
4 files changed, 185 insertions, 0 deletions
diff --git a/challenge-212/jeanluc2020/blog-1.txt b/challenge-212/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..b3e613f2dc --- /dev/null +++ b/challenge-212/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-212-1.html diff --git a/challenge-212/jeanluc2020/blog-2.txt b/challenge-212/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..012b5ba19f --- /dev/null +++ b/challenge-212/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-212-2.html diff --git a/challenge-212/jeanluc2020/perl/ch-1.pl b/challenge-212/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..fa0caf364d --- /dev/null +++ b/challenge-212/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-212/#TASK1 +# +# Task 1: Jumping Letters +# ======================= +# +# You are given a word having alphabetic characters only, and a list of +# positive integers of the same length +# +# Write a script to print the new word generated after jumping forward each +# letter in the given word by the integer in the list. The given list would +# have exactly the number as the total alphabets in the given word. +# +## Example 1 +## +## Input: $word = 'Perl' and @jump = (2,22,19,9) +## Output: Raku +## +## 'P' jumps 2 place forward and becomes 'R'. +## 'e' jumps 22 place forward and becomes 'a'. (jump is cyclic i.e. after 'z' you go back to 'a') +## 'r' jumps 19 place forward and becomes 'k'. +## 'l' jumps 9 place forward and becomes 'u'. +# +## Example 2 +## +## Input: $word = 'Raku' and @jump = (24,4,7,17) +## Output: 'Perl' +# +############################################################ +## +## discussion +## +############################################################ +# +# Going character by character, apply the jump. Just check if we +# are in a lower- or uppercase character first to jump in the +# right space. + +use strict; +use warnings; + +jumping_letters("Perl", [2,22,19,9]); +jumping_letters("Raku", [24,4,7,17]); + +sub jumping_letters { + my ($word, $jump) = @_; + my @chars = split //, $word; + my $result; + print "Input: $word - [" . join(",", @$jump) . "]\n"; + foreach my $i (0..$#chars) { + $result .= jumping_letter($chars[$i], $jump->[$i]); + } + print "Output: $result\n"; +} + +# jump a single letter +sub jumping_letter { + my ($chr, $jump) = @_; + my $o = ord($chr); + if(65 <= $o && $o <= 90) { + # uppercase letter + $o += $jump || 0; + # we need to wrap around since we + # jumped past "Z" + if($o > 90) { + $o -= 26; + } + return chr($o); + } else { + # lowercase character + $o += $jump || 0; + # we need to wrap around since we + # jumped past "z" + if($o > 122) { + $o -= 26; + } + return chr($o); + } +} + diff --git a/challenge-212/jeanluc2020/perl/ch-2.pl b/challenge-212/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..3aabdad661 --- /dev/null +++ b/challenge-212/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,103 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-212/#TASK2 +# +# Task 2: Rearrange Groups +# ======================== +# +# You are given a list of integers and group size greater than zero. +# +# Write a script to split the list into equal groups of the given size where +# integers are in sequential order. If it can’t be done then print -1. +# +## Example 1: +## +## Input: @list = (1,2,3,5,1,2,7,6,3) and $size = 3 +## Output: (1,2,3), (1,2,3), (5,6,7) +# +## Example 2: +## +## Input: @list = (1,2,3) and $size = 2 +## Output: -1 +# +## Example 3: +## +## Input: @list = (1,2,4,3,5,3) and $size = 3 +## Output: (1,2,3), (3,4,5) +# +## Example 4: +## +## Input: @list = (1,5,2,6,4,7) and $size = 3 +## Output: -1 +# +############################################################ +## +## discussion +## +############################################################ +# +# We can basically sort the elements of the array, then always try +# to (group size) elements from that array starting at the smallest +# element. If we can always do that, we return those results. If we +# can't in any point, we can return -1. +# In order to make this easier, we can count each element and keep the +# count in a hash table the keys of which equate the elements from the +# original list and the value is the count of this element in that +# list. + +use strict; +use warnings; +use List::Util qw(min); + +rearrange_groups([1,2,3,5,1,2,7,6,3], 3); +rearrange_groups([1,2,3], 2); +rearrange_groups([1,2,4,3,5,3], 3); +rearrange_groups([1,5,2,6,4,7], 3); +rearrange_groups([1,5,2,6,4,7], 2); + +sub rearrange_groups { + my ($list, $size) = @_; + print "Input: (" . join(",", @$list) . "); $size\n"; + my $data; + # count all elements into the hash %$data + foreach my $elem (@$list) { + $data->{$elem}++; + } + # start with the minimum key + my $min = min(keys(%$data)); + my @result = (); + # as long as there is still some data + while(defined($min)) { + my @tmp = (); + # find up to "size" sequential elements + foreach my $cur (0..$size-1) { + if($data->{$min+$cur}) { + # if there is such an element, add it to our + # current temporary sub-result, decrease the + # counter in this hash element, and remove + # the element from the hash altogether if it + # was the last for this key. + push @tmp, ($min+$cur); + $data->{$min+$cur}--; + delete $data->{$min+$cur} unless $data->{$min+$cur}; + } else { + # not enough sequential elements found, we're done here + print "Output: -1\n"; + return; + } + } + # OK, we found one result set to put on our result, then + # we can continue with the new minimum key in the hash + push @result, [ @tmp ]; + $min = min(keys(%$data)); + } + # Let's output the result that we found + print "Output: "; + my $first = 1; + foreach my $arr (@result) { + print ", " unless $first; + print "(" . join(",", @$arr) . ")"; + $first = 0; + } + print "\n"; +} + |
