Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
first few of the 99 Problems list (from http://aperiodic.net/phil/sca…
  • Loading branch information
prakashk committed Apr 10, 2013
1 parent 947252c commit f2f920d
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spec/examples/99-problems/01.mo
@@ -0,0 +1,4 @@
# P01 (*) Find the last element of a list.

my @list = [1, 1, 2, 3, 5, 8];
say @list[-1];
4 changes: 4 additions & 0 deletions spec/examples/99-problems/02.mo
@@ -0,0 +1,4 @@
# P02 (*) Find the last but one element of a list.

my @list = [1, 1, 2, 3, 5, 8];
say @list[-2];
11 changes: 11 additions & 0 deletions spec/examples/99-problems/03.mo
@@ -0,0 +1,11 @@
# P03 (*) Find the Kth element of a list.
# By convention, the first element in the list is element 0.

# Example:

# moe> nth(2, [1, 1, 2, 3, 5, 8)]
# 2

sub nth($n, @list) { @list[$n] }

say nth(2, [1, 1, 2, 3, 5, 8]);
4 changes: 4 additions & 0 deletions spec/examples/99-problems/04.mo
@@ -0,0 +1,4 @@
# P04 (*) Find the number of elements of a list.

my @list = [1, 1, 2, 3, 5, 8];
say @list.length;
4 changes: 4 additions & 0 deletions spec/examples/99-problems/05.mo
@@ -0,0 +1,4 @@
# P05 (*) Reverse a list.

my @list = [1, 1, 2, 3, 5, 8];
say @list.reverse;
12 changes: 12 additions & 0 deletions spec/examples/99-problems/06.mo
@@ -0,0 +1,12 @@
# P06 (*) Find out whether a list is a palindrome.
# Example:

# moe> is_palindrome([1, 2, 3, 2, 1)]
# true

sub is_palindrome(@list) {
@list.reverse.eqv(@list);
}

my @list = [1, 2, 3, 2, 1];
say is_palindrome(@list);
4 changes: 4 additions & 0 deletions spec/examples/99-problems/07.mo
@@ -0,0 +1,4 @@
# P07 (**) Flatten a nested list structure.

my @list = [[1, 1], 2, [3, [5, 8]]];
say @list.flatten;
20 changes: 20 additions & 0 deletions spec/examples/99-problems/08.mo
@@ -0,0 +1,20 @@
# 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:

# moe> compress(['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e'])
# ['a', 'b', 'c', 'a', 'd', 'e']

sub compress(*@list) {
@list.reduce(-> (@a, $b) {
if (@a.length == 0 || @a[-1] ne $b) {
@a.push($b)
}
@a},
[])
}

say compress('a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e');
22 changes: 22 additions & 0 deletions spec/examples/99-problems/09.mo
@@ -0,0 +1,22 @@
# P09 (**) Pack consecutive duplicates of list elements into sublists.
# If a list contains repeated elements they should be placed in
# separate sublists.

# Example:
# moe> 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']]

sub pack(*@list) {
@list.reduce(-> (@a, $b) {
if (@a.length == 0 || @a[-1].at_pos(0) ne $b) {
@a.push([$b]);
}
else {
@a[-1].push($b);
}
@a
},
[])
}

say pack('a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e');
28 changes: 28 additions & 0 deletions spec/examples/99-problems/10.mo
@@ -0,0 +1,28 @@
# 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 lists (N E) where N is the
# number of duplicates of the element E.

# Example:
# moe> encode(["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"]]

sub pack(@list) {
@list.reduce(-> (@a, $b) {
if (@a.length == 0 || @a[-1].at_pos(0) ne $b) {
@a.push([$b]);
}
else {
@a[-1].push($b);
}
@a
},
[])
}

sub encode(*@list) {
pack(@list).map(-> (@a) {[@a.length, @a[0]]})
}

say encode("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e");
26 changes: 26 additions & 0 deletions spec/examples/99-problems/11.mo
@@ -0,0 +1,26 @@
# 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)
# terms.

# Example:

# moe> encodeModified("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e")
# [[4, "a"], "b", [2, "c"], [2, "a"], "d", [4, "e"]]

sub encodeModified(*@list) {
@list.reduce(-> (@a, $b) {
if (@a.length == 0 || @a[-1].at_pos(0) ne $b) {
@a.push([$b]);
}
else {
@a[-1].push($b);
}
@a
},
[])
.map(-> (@a) {@a.length == 1 ? @a[0] : [@a.length, @a[0]]})
}

say encodeModified('a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e');
14 changes: 14 additions & 0 deletions spec/examples/99-problems/12.mo
@@ -0,0 +1,14 @@
# P12 (**) Decode a run-length encoded list.
# Given a run-length code list generated as specified in problem
# P10, construct its uncompressed version.

# Example:

# moe> decode([[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]])
# ["a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e"]

sub decode(@list) {
@list.map(-> (@rlc) {[@rlc[1]] x @rlc[0]}).flatten
}

say decode([[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]])
24 changes: 24 additions & 0 deletions spec/examples/99-problems/13.mo
@@ -0,0 +1,24 @@
# P13 (**) Run-length encoding of a list (direct solution).
# Implement the so-called run-length encoding data compression
# method directly. I.e. don't use other methods you've written
# (like P09's pack); do all the work directly.

# Example:
# moe> encodeDirect(["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"]]

sub encodeDirect(*@list) {
@list.reduce(-> (@a, $b) {
if (@a.length == 0 || @a[-1].at_pos(0) ne $b) {
@a.push([$b]);
}
else {
@a[-1].push($b);
}
@a
},
[])
.map(-> (@a) {[@a.length, @a[0]]})
}

say encodeDirect("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e");
11 changes: 11 additions & 0 deletions spec/examples/99-problems/14.mo
@@ -0,0 +1,11 @@
# P14 (*) Duplicate the elements of a list.
# Example:

# moe> duplicate(["a", "b", "c", "c", "d"])
# ["a", "a", "b", "b", "c", "c", "c", "c", "d", "d"]

sub duplicate(@list) {
@list.map(-> ($x) {[$x, $x]}).flatten
}

say duplicate(["a", "b", "c", "c", "d"]);
11 changes: 11 additions & 0 deletions spec/examples/99-problems/15.mo
@@ -0,0 +1,11 @@
# P15 (**) Duplicate the elements of a list a given number of times.
# Example:

# moe> duplicateN(3, ["a", "b", "c", "c", "d"])
# ["a", "a", "a", "b", "b", "b", "c", "c", "c", "c", "c", "c", "d", "d", "d"]

sub duplicateN($n, @list) {
@list.map(-> ($x) {[$x] x $n}).flatten
}

say duplicateN(3, ["a", "b", "c", "c", "d"]);
15 changes: 15 additions & 0 deletions spec/examples/99-problems/16.mo
@@ -0,0 +1,15 @@
# P16 (**) Drop every Nth element from a list.
# Example:

# moe> drop(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"])
# ["a", "b", "d", "e", "g", "h", "j", "k"]

sub drop($n, @list) {
# need prefix -- (pre-decrement) operator for this to work
# @list.grep(-> ($a) { --$n % 3 != 0 })

# until then:
@list.grep(-> ($a) { $n = $n - 1; $n % 3 != 0 })
}

say drop(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]);

1 comment on commit f2f920d

@stevan
Copy link
Member

@stevan stevan commented on f2f920d Apr 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prakashk we should turn these into tests as well, using the Test::More

Please sign in to comment.