Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
16 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# P01 (*) Find the last element of a list. | ||
|
||
my @list = [1, 1, 2, 3, 5, 8]; | ||
say @list[-1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# P04 (*) Find the number of elements of a list. | ||
|
||
my @list = [1, 1, 2, 3, 5, 8]; | ||
say @list.length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# P05 (*) Reverse a list. | ||
|
||
my @list = [1, 1, 2, 3, 5, 8]; | ||
say @list.reverse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# P07 (**) Flatten a nested list structure. | ||
|
||
my @list = [[1, 1], 2, [3, [5, 8]]]; | ||
say @list.flatten; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]); |
f2f920d
There was a problem hiding this comment.
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