Permalink
Browse files

Add specifications to all examples

  • Loading branch information...
1 parent 164fdee commit 75e98d00a0cebd144f09434bd77f7f2b6a1006ef @philandstuff philandstuff committed Sep 5, 2010
@@ -1,7 +1,14 @@
use v6;
+# Specification:
+# P07 (**) Flatten a nested array structure.
+# Transform an array, possibly holding arrays as elements into a `flat'
+# list by replacing each array with its elements (recursively).
+# Example:
+# > splat([1,[2,[3,4],5]]).perl.say;
+# (1, 2, 3, 4, 5)
+
-# p7
sub splat (@t) {
my @return;
for @t -> $i {
@@ -1,5 +1,24 @@
use v6;
+# Specification:
+# P07 (**) Flatten a nested array structure.
+# Transform an array, possibly holding arrays as elements into a `flat'
+# list by replacing each array with its elements (recursively).
+# Example:
+# > splat([1,[2,[3,4],5]]).perl.say;
+# (1, 2, 3, 4, 5)
+
+
+# We use the gather/take structure.
+# A gather block builds a list using take statements. Each take adds one
+# element to the list. The gather block returns the complete list.
+# A gather block operates with dynamic scope, so the take statements may be
+# in another subroutine.
+# for @t -> $t
+# iterates over @t, placing each element in turn in $t
+# ~~ is the smart match operator. Here we use it to check the type of $t.
+# we could have said $t.isa(Array) instead.
+
sub _splat(@t) {
for @t -> $t {
if $t ~~ Array { _splat($t) }
@@ -9,6 +28,6 @@ (@t)
sub splat (@t) { gather _splat(@t) }
-splat(['a', ['b',['c','d'], 'e']]).perl.say;
+splat([1, [2,[3,4], 5]]).perl.say;
# vim:ft=perl6
@@ -1,8 +1,16 @@
use v6;
-
-#p8
+# Specification:
+# P08 (**) Eliminate consecutive duplicates of list elements.
+# If a list contains repeated elements they should be replaced with a
+# single copy of the element. The order of the elements should not be
+# changed.
#
+# Example:
+# > say ~compress(<a a a a b c c a a d e e e e>)
+# a b c a d e
+
+
sub compress (@in) {
my @return;
my $last;
@@ -1,10 +1,33 @@
use v6;
-# "our" is needed for the current rakudo (2010-09-04)
-# but as soon as it's not needed, it's better without
+# Specification:
+# P08 (**) Eliminate consecutive duplicates of list elements.
+# If a list contains repeated elements they should be replaced with a
+# single copy of the element. The order of the elements should not be
+# changed.
+#
+# Example:
+# > say ~compress(<a a a a b c c a a d e e e e>)
+# a b c a d e
+
+
+# We define an infix operator called 'compress' such that:
+# 'a' compress 'a' gives 'a'
+# 'a' compress 'b' gives ['a','b']
+# [@a,'a'] compress 'a' gives [ @a,'a']
+# [@a,'a'] compress 'b' gives [ @a,'a','b']
+#
+# FIXME "our" is needed under rakudo as of 2010-09-04
our multi infix:<compress> ( $a, $b ) { $a ~~ $b ?? $a !! [ $a, $b ] }
our multi infix:<compress> ( @a, $b ) { @a[*-1] ~~ $b ?? @a !! [ @a, $b ] }
+# Now all we need to do is split our array up and insert compress.
+# given <a a b c c d> we want:
+# 'a' compress 'a' compress 'b' compress 'c' compress 'c' compress 'd'
+# The reduce metaoperator does exactly this. Example:
+# [+] (1,2,3,4,5) == 1 + 2 + 3 + 4 + 5 == 15
+# [~] <a b c d e> eq 'a' ~ 'b' ~ 'c' ~ 'd' ~ 'e' eq 'abcde'
+
say ([compress] <a a a a b c c a a d e e e e>).perl;
# vim:ft=perl6
@@ -1,6 +1,14 @@
-#
-# p09 - Pack consecutive duplicates of list elements into sublists.
-#
+use v6;
+
+# Specification:
+# P09 (**) Pack consecutive duplicates of list elements into sublists.
+# If a list contains repeated elements they should be placed in separate
+# sublists.
+# Example:
+# > pack_dup(<a a a a b c c a a d e e e e>).perl.say
+# [["a","a","a","a"],["b"],["c","c"],["a","a"],["d"],["e","e","e","e"]]
+
+
# Robert Eaglestone 22 sept 09
#
# My first Perl6 script - I'm sure this can be done better
@@ -1,11 +1,14 @@
-#Pack consecutive duplicates of list elements into sublists.
-#If a list contains repeated elements they should be placed in separate sublists.
-#
-#Example:
-#* (pack '(a a a a b c c a a d e e e e))
-# ((A A A A) (B) (C C) (A A) (D) (E E E E))
-
use v6;
+
+# Specification:
+# P09 (**) Pack consecutive duplicates of list elements into sublists.
+# If a list contains repeated elements they should be placed in separate
+# sublists.
+# Example:
+# > pack_dup(<a a a a b c c a a d e e e e>).perl.say
+# [["a","a","a","a"],["b"],["c","c"],["a","a"],["d"],["e","e","e","e"]]
+
+
my @l = <a a a a b c c a a d e e e e>;
sub packit (@in) {
my @out;
@@ -1,11 +1,14 @@
-#Pack consecutive duplicates of list elements into sublists.
-#If a list contains repeated elements they should be placed in separate sublists.
-#
-#Example:
-#* (pack '(a a a a b c c a a d e e e e))
-# ((A A A A) (B) (C C) (A A) (D) (E E E E))
-
use v6;
+
+# Specification:
+# P09 (**) Pack consecutive duplicates of list elements into sublists.
+# If a list contains repeated elements they should be placed in separate
+# sublists.
+# Example:
+# > pack_dup(<a a a a b c c a a d e e e e>).perl.say
+# [["a","a","a","a"],["b"],["c","c"],["a","a"],["d"],["e","e","e","e"]]
+
+
my @l = <a a a a b c c a a d e e e e>;
sub prob09 (@in) {
return gather while @in.elems {
@@ -1,11 +1,16 @@
-#Pack consecutive duplicates of list elements into sublists.
-#If a list contains repeated elements they should be placed in separate sublists.
-#
-#Example:
-#* (pack '(a a a a b c c a a d e e e e))
-# ((A A A A) (B) (C C) (A A) (D) (E E E E))
-
use v6;
+
+# Specification:
+# P10 (*) Run-length encoding of a list.
+# Use the result of problem P09 to implement the so-called run-length
+# encoding data compression method. Consecutive duplicates of elements
+# are encoded as arrays [N, E] where N is the number of duplicates of the
+# element E.
+# Example:
+# > encode(<a a a a b c c a a d e e e e>).perl.say
+# [[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]]
+
+
my @l = <a a a a b c c a a d e e e e>;
sub packit (@in) {
my @out;
@@ -1,13 +1,16 @@
-# Use the result of problem P09 to implement the so-called run-length encoding
-# data compression method. Consecutive duplicates of elements are encoded as
-# lists (N E) where N is the number of duplicates of the element E.Pack
-# consecutive duplicates of list elements into sublists.
+use v6;
+
+# Specification:
+# P10 (*) Run-length encoding of a list.
+# Use the result of problem P09 to implement the so-called run-length
+# encoding data compression method. Consecutive duplicates of elements
+# are encoded as arrays [N, E] where N is the number of duplicates of the
+# element E.
+# Example:
+# > encode(<a a a a b c c a a d e e e e>).perl.say
+# [[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]]
-#Example:
-#* (pack '(a a a a b c c a a d e e e e))
-# ((4 a) (1 b) (2 c) (2 a) (1 d) (4 e))
-use v6;
my @l = <a a a a b c c a a d e e e e>;
sub prob10 (@in) {
return gather loop {
@@ -1,12 +1,15 @@
-# Modify the result of problem P10 in such a way that if an element has no
-# duplicates it is simply copied into the result list. Only elements with
-# duplicates are transferred as (N E) lists.
-#
-#Example:
-#* (pack '(a a a a b c c a a d e e e e))
-# ((4 a) b (2 c) (2 a) d (4 e))
-
use v6;
+
+# Specification:
+# P11 (*) Modified run-length encoding.
+# Modify the result of problem P10 in such a way that if an element has
+# no duplicates it is simply copied into the result list. Only elements
+# with duplicates are transferred as (N E) lists.
+# Example:
+# > encode_modified(<a a a a b c c a a d e e e e>).perl.say
+# ([4, "a"], "b", [2, "c"], [2, "a"], "d", [4, "e"])
+
+
my @l = <a a a a b c c a a d e e e e>;
sub prob11 (@in) {
return gather loop {
@@ -1,5 +1,25 @@
use v6;
+# Specification:
+# P12 (**) Decode a run-length encoded list.
+# Given a run-length code list generated as specified in problem P11.
+# Construct its uncompressed version.
+
+
+# We use the following constructs:
+# .map
+# creates a modified sequence by applying the block to each element in
+# turn. Within the block the element is represented by $_
+# when expr { block }
+# roughly equivalent to: if $_ ~~ expr { block; next }
+# xx
+# list repetition operator. (<1 2> xx 3) is the same as <1 2 1 2 1 2>
+# .flat
+# flattens a sequence. The map has constructed a sequence of Parcels:
+# (('a','a','a','a'),'b',('c','c'))
+# This sequence may be presented flat or hierarchical depending on
+# context. We use .flat to force a flattened context.
+
sub prob12 (@a) {
my $l = @a.map: {
when Array { $_[1] xx $_[0] }
@@ -1,11 +1,11 @@
-# Given a run-length code list generated as specified in problem P11. Construct
-# its uncompressed version.
-#
-#Example:
-#* (unpack '((4 a) b (2 c) (2 a) d (4 e)))
-#(a a a a b c c a a d e e e e)
-
use v6;
+
+# Specification:
+# P12 (**) Decode a run-length encoded list.
+# Given a run-length code list generated as specified in problem P11.
+# Construct its uncompressed version.
+
+
my @l = ([<4 a>],'b',[<2 c>],[<2 a>], 'd', [<4 e>]);
sub prob12 (@in) {
my @out;
@@ -1,11 +1,25 @@
use v6;
+# Specification:
+# P13 (**) Run-length encoding of a list (direct solution).
+# Implement the so-called run-length encoding data compression method
+# directly. I.e. don't explicitly create the sublists containing the
+# duplicates, as in problem P09, but only count them. As in problem P11,
+# simplify the result list by replacing the singletons [1,X] by X.
+# Example:
+# > encode_direct(<a a a a b c c a a d e e e e>).perl.say
+# ([4, "a"], "b", [2, "c"], [2, "a"], "d", [4, "e"])
+
+
+# We use gather as a loop modifier, to gather a list over all the loop
+# iterations
+
sub runlength (@a) {
gather while @a.elems {
my $val = @a.shift;
my $num = 1;
while @a[0] ~~ $val { @a.shift; $num++; }
- take [$num,$val];
+ take $num == 1 ?? $val !! [$num,$val];
}
}
@@ -1,5 +1,16 @@
use v6;
+# Specification:
+# P13 (**) Run-length encoding of a list (direct solution).
+# Implement the so-called run-length encoding data compression method
+# directly. I.e. don't explicitly create the sublists containing the
+# duplicates, as in problem P09, but only count them. As in problem P11,
+# simplify the result list by replacing the singletons [1,X] by X.
+# Example:
+# > encode_direct(<a a a a b c c a a d e e e e>).perl.say
+# ([4, "a"], "b", [2, "c"], [2, "a"], "d", [4, "e"])
+
+
multi infix:<compress> ( $a, $b ) { $a ~~ $b ?? [[2, $a]] !! [ [1, $a], [1, $b] ] }
multi infix:<compress> ( @a, $b ) {
if @a[*-1][1] ~~ $b {
@@ -1,11 +1,13 @@
-# Duplicate the elements of a list.
-# Example:
-# * (dupli '(a b c c d))<BR>
-# (A A B B C C C C D D)<P>
-
use v6;
+# Specification:
+# P14 (*) Duplicate the elements of a list.
+# Example:
+# > say ~dupli(<a b c c d>);
+# a a b b c c c c d d
+
+
sub dupli(@l) {
return @l.map({$_, $_});
}
-say dupli(<a b c c d>);
+say ~dupli(<a b c c d>);
@@ -1,4 +1,10 @@
use v6;
+# Specification:
+# P14 (*) Duplicate the elements of a list.
+# Example:
+# > say ~dupli(<a b c c d>);
+# a a b b c c c c d d
+
say (map { $_ xx 2 }, <a b c c d>).perl;
@@ -1,11 +1,14 @@
-# Replicate the elements of a list a given number of times.
+use v6;
+
+# Specification:
+# P15 (**) Replicate the elements of a list a given number of times.
# Example:
-# * (repli '(a b c) 3)
-# (A A A B B B C C C)
+# > say ~repli <a b c>,3;
+# a a a b b b c c c
+
-use v6;
sub repli(@l,$n) {
return @l.map({$_ xx $n});
}
-say repli(<a b c>,3);
+say ~repli <a b c>,3;
@@ -1,7 +1,12 @@
-# Replicate the elements of a list a given number of times.
+use v6;
+
+# Specification:
+# P15 (**) Replicate the elements of a list a given number of times.
# Example:
-# * (repli '(a b c) 3)
-# (A A A B B B C C C)
+# > say ~repli <a b c>,3;
+# a a a b b b c c c
+
+
my @l = <a b c>;
sub prob15(@in, $n) {
gather { for 0 ... @in.end -> $i { for 1 ... $n { take @in[$i] } } }
Oops, something went wrong.

0 comments on commit 75e98d0

Please sign in to comment.