aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-08-02 13:28:17 +0100
committerGitHub <noreply@github.com>2023-08-02 13:28:17 +0100
commite7f919d06cc8f95c1364ea719bf5c9d634bcaebf (patch)
treeb7d878256c0c8974af2fcc68ad5ec22a1e28f6cf
parent87ca32a012a86f9f8c6fc0d306bf019fe420b290 (diff)
parentc15689d480f40931ab66185ffc9d7cef25259243 (diff)
downloadperlweeklychallenge-club-e7f919d06cc8f95c1364ea719bf5c9d634bcaebf.tar.gz
perlweeklychallenge-club-e7f919d06cc8f95c1364ea719bf5c9d634bcaebf.tar.bz2
perlweeklychallenge-club-e7f919d06cc8f95c1364ea719bf5c9d634bcaebf.zip
Merge pull request #8493 from packy/master
Challenge 228 solutions in Perl and Raku by Packy Anderson
-rw-r--r--challenge-227/packy-anderson/README.md (renamed from challenge-227/packy-anderson/README)0
-rwxr-xr-xchallenge-227/packy-anderson/perl/task-1.pl (renamed from challenge-227/packy-anderson/perl/ch-1.pl)0
-rwxr-xr-xchallenge-227/packy-anderson/perl/task-2.pl (renamed from challenge-227/packy-anderson/perl/ch-2.pl)0
-rwxr-xr-xchallenge-227/packy-anderson/raku/task-1.raku (renamed from challenge-227/packy-anderson/raku/ch-1.raku)0
-rwxr-xr-xchallenge-227/packy-anderson/raku/task-2.raku (renamed from challenge-227/packy-anderson/raku/ch-2.raku)0
-rw-r--r--challenge-228/packy-anderson/README68
-rw-r--r--challenge-228/packy-anderson/README.md100
-rwxr-xr-xchallenge-228/packy-anderson/perl/task-1.pl36
-rwxr-xr-xchallenge-228/packy-anderson/perl/task-2.pl34
-rwxr-xr-xchallenge-228/packy-anderson/raku/task-1.raku34
-rwxr-xr-xchallenge-228/packy-anderson/raku/task-2.raku32
11 files changed, 236 insertions, 68 deletions
diff --git a/challenge-227/packy-anderson/README b/challenge-227/packy-anderson/README.md
index 1b230ba294..1b230ba294 100644
--- a/challenge-227/packy-anderson/README
+++ b/challenge-227/packy-anderson/README.md
diff --git a/challenge-227/packy-anderson/perl/ch-1.pl b/challenge-227/packy-anderson/perl/task-1.pl
index 1bfd2a1d13..1bfd2a1d13 100755
--- a/challenge-227/packy-anderson/perl/ch-1.pl
+++ b/challenge-227/packy-anderson/perl/task-1.pl
diff --git a/challenge-227/packy-anderson/perl/ch-2.pl b/challenge-227/packy-anderson/perl/task-2.pl
index 425a4341ea..425a4341ea 100755
--- a/challenge-227/packy-anderson/perl/ch-2.pl
+++ b/challenge-227/packy-anderson/perl/task-2.pl
diff --git a/challenge-227/packy-anderson/raku/ch-1.raku b/challenge-227/packy-anderson/raku/task-1.raku
index d9fef52c0d..d9fef52c0d 100755
--- a/challenge-227/packy-anderson/raku/ch-1.raku
+++ b/challenge-227/packy-anderson/raku/task-1.raku
diff --git a/challenge-227/packy-anderson/raku/ch-2.raku b/challenge-227/packy-anderson/raku/task-2.raku
index 252340956f..252340956f 100755
--- a/challenge-227/packy-anderson/raku/ch-2.raku
+++ b/challenge-227/packy-anderson/raku/task-2.raku
diff --git a/challenge-228/packy-anderson/README b/challenge-228/packy-anderson/README
deleted file mode 100644
index 1b230ba294..0000000000
--- a/challenge-228/packy-anderson/README
+++ /dev/null
@@ -1,68 +0,0 @@
-# Solutions by Packy Anderson
-
-## Perl
-
-* [Task 1](perl/task-1.pl)
-
-Sample output
-```
-$ perl/task-1.pl 2023
-Input: $year = 2023
-Output: 2
-```
-* [Task 2](perl/task-2.pl)
-
-Sample output
-```
-$ perl/task-2.pl < task-2-input.txt
-IV + V => IX
-M - I => CMXCIX
-X / II => V
-XI * VI => LXVI
-VII ** III => CCCXLIII
-V - V => nulla (they knew about zero but didn't have a symbol)
-V / II => non potest (they didn't do fractions)
-MMM + M => non potest (they only went up to 3999)
-V - X => non potest (they didn't do negative numbers)
-$ echo "MMMM" | perl/task-2.pl
-Lines must be of the form "operand1 operator operand2"
-where both operands are valid roman numerals and the
-operator is one of the following: + - * / **
-$ echo "X + Y" | perl/task-2.pl
-'Y' is not a roman numberal!
-```
-
-## Raku
-
-* [Task 1](raku/task-1.raku)
-
-Sample output
-```
-$ raku/task-1.raku 2023
-Input: $year = 2023
-Output: 2
-```
-
-* [Task 2](raku/task-2.raku)
-
-Sample output
-```
-$ raku/task-2.raku < task-2-input.txt
-IV + V => IX
-M - I => CMXCIX
-X / II => V
-XI * VI => LXVI
-VII ** III => CCCXLIII
-V - V => nulla (they knew about zero but didn't have a symbol)
-V / II => non potest (they didn't do fractions)
-MMM + M => non potest (they only went up to 3999)
-V - X => non potest (they didn't do negative numbers)
-```
-
-## Data File
-
-[task-2-input.txt](task-2-input.txt)
-
-## Blog Post
-
-[Perl Weekly Challenge 227](http://packy.dardan.com/2023/07/27/perl-weekly-challenge-227/) \ No newline at end of file
diff --git a/challenge-228/packy-anderson/README.md b/challenge-228/packy-anderson/README.md
new file mode 100644
index 0000000000..481e8b2ced
--- /dev/null
+++ b/challenge-228/packy-anderson/README.md
@@ -0,0 +1,100 @@
+# Solutions by Packy Anderson
+
+## Perl
+
+* [Task 1](perl/task-1.pl)
+
+Sample output
+```
+$ perl/task-1.pl 2 1 3 2
+Input: @int = (2, 1, 3, 2)
+Output: 4
+
+In the given array we have 2 unique elements (1, 3).
+
+$ perl/task-1.pl 1 1 1 1
+Input: @int = (1, 1, 1, 1)
+Output: 0
+
+In the given array no unique element found.
+
+$ perl/task-1.pl 2 1 3 4
+Input: @int = (2, 1, 3, 4)
+Output: 10
+
+In the given array every element is unique.
+```
+* [Task 2](perl/task-2.pl)
+
+Sample output
+```
+$ perl/task-2.pl 3 4 2
+Input: @int = (3, 4, 2)
+Output: 5
+
+Operation 1: move 3 to the end: (4,2,3)
+Operation 2: move 4 to the end: (2,3,4)
+Operation 3: remove element 2: (3,4)
+Operation 4: remove element 3: (4)
+Operation 5: remove element 4: ()
+
+$ perl/task-2.pl 1 2 3
+Input: @int = (1, 2, 3)
+Output: 3
+
+Operation 1: remove element 1: (2,3)
+Operation 2: remove element 2: (3)
+Operation 3: remove element 3: ()
+```
+
+## Raku
+
+* [Task 1](raku/task-1.raku)
+
+Sample output
+```
+$ raku/task-1.raku 2 1 3 2
+Input: @int = (2, 1, 3, 2)
+Output: 4
+
+In the given array we have 2 unique elements (1, 3).
+
+$ raku/task-1.raku 1 1 1 1
+Input: @int = (1, 1, 1, 1)
+Output: 0
+
+In the given array no unique element found.
+
+$ raku/task-1.raku 2 1 3 4
+Input: @int = (2, 1, 3, 4)
+Output: 10
+
+In the given array every element is unique.
+```
+
+* [Task 2](raku/task-2.raku)
+
+Sample output
+```
+$ raku/task-2.raku 3 4 2
+Input: @int = (3, 4, 2)
+Output: 5
+
+Operation 1: move 3 to the end: (4, 2, 3)
+Operation 2: move 4 to the end: (2, 3, 4)
+Operation 3: remove element 2: (3, 4)
+Operation 4: remove element 3: (4)
+Operation 5: remove element 4: ()
+
+$ raku/task-2.raku 1 2 3
+Input: @int = (1, 2, 3)
+Output: 3
+
+Operation 1: remove element 1: (2, 3)
+Operation 2: remove element 2: (3)
+Operation 3: remove element 3: ()
+```
+
+## Blog Post
+
+[Perl Weekly Challenge: Unique Sums and Empty Arrays](http://packy.dardan.com/2023/08/02/perl-weekly-challenge-unique-sums-and-empty-arrays/) \ No newline at end of file
diff --git a/challenge-228/packy-anderson/perl/task-1.pl b/challenge-228/packy-anderson/perl/task-1.pl
new file mode 100755
index 0000000000..c92826b80d
--- /dev/null
+++ b/challenge-228/packy-anderson/perl/task-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+use List::Util qw( sum );
+
+my @ints = @ARGV; # just accept the list of integers on the command line
+
+# find the unique elements
+my %unique;
+foreach my $int ( @ints ) {
+ $unique{$int}++;
+}
+
+# make a list of ONLY the unique ints
+my @unique_ints = grep { $unique{$_} == 1 } @ints;
+
+# sum the unique elements
+my $sum = sum(@unique_ints) // 0;
+
+# produce the output
+say "Input: \@int = (" . join(', ', @ints) . ")";
+say "Output: $sum";
+say "";
+
+print "In the given array ";
+if ( scalar(@unique_ints) == scalar(@ints) ) {
+ say "every element is unique.";
+}
+elsif ( scalar(@unique_ints) == 0 ) {
+ say "no unique element found.";
+}
+else {
+ say "we have " . scalar(@unique_ints) . " unique elements ("
+ . join(', ', @unique_ints) . ").";
+} \ No newline at end of file
diff --git a/challenge-228/packy-anderson/perl/task-2.pl b/challenge-228/packy-anderson/perl/task-2.pl
new file mode 100755
index 0000000000..079c1391bb
--- /dev/null
+++ b/challenge-228/packy-anderson/perl/task-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+use List::Util qw( min );
+
+my @ints = @ARGV; # just accept the list of integers on the command line
+
+my @operations;
+my $count = 1;
+while ( scalar(@ints) > 0 ) {
+ my $min = min @ints;
+
+ # in either case, we're removing the first element from the list
+ my $first = shift @ints;
+
+ if ($min == $first) {
+ # the first element is the minimum, discard it
+ push @operations, "Operation $count: remove element $min: (" . join(',', @ints) . ")";
+ }
+ else {
+ # the first element is NOT the minimum, add it to the end
+ push @ints, $first;
+ push @operations, "Operation $count: move $first to the end: (" . join(',', @ints) . ")";
+ }
+ $count++;
+}
+
+# produce the output
+# let's use @ARGV again, since we modify @ints as we go along
+say "Input: \@int = (" . join(', ', @ARGV) . ")";
+say "Output: " . scalar(@operations);
+say "";
+say join "\n", @operations;
diff --git a/challenge-228/packy-anderson/raku/task-1.raku b/challenge-228/packy-anderson/raku/task-1.raku
new file mode 100755
index 0000000000..f5221b310a
--- /dev/null
+++ b/challenge-228/packy-anderson/raku/task-1.raku
@@ -0,0 +1,34 @@
+#!/usr/bin/env raku
+
+use v6;
+
+my @ints = @*ARGS; # just accept the list of integers on the command line
+
+# find the unique elements
+my %unique;
+for @ints -> $int {
+ %unique{$int}++;
+}
+
+# make a list of ONLY the unique ints
+my @unique_ints = grep { %unique{$_} == 1 }, @ints;
+
+# sum the unique elements
+my $sum = [+] @unique_ints;
+
+# produce the output
+say "Input: \@int = (" ~ @ints.join(', ') ~ ")";
+say "Output: $sum";
+say "";
+
+print "In the given array ";
+if ( @unique_ints.elems == @ints.elems ) {
+ say "every element is unique.";
+}
+elsif ( @unique_ints.elems == 0 ) {
+ say "no unique element found.";
+}
+else {
+ say "we have " ~ @unique_ints.elems ~ " unique elements ("
+ ~ @unique_ints.join(', ') ~ ").";
+}
diff --git a/challenge-228/packy-anderson/raku/task-2.raku b/challenge-228/packy-anderson/raku/task-2.raku
new file mode 100755
index 0000000000..a7c75860bc
--- /dev/null
+++ b/challenge-228/packy-anderson/raku/task-2.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+
+use v6;
+
+my @ints = @*ARGS; # just accept the list of integers on the command line
+
+my @operations;
+my $count = 1;
+while ( @ints.elems > 0 ) {
+ my $min = @ints.min;
+
+ # in either case, we're removing the first element from the list
+ my $first = @ints.shift;
+
+ if ($min == $first) {
+ # the first element is the minimum, discard it
+ push @operations, "Operation $count: remove element $min: (" ~ @ints.join(', ') ~ ")";
+ }
+ else {
+ # the first element is NOT the minimum, add it to the end
+ push @ints, $first;
+ push @operations, "Operation $count: move $first to the end: (" ~ @ints.join(', ') ~ ")";
+ }
+ $count++;
+}
+
+# produce the output
+# let's use @ARGV again, since we modify @ints as we go along
+say "Input: \@int = (" ~ @*ARGS.join(', ') ~ ")";
+say "Output: " ~ @operations.elems;
+say "";
+say join "\n", @operations; \ No newline at end of file