diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-11 09:35:48 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-11 09:35:48 +0100 |
| commit | 2c757db65679e6165b9eda52ad6a5b25c54a3c75 (patch) | |
| tree | 639d20c43120eda5e7138592ae1d0a864245e076 | |
| parent | 6763f8eb88bb7b4e5db00d09508298893c01e892 (diff) | |
| parent | 54a9e6263da63d4fce1c6e3b15051774daf8ea42 (diff) | |
| download | perlweeklychallenge-club-2c757db65679e6165b9eda52ad6a5b25c54a3c75.tar.gz perlweeklychallenge-club-2c757db65679e6165b9eda52ad6a5b25c54a3c75.tar.bz2 perlweeklychallenge-club-2c757db65679e6165b9eda52ad6a5b25c54a3c75.zip | |
Merge pull request #8351 from voegelas/challenge-225
Challenge 225 by Andreas Vögele
| -rwxr-xr-x | challenge-225/andreas-voegele/kotlin/ch-1.kts | 27 | ||||
| -rwxr-xr-x | challenge-225/andreas-voegele/kotlin/ch-2.kts | 23 | ||||
| -rwxr-xr-x | challenge-225/andreas-voegele/perl/ch-1.pl | 27 | ||||
| -rwxr-xr-x | challenge-225/andreas-voegele/perl/ch-2.pl | 33 |
4 files changed, 110 insertions, 0 deletions
diff --git a/challenge-225/andreas-voegele/kotlin/ch-1.kts b/challenge-225/andreas-voegele/kotlin/ch-1.kts new file mode 100755 index 0000000000..8f99122e31 --- /dev/null +++ b/challenge-225/andreas-voegele/kotlin/ch-1.kts @@ -0,0 +1,27 @@ +#!/usr/bin/env kotlin + +/* + * You are given a list of sentences. A sentence is a list of words that are + * separated by a single space with no leading or trailing spaces. Write a + * script to find out the maximum number of words that appear in a single + * sentence. + */ + +fun maxWords(vararg sentences: String): Int = + sentences.maxOfOrNull { it.split(' ').size } ?: 0 + +println( + maxWords( + "Perl and Raku belong to the same family.", + "I love Perl.", + "The Perl and Raku Conference." + ) +) + +println( + maxWords( + "The Weekly Challenge.", + "Kotlin is the most interesting guest language.", + "Team PWC has over 300 members." + ) +) diff --git a/challenge-225/andreas-voegele/kotlin/ch-2.kts b/challenge-225/andreas-voegele/kotlin/ch-2.kts new file mode 100755 index 0000000000..a8674c28c7 --- /dev/null +++ b/challenge-225/andreas-voegele/kotlin/ch-2.kts @@ -0,0 +1,23 @@ +#!/usr/bin/env kotlin + +/* + * You are given an array of integers. For each element calculate the the + * sums of the elements to the left and right. Return the differences between + * these sums. + */ + +fun leftRightSumDiff(ints: IntArray): IntArray { + val differences = IntArray(ints.size) + var leftSum = 0 + var rightSum = ints.sum() + ints.forEachIndexed { i, n -> + rightSum -= n + differences[i] = Math.abs(leftSum - rightSum) + leftSum += n + } + return differences +} + +println(leftRightSumDiff(intArrayOf(10, 4, 8, 3)).contentToString()) +println(leftRightSumDiff(intArrayOf(1)).contentToString()) +println(leftRightSumDiff(intArrayOf(1, 2, 3, 4, 5)).contentToString()) diff --git a/challenge-225/andreas-voegele/perl/ch-1.pl b/challenge-225/andreas-voegele/perl/ch-1.pl new file mode 100755 index 0000000000..51438b7327 --- /dev/null +++ b/challenge-225/andreas-voegele/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +# You are given a list of sentences. A sentence is a list of words that are +# separated by a single space with no leading or trailing spaces. Write a +# script to find out the maximum number of words that appear in a single +# sentence. + +use 5.036; +use utf8; + +use List::Util qw(max); + +sub max_words (@sentences) { + return max 0, map { scalar split } @sentences; +} + +say max_words( + 'Perl and Raku belong to the same family.', + 'I love Perl.', + 'The Perl and Raku Conference.' +); + +say max_words( + 'The Weekly Challenge.', + 'Kotlin is the most interesting guest language.', + 'Team PWC has over 300 members.' +); diff --git a/challenge-225/andreas-voegele/perl/ch-2.pl b/challenge-225/andreas-voegele/perl/ch-2.pl new file mode 100755 index 0000000000..760eba70ee --- /dev/null +++ b/challenge-225/andreas-voegele/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +# You are given an array of integers. For each element calculate the the sums +# of the elements to the left and right. Return the differences between these +# sums. + +use 5.036; +use utf8; + +use experimental qw(builtin for_list); + +use builtin qw(indexed); +use List::Util qw(sum0); + +sub left_right_sum_diff (@ints) { + my @differences; + my $left_sum = 0; + my $right_sum = sum0 @ints; + for my ($i, $n) (indexed @ints) { + $right_sum -= $n; + $differences[$i] = abs $left_sum - $right_sum; + $left_sum += $n; + } + return @differences; +} + +sub format_array (@array) { + return '(' . join(', ', @array) . ')'; +} + +say format_array(left_right_sum_diff(10, 4, 8, 3)); +say format_array(left_right_sum_diff(1)); +say format_array(left_right_sum_diff(1, 2, 3, 4, 5)); |
