Permalink
Browse files

Added my own solutions for 99-problems/P01-P26.

All currently run correctly on both rakudo and niecza.
  • Loading branch information...
1 parent d692b61 commit 7d9c16311ed1cacedbd2b227e5d69b384eee8381 Ryan Connelly committed Aug 16, 2012
View
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl6
+use v6;
+
+# Use of the Whatever type to grab the last element of a list.
+
+sub last-elem(@list)
+{
+ @list[* - 1];
+}
+
+say last-elem(<a b c d e>);
+
+=begin pod
+
+=head1 NAME
+
+P01 - Find the last element of a list.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl6
+use v6;
+
+# Further use of the Whatever type to grab the last elements of a list.
+
+sub last-two(@list)
+{
+ @list[* - 2, * - 1];
+}
+
+say last-two(<a b c d e>);
+
+=begin pod
+
+=head1 NAME
+
+P02 - Find the last two elements of a list.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub get-at(@list, $elem)
+{
+ @list[$elem];
+}
+
+say get-at(<a b c d e>, 4);
+
+=begin pod
+
+=head1 NAME
+
+P03 - Find the C<k>th element of a list.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub get-num-elems(@list)
+{
+ @list.elems;
+}
+
+say get-num-elems(<a b c d e>);
+
+=begin pod
+
+=head1 NAME
+
+P04 - Find the number of elements in a list.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub reverse-list(@list)
+{
+ @list.reverse;
+}
+
+# Make sure it's treated as a list, since <> creates a parcel.
+say reverse-list(<a b c d e>.list);
+
+=begin pod
+
+=head1 NAME
+
+P05 - Reverse a list.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub palindromic(@list)
+{
+ @list ~~ @list.reverse;
+}
+
+say palindromic(<a b c d e>.list);
+say palindromic(<a b c b a>.list);
+
+=begin pod
+
+=head1 NAME
+
+P06 - Find out whether a list is a palindrome.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub flatten(@array)
+{
+ gather for @array -> $e
+ {
+ $e ~~ Array ?? take(flatten $e) !! take $e
+ }
+}
+
+my @a = 1, 2, [3, 4], 5;
+
+say @a.perl;
+say 'Flattened:';
+say flatten(@a).perl;
+
+=begin pod
+
+=head1 NAME
+
+P07 - Flatten a nested array structure.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub eliminate-consecutive-duplicates(@list)
+{
+ my $last = ();
+
+ gather for @list -> $e
+ {
+ # ~~ is the smart match operator.
+ # It's the closest thing I can think of to a comparison function
+ # which doesn't enforce a particular context... should be alright. :-)
+ next if $e ~~ $last;
+
+ $last = $e;
+
+ take $e;
+ }
+}
+
+say eliminate-consecutive-duplicates(<a a a a a b b c b d e e>);
+
+=begin pod
+
+=head1 NAME
+
+P08 - Eliminate consecutive duplicates of list elements.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub pack-consecutive-dups(@list)
+{
+ gather while @list.elems
+ {
+ my $last = @list[0];
+
+ take [
+ gather while @list.elems
+ and @list[0] ~~ $last
+ {
+ take ($last = shift @list)
+ }
+ ]
+ }
+}
+
+say pack-consecutive-dups(<a a a a a b b c b d e e>.list).perl;
+
+=begin pod
+
+=head1 NAME
+
+P09 - Pack consecutive duplicate elements of a list into sublists.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub encode(@list)
+{
+ gather while @list.elems
+ {
+ my $value = @list[0];
+ my $count = 0;
+
+ take [
+ while @list.elems
+ and @list[0] ~~ $value
+ {
+ $count++;
+ shift @list
+ }
+
+ $count, $value
+ ]
+ }
+}
+
+say encode(<a a a a a b b c b d e e>.list).perl;
+
+=begin pod
+
+=head1 NAME
+
+P10 - Run-length encoding of a list.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub encode(@list)
+{
+ gather while @list.elems
+ {
+ my $value = @list[0];
+ my $count = 0;
+
+ take (
+ while @list.elems
+ and @list[0] ~~ $value
+ {
+ $count++;
+ shift @list
+ }
+
+ $count == 1 ?? $value !! [$count, $value]
+ )
+ }
+}
+
+say encode(<a a a a b c c a a d e e e e>.list).perl;
+
+=begin pod
+
+=head1 NAME
+
+P11 - Modified run-length encoding.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,22 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub decode(@list)
+{
+ gather for @list -> $e
+ {
+ $e !~~ Array ?? take($e) !! take($e[1] xx $e[0])
+ }
+}
+
+say decode(([5, "a"], [2, "b"], "c", "b", "d", [2, "e"]).list).perl;
+
+=begin pod
+
+=head1 NAME
+
+P12 - Decode modified run-length encoding.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl6
+use v6;
+
+# I can't see the difference between this and P11.
+# Perhaps I'm being stupid.
+
+sub encode(@list)
+{
+ gather while @list.elems
+ {
+ my $value = @list[0];
+ my $count = 0;
+
+ take (
+ while @list.elems
+ and @list[0] ~~ $value
+ {
+ $count++;
+ shift @list
+ }
+
+ $count == 1 ?? $value !! [$count, $value]
+ )
+ }
+}
+
+say encode(<a a a a b c c a a d e e e e>.list).perl;
+
+=begin pod
+
+=head1 NAME
+
+P13 - Direct run-length encoding.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub duplicate(@list)
+{
+ @list.map({$_, $_});
+}
+
+say duplicate(<a b c c d e>.list);
+
+=begin pod
+
+=head1 NAME
+
+P14 - Duplicate the elements in a list.
+
+=end pod
+
+# vim: ft=perl6
View
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub replicate(@list, $times)
+{
+ @list.map({$_ xx $times});
+}
+
+say replicate(<a b c c d e>.list, 3);
+
+=begin pod
+
+=head1 NAME
+
+P15 - Replicate the elements of a list a given number of times.
+
+=end pod
+
+# vim: ft=perl6
Oops, something went wrong.

0 comments on commit 7d9c163

Please sign in to comment.