From 040c25f0a6877449480160d2d0113a409b502b09 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 31 Jul 2023 11:37:55 -0400 Subject: Fix filenames. --- challenge-227/packy-anderson/perl/ch-1.pl | 36 --------- challenge-227/packy-anderson/perl/ch-2.pl | 79 ------------------- challenge-227/packy-anderson/perl/task-1.pl | 36 +++++++++ challenge-227/packy-anderson/perl/task-2.pl | 79 +++++++++++++++++++ challenge-227/packy-anderson/raku/ch-1.raku | 29 ------- challenge-227/packy-anderson/raku/ch-2.raku | 106 -------------------------- challenge-227/packy-anderson/raku/task-1.raku | 29 +++++++ challenge-227/packy-anderson/raku/task-2.raku | 106 ++++++++++++++++++++++++++ 8 files changed, 250 insertions(+), 250 deletions(-) delete mode 100755 challenge-227/packy-anderson/perl/ch-1.pl delete mode 100755 challenge-227/packy-anderson/perl/ch-2.pl create mode 100755 challenge-227/packy-anderson/perl/task-1.pl create mode 100755 challenge-227/packy-anderson/perl/task-2.pl delete mode 100755 challenge-227/packy-anderson/raku/ch-1.raku delete mode 100755 challenge-227/packy-anderson/raku/ch-2.raku create mode 100755 challenge-227/packy-anderson/raku/task-1.raku create mode 100755 challenge-227/packy-anderson/raku/task-2.raku diff --git a/challenge-227/packy-anderson/perl/ch-1.pl b/challenge-227/packy-anderson/perl/ch-1.pl deleted file mode 100755 index 1bfd2a1d13..0000000000 --- a/challenge-227/packy-anderson/perl/ch-1.pl +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env perl -use v5.38; - -# let's use the core modules for date manipulation -use Time::Piece; -use Time::Seconds qw( ONE_DAY ); - -# get the year from the command line -my $year = shift @ARGV - or die "usage: $0 year\n"; - -# do bounds checking as specified in the problem -if ($year < 1753 || $year > 9999) { - die "Only years between 1753 to 9999 are allowed ($year is out of range)\n"; -} - -# create an object for Jan 01 of the given year -my $t = Time::Piece->strptime("$year-01-01", "%Y-%m-%d") - ->truncate(to => 'day'); - -# find the first friday -# in Time::Piece->wday, 1 = Sunday, 6 = Friday -while ( $t->wday != 6) { - $t += ONE_DAY; # add 1 day -} - -# now keep adding 7 days to the date until the year changes, -# noting how many times the day of the month is 13 -my $thirteen_count = 0; -while ( $t->year == $year ) { - $thirteen_count++ if $t->mday == 13; - $t += ONE_DAY * 7; -} - -say "Input: \$year = $year"; -say "Output: $thirteen_count"; \ No newline at end of file diff --git a/challenge-227/packy-anderson/perl/ch-2.pl b/challenge-227/packy-anderson/perl/ch-2.pl deleted file mode 100755 index 425a4341ea..0000000000 --- a/challenge-227/packy-anderson/perl/ch-2.pl +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env perl -use v5.38; - -use Roman; # there's a module for handling Roman Numerals! - -sub do_arithmetic { - my $line = shift; - # split the inout line into the three parts: - # the two operands and the infix operator - my($operand1r, $operator, $operand2r) = split /\s+/, $line; - unless (defined $operand1r && - defined $operator && - defined $operand2r) { - say q{Lines must be of the form "operand1 operator operand2"}; - say q{where both operands are valid roman numerals and the}; - say q{operator is one of the following: + - * / **}; - return; - } - - my($operand1a, $operand2a); - - # check that the first operand is a roman numeral - if (isroman($operand1r)) { - # it is a roman numeral, convert it - $operand1a = arabic($operand1r); - } - else { - say "'$operand1r' is not a roman numberal!"; - return; - } - - # check that the second operand is a roman numeral - if (isroman($operand2r)) { - # it is a roman numeral, convert it - $operand2a = arabic($operand2r); - } - else { - say "'$operand2r' is not a roman numberal!"; - return; - } - - # calculate the results - my $result; - if ($operator eq '+') { $result = $operand1a + $operand2a; } - elsif ($operator eq '-') { $result = $operand1a - $operand2a; } - elsif ($operator eq '*') { $result = $operand1a * $operand2a; } - elsif ($operator eq '/') { $result = $operand1a / $operand2a; } - elsif ($operator eq '**') { $result = $operand1a ** $operand2a; } - else { - die "Unknown operator '$operator'; valid operators are + - * / **\n"; - } - - # handle all the special output cases - if ($result == 0) { - say "$operand1r $operator $operand2r => nulla " - . "(they knew about zero but didn't have a symbol)"; - } - elsif (int($result) != $result) { - say "$operand1r $operator $operand2r => non potest " - . "(they didn't do fractions)"; - } - elsif ($result > 3999) { - say "$operand1r $operator $operand2r => non potest " - . "(they only went up to 3999)"; - } - elsif ($result < 0) { - say "$operand1r $operator $operand2r => non potest " - . "(they didn't do negative numbers)"; - } - else { - say "$operand1r $operator $operand2r => " . uc roman($result); - } -} - -# while we have input on STDIN, process the calculations -while (my $line = <>) { - chomp $line; - do_arithmetic($line); -} \ No newline at end of file diff --git a/challenge-227/packy-anderson/perl/task-1.pl b/challenge-227/packy-anderson/perl/task-1.pl new file mode 100755 index 0000000000..1bfd2a1d13 --- /dev/null +++ b/challenge-227/packy-anderson/perl/task-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl +use v5.38; + +# let's use the core modules for date manipulation +use Time::Piece; +use Time::Seconds qw( ONE_DAY ); + +# get the year from the command line +my $year = shift @ARGV + or die "usage: $0 year\n"; + +# do bounds checking as specified in the problem +if ($year < 1753 || $year > 9999) { + die "Only years between 1753 to 9999 are allowed ($year is out of range)\n"; +} + +# create an object for Jan 01 of the given year +my $t = Time::Piece->strptime("$year-01-01", "%Y-%m-%d") + ->truncate(to => 'day'); + +# find the first friday +# in Time::Piece->wday, 1 = Sunday, 6 = Friday +while ( $t->wday != 6) { + $t += ONE_DAY; # add 1 day +} + +# now keep adding 7 days to the date until the year changes, +# noting how many times the day of the month is 13 +my $thirteen_count = 0; +while ( $t->year == $year ) { + $thirteen_count++ if $t->mday == 13; + $t += ONE_DAY * 7; +} + +say "Input: \$year = $year"; +say "Output: $thirteen_count"; \ No newline at end of file diff --git a/challenge-227/packy-anderson/perl/task-2.pl b/challenge-227/packy-anderson/perl/task-2.pl new file mode 100755 index 0000000000..425a4341ea --- /dev/null +++ b/challenge-227/packy-anderson/perl/task-2.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +use v5.38; + +use Roman; # there's a module for handling Roman Numerals! + +sub do_arithmetic { + my $line = shift; + # split the inout line into the three parts: + # the two operands and the infix operator + my($operand1r, $operator, $operand2r) = split /\s+/, $line; + unless (defined $operand1r && + defined $operator && + defined $operand2r) { + say q{Lines must be of the form "operand1 operator operand2"}; + say q{where both operands are valid roman numerals and the}; + say q{operator is one of the following: + - * / **}; + return; + } + + my($operand1a, $operand2a); + + # check that the first operand is a roman numeral + if (isroman($operand1r)) { + # it is a roman numeral, convert it + $operand1a = arabic($operand1r); + } + else { + say "'$operand1r' is not a roman numberal!"; + return; + } + + # check that the second operand is a roman numeral + if (isroman($operand2r)) { + # it is a roman numeral, convert it + $operand2a = arabic($operand2r); + } + else { + say "'$operand2r' is not a roman numberal!"; + return; + } + + # calculate the results + my $result; + if ($operator eq '+') { $result = $operand1a + $operand2a; } + elsif ($operator eq '-') { $result = $operand1a - $operand2a; } + elsif ($operator eq '*') { $result = $operand1a * $operand2a; } + elsif ($operator eq '/') { $result = $operand1a / $operand2a; } + elsif ($operator eq '**') { $result = $operand1a ** $operand2a; } + else { + die "Unknown operator '$operator'; valid operators are + - * / **\n"; + } + + # handle all the special output cases + if ($result == 0) { + say "$operand1r $operator $operand2r => nulla " + . "(they knew about zero but didn't have a symbol)"; + } + elsif (int($result) != $result) { + say "$operand1r $operator $operand2r => non potest " + . "(they didn't do fractions)"; + } + elsif ($result > 3999) { + say "$operand1r $operator $operand2r => non potest " + . "(they only went up to 3999)"; + } + elsif ($result < 0) { + say "$operand1r $operator $operand2r => non potest " + . "(they didn't do negative numbers)"; + } + else { + say "$operand1r $operator $operand2r => " . uc roman($result); + } +} + +# while we have input on STDIN, process the calculations +while (my $line = <>) { + chomp $line; + do_arithmetic($line); +} \ No newline at end of file diff --git a/challenge-227/packy-anderson/raku/ch-1.raku b/challenge-227/packy-anderson/raku/ch-1.raku deleted file mode 100755 index d9fef52c0d..0000000000 --- a/challenge-227/packy-anderson/raku/ch-1.raku +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env raku - -sub MAIN($year) { - # do bounds checking as specified in the problem - if ($year < 1753 || $year > 9999) { - say "Only years between 1753 to 9999 are allowed ($year is out of range)"; - exit 1; - } - - # create an object for Jan 01 of the given year - my $t = Date.new($year, 1, 1); - - # find the first friday - # in Date.day-of-week, 0 = Sunday, 5 = Friday - while ( $t.day-of-week != 5) { - $t++; # add 1 day - } - - # now keep adding 7 days to the date until the year changes, - # noting how many times the day of the month is 13 - my $thirteen_count = 0; - while ( $t.year == $year ) { - $thirteen_count++ if $t.day == 13; - $t += 7; - } - - say "Input: \$year = $year"; - say "Output: $thirteen_count"; -} \ No newline at end of file diff --git a/challenge-227/packy-anderson/raku/ch-2.raku b/challenge-227/packy-anderson/raku/ch-2.raku deleted file mode 100755 index 252340956f..0000000000 --- a/challenge-227/packy-anderson/raku/ch-2.raku +++ /dev/null @@ -1,106 +0,0 @@ -#!/usr/bin/env raku -use Math::Roman; # it's v0.0.1, but usable - -sub isroman ( $var ) { - # Math::Roman doesn't have a test to see if a string is - # a Roman numeral, but it does throw an exception if it - # cannot convert it - my $result; - try { - CATCH { - default { - return False; - } - } - $result = Math::Roman.new: $var; - } - # Math::Roman also doesn't respect the maximum of 3999 - if ($result.as-arabic > 3999) { - return False; - } - - return True; -} - -sub do_arithmetic (Str $line) { - # split the inout line into the three parts: - # the two operands and the infix operator - my ($operand1, $operator, $operand2) = $line.split(/\s+/); - - unless (defined $operand1 && - defined $operator && - defined $operand2) { - say q{Lines must be of the form "operand1 operator operand2"}; - say q{where both operands are valid roman numerals and the}; - say q{operator is one of the following: + - * / **}; - return; - } - - # check that the first operand is a roman numeral - if (isroman($operand1)) { - # it is a roman numeral, convert it - $operand1 = Math::Roman.new: $operand1; - } - else { - say "'$operand1' is not a roman numberal!"; - return; - } - - # check that the second operand is a roman numeral - if (isroman($operand2)) { - # it is a roman numeral, convert it - $operand2 = Math::Roman.new: $operand2; - } - else { - say "'$operand2' is not a roman numberal!"; - return; - } - - # # calculate the results - my $result; - if ($operator eq '+') { - $result = $operand1.as-arabic + $operand2.as-arabic; - } - elsif ($operator eq '-') { - $result = $operand1.as-arabic - $operand2.as-arabic; - } - elsif ($operator eq '*') { - $result = $operand1.as-arabic * $operand2.as-arabic; - } - elsif ($operator eq '/') { - $result = $operand1.as-arabic / $operand2.as-arabic; - } - elsif ($operator eq '**') { - $result = $operand1.as-arabic ** $operand2.as-arabic; - } - else { - die "Unknown operator '$operator'; valid operators are + - * / **\n"; - } - - # handle all the special output cases - if ($result == 0) { - say "$operand1 $operator $operand2 => nulla " - ~ "(they knew about zero but didn't have a symbol)"; - } - elsif ($result.truncate != $result) { - say "$operand1 $operator $operand2 => non potest " - ~ "(they didn't do fractions)"; - } - elsif ($result > 3999) { - say "$operand1 $operator $operand2 => non potest " - ~ "(they only went up to 3999)"; - } - elsif ($result < 0) { - say "$operand1 $operator $operand2 => non potest " - ~ "(they didn't do negative numbers)"; - } - else { - $result = Math::Roman.new: value => $result.Int; - say "$operand1 $operator $operand2 => $result"; - } -} - -# while we have input on STDIN, process the calculations -for $*IN.lines -> $line { - do_arithmetic($line); -} \ No newline at end of file diff --git a/challenge-227/packy-anderson/raku/task-1.raku b/challenge-227/packy-anderson/raku/task-1.raku new file mode 100755 index 0000000000..d9fef52c0d --- /dev/null +++ b/challenge-227/packy-anderson/raku/task-1.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +sub MAIN($year) { + # do bounds checking as specified in the problem + if ($year < 1753 || $year > 9999) { + say "Only years between 1753 to 9999 are allowed ($year is out of range)"; + exit 1; + } + + # create an object for Jan 01 of the given year + my $t = Date.new($year, 1, 1); + + # find the first friday + # in Date.day-of-week, 0 = Sunday, 5 = Friday + while ( $t.day-of-week != 5) { + $t++; # add 1 day + } + + # now keep adding 7 days to the date until the year changes, + # noting how many times the day of the month is 13 + my $thirteen_count = 0; + while ( $t.year == $year ) { + $thirteen_count++ if $t.day == 13; + $t += 7; + } + + say "Input: \$year = $year"; + say "Output: $thirteen_count"; +} \ No newline at end of file diff --git a/challenge-227/packy-anderson/raku/task-2.raku b/challenge-227/packy-anderson/raku/task-2.raku new file mode 100755 index 0000000000..252340956f --- /dev/null +++ b/challenge-227/packy-anderson/raku/task-2.raku @@ -0,0 +1,106 @@ +#!/usr/bin/env raku +use Math::Roman; # it's v0.0.1, but usable + +sub isroman ( $var ) { + # Math::Roman doesn't have a test to see if a string is + # a Roman numeral, but it does throw an exception if it + # cannot convert it + my $result; + try { + CATCH { + default { + return False; + } + } + $result = Math::Roman.new: $var; + } + # Math::Roman also doesn't respect the maximum of 3999 + if ($result.as-arabic > 3999) { + return False; + } + + return True; +} + +sub do_arithmetic (Str $line) { + # split the inout line into the three parts: + # the two operands and the infix operator + my ($operand1, $operator, $operand2) = $line.split(/\s+/); + + unless (defined $operand1 && + defined $operator && + defined $operand2) { + say q{Lines must be of the form "operand1 operator operand2"}; + say q{where both operands are valid roman numerals and the}; + say q{operator is one of the following: + - * / **}; + return; + } + + # check that the first operand is a roman numeral + if (isroman($operand1)) { + # it is a roman numeral, convert it + $operand1 = Math::Roman.new: $operand1; + } + else { + say "'$operand1' is not a roman numberal!"; + return; + } + + # check that the second operand is a roman numeral + if (isroman($operand2)) { + # it is a roman numeral, convert it + $operand2 = Math::Roman.new: $operand2; + } + else { + say "'$operand2' is not a roman numberal!"; + return; + } + + # # calculate the results + my $result; + if ($operator eq '+') { + $result = $operand1.as-arabic + $operand2.as-arabic; + } + elsif ($operator eq '-') { + $result = $operand1.as-arabic - $operand2.as-arabic; + } + elsif ($operator eq '*') { + $result = $operand1.as-arabic * $operand2.as-arabic; + } + elsif ($operator eq '/') { + $result = $operand1.as-arabic / $operand2.as-arabic; + } + elsif ($operator eq '**') { + $result = $operand1.as-arabic ** $operand2.as-arabic; + } + else { + die "Unknown operator '$operator'; valid operators are + - * / **\n"; + } + + # handle all the special output cases + if ($result == 0) { + say "$operand1 $operator $operand2 => nulla " + ~ "(they knew about zero but didn't have a symbol)"; + } + elsif ($result.truncate != $result) { + say "$operand1 $operator $operand2 => non potest " + ~ "(they didn't do fractions)"; + } + elsif ($result > 3999) { + say "$operand1 $operator $operand2 => non potest " + ~ "(they only went up to 3999)"; + } + elsif ($result < 0) { + say "$operand1 $operator $operand2 => non potest " + ~ "(they didn't do negative numbers)"; + } + else { + $result = Math::Roman.new: value => $result.Int; + say "$operand1 $operator $operand2 => $result"; + } +} + +# while we have input on STDIN, process the calculations +for $*IN.lines -> $line { + do_arithmetic($line); +} \ No newline at end of file -- cgit From 87f2e36eff988be70318ba16552579c8981529d2 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 31 Jul 2023 11:55:12 -0400 Subject: More filename fixes --- challenge-227/packy-anderson/README | 68 ---------------------------------- challenge-227/packy-anderson/README.md | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 68 deletions(-) delete mode 100644 challenge-227/packy-anderson/README create mode 100644 challenge-227/packy-anderson/README.md diff --git a/challenge-227/packy-anderson/README b/challenge-227/packy-anderson/README deleted file mode 100644 index 1b230ba294..0000000000 --- a/challenge-227/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-227/packy-anderson/README.md b/challenge-227/packy-anderson/README.md new file mode 100644 index 0000000000..1b230ba294 --- /dev/null +++ b/challenge-227/packy-anderson/README.md @@ -0,0 +1,68 @@ +# 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 -- cgit From 9ea9733bd286fd5f43b0694966765cc2c51563c7 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 31 Jul 2023 12:11:47 -0400 Subject: Challenge 228 solutions in Perl by Packy Anderson --- challenge-228/packy-anderson/README | 68 ----------------------------- challenge-228/packy-anderson/README.md | 66 ++++++++++++++++++++++++++++ challenge-228/packy-anderson/perl/task-1.pl | 36 +++++++++++++++ challenge-228/packy-anderson/perl/task-2.pl | 34 +++++++++++++++ 4 files changed, 136 insertions(+), 68 deletions(-) delete mode 100644 challenge-228/packy-anderson/README create mode 100644 challenge-228/packy-anderson/README.md create mode 100755 challenge-228/packy-anderson/perl/task-1.pl create mode 100755 challenge-228/packy-anderson/perl/task-2.pl 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..d866c69b7c --- /dev/null +++ b/challenge-228/packy-anderson/README.md @@ -0,0 +1,66 @@ +# 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 +``` +``` + +* [Task 2](raku/task-2.raku) + +Sample output +``` +``` + +## Blog Post + +[Perl Weekly Challenge 228]() \ 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; -- cgit From 92a8a1135c0ac2f2b9b98c3f203320105279d6b2 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 31 Jul 2023 13:31:07 -0400 Subject: Challenge 228 solutions in Raku by Packy Anderson --- challenge-228/packy-anderson/README.md | 34 +++++++++++++++++++++++++++ challenge-228/packy-anderson/raku/task-1.raku | 34 +++++++++++++++++++++++++++ challenge-228/packy-anderson/raku/task-2.raku | 32 +++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100755 challenge-228/packy-anderson/raku/task-1.raku create mode 100755 challenge-228/packy-anderson/raku/task-2.raku diff --git a/challenge-228/packy-anderson/README.md b/challenge-228/packy-anderson/README.md index d866c69b7c..8e9cbe199c 100644 --- a/challenge-228/packy-anderson/README.md +++ b/challenge-228/packy-anderson/README.md @@ -53,12 +53,46 @@ Operation 3: remove element 3: () 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 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 -- cgit From c15689d480f40931ab66185ffc9d7cef25259243 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Wed, 2 Aug 2023 00:18:23 -0400 Subject: Add blog post link to README --- challenge-228/packy-anderson/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-228/packy-anderson/README.md b/challenge-228/packy-anderson/README.md index 8e9cbe199c..481e8b2ced 100644 --- a/challenge-228/packy-anderson/README.md +++ b/challenge-228/packy-anderson/README.md @@ -97,4 +97,4 @@ Operation 3: remove element 3: () ## Blog Post -[Perl Weekly Challenge 228]() \ No newline at end of file +[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 -- cgit