diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2024-07-22 21:26:52 +0200 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2024-07-22 21:26:52 +0200 |
| commit | 5cefaca8dbfbe20aa97cf844335e26fad0e087ac (patch) | |
| tree | 070171d81b23a0551aab2d7d4230bbbcf7df4c9d /challenge-279 | |
| parent | 83cffa5c970b2778c48d6b14aa6788dc4062359e (diff) | |
| download | perlweeklychallenge-club-5cefaca8dbfbe20aa97cf844335e26fad0e087ac.tar.gz perlweeklychallenge-club-5cefaca8dbfbe20aa97cf844335e26fad0e087ac.tar.bz2 perlweeklychallenge-club-5cefaca8dbfbe20aa97cf844335e26fad0e087ac.zip | |
Add solution 279.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
Diffstat (limited to 'challenge-279')
| -rw-r--r-- | challenge-279/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-279/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-279/jeanluc2020/perl/ch-1.pl | 58 | ||||
| -rwxr-xr-x | challenge-279/jeanluc2020/perl/ch-2.pl | 69 |
4 files changed, 129 insertions, 0 deletions
diff --git a/challenge-279/jeanluc2020/blog-1.txt b/challenge-279/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..508a87d6f0 --- /dev/null +++ b/challenge-279/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-279-1.html diff --git a/challenge-279/jeanluc2020/blog-2.txt b/challenge-279/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..b76fc0356d --- /dev/null +++ b/challenge-279/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-279-2.html diff --git a/challenge-279/jeanluc2020/perl/ch-1.pl b/challenge-279/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..861dbd26a1 --- /dev/null +++ b/challenge-279/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-279/#TASK1 +# +# Task 1: Sort Letters +# ==================== +# +# You are given two arrays, @letters and @weights. +# +# Write a script to sort the given array @letters based on the @weights. +# +## Example 1 +## +## Input: @letters = ('R', 'E', 'P', 'L') +## @weights = (3, 2, 1, 4) +## Output: PERL +# +## Example 2 +## +## Input: @letters = ('A', 'U', 'R', 'K') +## @weights = (2, 4, 1, 3) +## Output: RAKU +# +## Example 3 +## +## Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T') +## @weights = (5, 4, 2, 6, 1, 3) +## Output: PYTHON +# +############################################################ +## +## discussion +## +############################################################ +# +# Merge the arrays into one so it's easier to sort, then +# concatenate the sorted characters + +use strict; +use warnings; + +sort_letters( ['R', 'E', 'P', 'L'], [3, 2, 1, 4] ); +sort_letters( ['A', 'U', 'R', 'K'], [2, 4, 1, 3] ); +sort_letters( ['O', 'H', 'Y', 'N', 'P', 'T'], [5, 4, 2, 6, 1, 3] ); + +sub sort_letters { + my ($letters, $weights) = @_; + my @letters = @$letters; + print "Input: ('", join("', '", @$letters), "'), (", join(", ", @$weights), ")\n"; + my @combined; + foreach my $i (0..$#letters) { + push @combined, [ $letters->[$i], $weights->[$i] ]; + } + my $output = ""; + foreach my $entry ( sort { $a->[1] <=> $b->[1] } @combined ) { + $output .= $entry->[0]; + } + print "Output: $output\n"; +} diff --git a/challenge-279/jeanluc2020/perl/ch-2.pl b/challenge-279/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..8811b072da --- /dev/null +++ b/challenge-279/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-279/#TASK2 +# +# Task 2: Split String +# ==================== +# +# You are given a string, $str. +# +# Write a script to split the given string into two containing exactly same +# number of vowels and return true if you can otherwise false. +# +## Example 1 +## +## Input: $str = "perl" +## Ouput: false +# +## Example 2 +## +## Input: $str = "book" +## Ouput: true +## +## Two possible strings "bo" and "ok" containing exactly one vowel each. +# +## Example 3 +## +## Input: $str = "good morning" +## Ouput: true +## +## Two possible strings "good " and "morning" containing two vowels each or +## "good m" and "orning" containing two vowels each. +# +############################################################ +## +## discussion +## +############################################################ +# +# We turn $str into an array of characters, then we check at each position +# whether a split right there would create two substrings with the same +# amount of vowels. +# A helper function takes care of counting the vowels in an array of characters. + +use strict; +use warnings; + +split_string("perl"); +split_string("book"); +split_string("good morning"); + +sub split_string { + my $str = shift; + print "Input: \"$str\"\n"; + my @letters = split //, $str; + foreach my $i (0..$#letters) { + return print "Output: true\n" if count_vowels(@letters[0..$i]) == count_vowels(@letters[$i+1..$#letters]); + } + return print "Output: false\n"; +} + +sub count_vowels { + my @chars = @_; + my $count = 0; + my $vowels = { a => 1, e => 1, i => 1, o => 1, u => 1, + A => 1, E => 1, I => 1, O => 1, U => 1, }; + foreach my $char (@chars) { + $count++ if $vowels->{$char}; + } + return $count; +} |
