diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-11 09:41:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-11 09:41:21 +0100 |
| commit | 7393288e47338ee64ed0452c497020e8c8d1af53 (patch) | |
| tree | fdd76c6116fbec5ba0d96be2c0dfef7c5ff1d107 | |
| parent | f6f10ca8fde69577f0cce76aa5b8660d502ed851 (diff) | |
| parent | e7cbcbef554ad48ae88b8921dda46f0c16a148db (diff) | |
| download | perlweeklychallenge-club-7393288e47338ee64ed0452c497020e8c8d1af53.tar.gz perlweeklychallenge-club-7393288e47338ee64ed0452c497020e8c8d1af53.tar.bz2 perlweeklychallenge-club-7393288e47338ee64ed0452c497020e8c8d1af53.zip | |
Merge pull request #8353 from jeanluc2020/jeanluc-225
Add solution 225
| -rw-r--r-- | challenge-225/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-225/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-225/jeanluc2020/perl/ch-1.pl | 54 | ||||
| -rwxr-xr-x | challenge-225/jeanluc2020/perl/ch-2.pl | 93 |
4 files changed, 149 insertions, 0 deletions
diff --git a/challenge-225/jeanluc2020/blog-1.txt b/challenge-225/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..a5f935d5e7 --- /dev/null +++ b/challenge-225/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-225-1.html diff --git a/challenge-225/jeanluc2020/blog-2.txt b/challenge-225/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..fb76c73620 --- /dev/null +++ b/challenge-225/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-225-2.html diff --git a/challenge-225/jeanluc2020/perl/ch-1.pl b/challenge-225/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..6b26da959b --- /dev/null +++ b/challenge-225/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-225/#TASK1 +# +# Task 1: Max Words +# ================= +# +# You are given a list of sentences, @list. +# +## 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. +# +## Example 1 +## +## Input: @list = ("Perl and Raku belong to the same family.", +## "I love Perl.", +## "The Perl and Raku Conference.") +## Output: 8 +# +## Example 2 +## +## Input: @list = ("The Weekly Challenge.", +## "Python is the most popular guest language.", +## "Team PWC has over 300 members.") +## Output: 7 +# +############################################################ +## +## discussion +## +############################################################ +# +# Split all sentences into their single words and count. Keep +# the maximum. + +use strict; +use warnings; + +max_words("Perl and Raku belong to the same family.", "I love Perl.", "The Perl and Raku Conference."); +max_words("The Weekly Challenge.", "Python is the most popular guest language.", "Team PWC has over 300 members."); + +sub max_words { + my @list = @_; + print "Input: \@list = (\"" . join("\",\n \"",@list) . "\")\n"; + my $max = 0; + foreach my $sentence (@list) { + my @words = split / /, $sentence; + my $count = scalar(@words); + $max = $count if $count > $max; + } + print "Output: $max\n"; +} + diff --git a/challenge-225/jeanluc2020/perl/ch-2.pl b/challenge-225/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ce01c087cc --- /dev/null +++ b/challenge-225/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,93 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-225/#TASK2 +# +# Task 2: Left Right Sum Diff +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to return left right sum diff array as shown below: +# +# @ints = (a, b, c, d, e) +# +# @left = (0, a, (a+b), (a+b+c)) +# @right = ((c+d+e), (d+e), e, 0) +# @left_right_sum_diff = ( | 0 - (c+d+e) |, +# | a - (d+e) |, +# | (a+b) - e |, +# | (a+b+c) - 0 | ) +# +## +## WARNING: this explaination isn't entirely correct. As per +## https://leetcode.com/problems/left-and-right-sum-differences/ +## it would make more sense to have an additional element +## (a+b+c+d) at the end of @left and ((b+c+d+e) at the beginning +## of @right, which also the examples demonstrate: +## +# +## Example 1: +## +## Input: @ints = (10, 4, 8, 3) +## Output: (15, 1, 11, 22) +## +## @left = (0, 10, 14, 22) +## @right = (15, 11, 3, 0) +## +## @left_right_sum_diff = ( |0-15|, |10-11|, |14-3|, |22-0|) +## = (15, 1, 11, 22) +# +## Example 2: +## +## Input: @ints = (1) +## Output: (0) +## +## @left = (0) +## @right = (0) +## +## @left_right_sum_diff = ( |0-0| ) = (0) +# +## Example 3: +## +## Input: @ints = (1, 2, 3, 4, 5) +## Output: (14, 11, 6, 1, 19) +## +## @left = (0, 1, 3, 6, 10) +## @right = (14, 12, 9, 5, 0) +## +## @left_right_sum_diff = ( |0-14|, |1-12|, |3-9|, |6-5|, |10-0|) +## = (14, 11, 6, 1, 10) +# +############################################################ +## +## discussion +## +############################################################ +# +# First, calculate @left and @right, then calculate the +# @left_right_sum_diff + +use strict; +use warnings; + +left_right_sum_diff(10, 4, 8, 3); +left_right_sum_diff(1); +left_right_sum_diff(1, 2, 3, 4, 5); + +sub left_right_sum_diff { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my ($current_left, $current_right) = (0, 0); + my @left = (); + my @right = (); + foreach my $index (0..$#ints) { + push @left, $current_left; + $current_left += $ints[$index]; + unshift @right, $current_right; + $current_right += $ints[ $#ints - $index ]; + } + my @left_right_sum_diff = (); + foreach my $index (0..$#ints) { + $left_right_sum_diff[$index] = abs($left[$index] - $right[$index]); + } + print "Output: (" . join(", ", @left_right_sum_diff) . ")\n"; +} |
