Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
few more problems from "99 problems" series.
- Loading branch information
Showing
6 changed files
with
88 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,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"]); | ||
|
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,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"]); |
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,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"]); |
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,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']); |
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,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']) |
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,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) | ||
|