diff options
| -rw-r--r-- | challenge-228/packy-anderson/README.md | 34 | ||||
| -rwxr-xr-x | challenge-228/packy-anderson/raku/task-1.raku | 34 | ||||
| -rwxr-xr-x | challenge-228/packy-anderson/raku/task-2.raku | 32 |
3 files changed, 100 insertions, 0 deletions
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 |
