diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-23 00:04:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-23 00:04:20 +0100 |
| commit | 2263b40a381b10424128d2ced8ccc8da192a1a24 (patch) | |
| tree | 120ae7c63a43eea9e3cb1dd66c0a91e56b701ab0 | |
| parent | 35e22b8fdfe2553f1e779bfbb6d7a07b439222ce (diff) | |
| parent | 59d7ad2893326065058cfa5a7150fa8ecf3961f1 (diff) | |
| download | perlweeklychallenge-club-2263b40a381b10424128d2ced8ccc8da192a1a24.tar.gz perlweeklychallenge-club-2263b40a381b10424128d2ced8ccc8da192a1a24.tar.bz2 perlweeklychallenge-club-2263b40a381b10424128d2ced8ccc8da192a1a24.zip | |
Merge pull request #8423 from jeanluc2020/jeanluc-226
Add solution 226
| -rw-r--r-- | challenge-226/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-226/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-226/jeanluc2020/perl/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-226/jeanluc2020/perl/ch-2.pl | 102 |
4 files changed, 151 insertions, 0 deletions
diff --git a/challenge-226/jeanluc2020/blog-1.txt b/challenge-226/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..fb39ca1878 --- /dev/null +++ b/challenge-226/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-226-1.html diff --git a/challenge-226/jeanluc2020/blog-2.txt b/challenge-226/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..003976ec40 --- /dev/null +++ b/challenge-226/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-226-2.html diff --git a/challenge-226/jeanluc2020/perl/ch-1.pl b/challenge-226/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..c42da7d93c --- /dev/null +++ b/challenge-226/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-226/#TASK1 +# +# Task 1: Shuffle String +# ====================== +# +# You are given a string and an array of indices of same length as string. +# +# Write a script to return the string after re-arranging the indices in the correct order. +# +## Example 1 +## +## Input: $string = 'lacelengh', @indices = (3,2,0,5,4,8,6,7,1) +## Output: 'challenge' +# +## Example 2 +## +## Input: $string = 'rulepark', @indices = (4,7,3,1,0,5,2,6) +## Output: 'perlraku' +# +############################################################ +## +## discussion +## +############################################################ +# +# Split the string into an array of characters, then walk that +# array and assign each character to the corresponding index of +# a new array. + +use strict; +use warnings; + +shuffle_string('lacelengh', 3,2,0,5,4,8,6,7,1); +shuffle_string('rulepark', 4,7,3,1,0,5,2,6); + +sub shuffle_string { + my ($string, @indices) = @_; + print "Input: '$string', (" . join(",", @indices) . ")\n"; + my @chars = split //, $string; + my @new = (); + foreach my $i (0..$#chars) { + $new[$indices[$i]] = $chars[$i]; + } + print "Output: '" . join("",@new) . "'\n"; +} + diff --git a/challenge-226/jeanluc2020/perl/ch-2.pl b/challenge-226/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..2acb7aaca8 --- /dev/null +++ b/challenge-226/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,102 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-226/#TASK2 +# +# Task 2: Zero Array +# ================== +# +# You are given an array of non-negative integers, @ints. +# +# Write a script to return the minimum number of operations to make every +# element equal zero. +# +## In each operation, you are required to pick a positive number less than or +## equal to the smallest element in the array, then subtract that from each +## positive element in the array. +# +# +# Example 1: +# +# Input: @ints = (1, 5, 0, 3, 5) +# Output: 3 +# +# operation 1: pick 1 => (0, 4, 0, 2, 4) +# operation 2: pick 2 => (0, 2, 0, 0, 2) +# operation 3: pick 2 => (0, 0, 0, 0, 0) +# +# Example 2: +# +# Input: @ints = (0) +# Output: 0 +# +# Example 3: +# +# Input: @ints = (2, 1, 4, 0, 3) +# Output: 4 +# +# operation 1: pick 1 => (1, 0, 3, 0, 2) +# operation 2: pick 1 => (0, 0, 2, 0, 1) +# operation 3: pick 1 => (0, 0, 1, 0, 0) +# operation 4: pick 1 => (0, 0, 0, 0, 0) +# +############################################################ +## +## discussion +## +############################################################ +# +# Comment: +# While the description says "pick a positive number less than or +# equal to the smallest element in the array", it is actually the +# smallest *positive* element in the array, or we could only pick +# "0" after the first element in the array is 0. +# +# Let's think a moment. If we pick any number that is less than +# the smallest positive number, we will have to pick another +# number again to get the smallest positive number to zero, so +# it's always best to use the smallest positive number to minimize +# the number of picks. Given this, it's simple to always pick the +# smallest positive number, substract it from all remaining +# positive numbers and be done. + +use strict; +use warnings; + +zero_array(1, 5, 0, 3, 5); +zero_array(0); +zero_array(2, 1, 4, 0, 3); + +sub zero_array { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $steps = 0; + while(non_zero(@ints)) { + my $smallest_positive = smallest_positive(@ints); + # substract the smallest positive number from each non-zero element + @ints = map { $_ == 0 ? 0 : $_ - $smallest_positive; } @ints; + $steps++; + } + print "Output: $steps\n"; +} + +# check if there are non-zero elements in an array +sub non_zero { + my @ints = @_; + foreach my $i (@ints) { + return 1 if $i; + } + return 0; +} + +# find the smallest positive number in an array +sub smallest_positive { + my @ints = @_; + my $smallest; + foreach my $i (@ints) { + if($i) { + $smallest //= $i; + $smallest = $i if $i < $smallest; + } + } + # returns undef if there is no positive number in the array + return $smallest; +} |
