Skip to content

Commit

Permalink
few more problems from "99 problems" series.
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashk committed Apr 23, 2013
1 parent d408f4f commit 2217345
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
15 changes: 15 additions & 0 deletions spec/examples/99-problems/17.mo
@@ -0,0 +1,15 @@
# P17 (*) Split a list into two parts.
# The length of the first part is given.

# Example:

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

sub split_list($n, @list) {
# XXXX: the range expressions should work without enclosing parens too!
[@list[0..($n-1)], @list[$n..(@list.length-1)]]
}

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

16 changes: 16 additions & 0 deletions spec/examples/99-problems/18.mo
@@ -0,0 +1,16 @@
# P18 (**) Extract a slice from a list.
# Given two indices, I and K, the slice is the list containing the
# elements from and including the Ith element up to but not
# including the Kth element of the original list. Start counting
# the elements with 0.

# Example:

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

sub slice_list($s, $e, @list) {
# XXXX: the range operand without enclosing parens should work too!
@list[$s .. ($e-1)]
}
say slice_list(3, 7, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]);
18 changes: 18 additions & 0 deletions spec/examples/99-problems/19.mo
@@ -0,0 +1,18 @@
# P19 (**) Rotate a list N places to the left.
# Examples:

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

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

sub rotate($n, @list) {
my $len = @list.length;
my @r = @list[($n < 0 ? $len + $n : $n) .. ($len - 1)];
@r.push(@list[0 .. ($n < 0 ? $len + $n : $n-1)]);
@r.flatten
}

say rotate(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]);
say rotate(-2, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]);
16 changes: 16 additions & 0 deletions spec/examples/99-problems/20.mo
@@ -0,0 +1,16 @@
# P20 (*) Remove the Kth element from a list.
# Return the list and the removed element in a Tuple. Elements are numbered from 0.

# Example:

# moe> remove_at(1, ['a', 'b', 'c', 'd'])
# [['a', 'c', 'd'],'b']

sub remove_at($i, @list) {
my $removed = @list[$i];
my @rest = @list[0 .. ($i-1)];
@rest.push(@list[($i+1) .. (@list.length-1)]);
[@rest.flatten, $removed]
}

say remove_at(1, ['a', 'b', 'c', 'd']);
13 changes: 13 additions & 0 deletions spec/examples/99-problems/21.mo
@@ -0,0 +1,13 @@
# P21 (*) Insert an element at a given position into a list.
# Example:

# moe> insert_at('new', 1, ['a', 'b', 'c', 'd'])
# ['a', 'new', 'b', 'c', 'd')

sub insert_at($new, $at, @list) {
my @r = @list[0 .. ($at-1)];
@r.push($new, @list[$at .. (@list.length-1)]);
@r.flatten
}

say insert_at('new', 1, ['a', 'b', 'c', 'd'])
10 changes: 10 additions & 0 deletions spec/examples/99-problems/22.mo
@@ -0,0 +1,10 @@
# P22 (*) Create a list containing all integers within a given range.
# Example:

# moe> range(4, 9)
# [4, 5, 6, 7, 8, 9]

sub range($start, $end) { $start .. $end }

say range(4, 9)

0 comments on commit 2217345

Please sign in to comment.