Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from MoeOrganization/prakashk/array-methods
few more array methods
- Loading branch information
Showing
9 changed files
with
324 additions
and
3 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
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
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
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
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,25 @@ | ||
package org.moe.parser | ||
|
||
import org.scalatest.FunSuite | ||
import org.scalatest.BeforeAndAfter | ||
import org.scalatest.matchers.ShouldMatchers | ||
|
||
import org.moe.runtime._ | ||
import org.moe.runtime.nativeobjects._ | ||
import org.moe.interpreter._ | ||
import org.moe.ast._ | ||
import org.moe.parser._ | ||
|
||
class BuiltinTestSuite extends FunSuite with BeforeAndAfter with ParserTestUtils with ShouldMatchers { | ||
test("... rand -- with no argument") { | ||
val result = interpretCode("rand()").unboxToDouble.get | ||
result should be >= (0.0) | ||
result should be < (1.0) | ||
} | ||
|
||
test("... rand -- with a limit argument") { | ||
val result = interpretCode("rand(10)").unboxToDouble.get | ||
result should be >= (0.0) | ||
result should be < (10.0) | ||
} | ||
} |
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
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,37 @@ | ||
# P23 (**) Extract a given number of randomly selected elements from a list. | ||
# Example: | ||
|
||
# moe> random_select(3, ['a', 'b', 'c', 'd', 'f', 'g', 'h']) | ||
# ['e', 'd', 'a'] | ||
|
||
# Note: the selection is done without replacement; there should be no | ||
# duplicates in output | ||
|
||
use Test::More; | ||
|
||
# from P20 | ||
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] | ||
} | ||
|
||
sub random_select($n, @list) { | ||
my @l = @list; | ||
1..$n.map(-> { | ||
my @result = remove_at(rand(@l.length).Int, @l); | ||
@l = @result[0]; | ||
@result[1] | ||
}) | ||
} | ||
|
||
my @picks = random_select(3, ['a', 'b', 'c', 'd', 'f', 'g', 'h']); | ||
|
||
# can't really test for specific values in the result | ||
is(@picks.length, 3, "... P23"); | ||
|
||
# check for duplicates | ||
ok(@picks.uniq.length == @picks.length, "... P23 -- no duplicates"); | ||
|
||
done_testing(); |
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,33 @@ | ||
# P24 (*) Lotto: Draw N different random numbers from the set 1..M. | ||
# Example: | ||
|
||
# moe> lotto(6, 49) | ||
# [23, 1, 17, 33, 21, 37] | ||
|
||
use Test::More; | ||
|
||
# from P20 | ||
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] | ||
} | ||
|
||
# from P23 | ||
sub random_select($n, @list) { | ||
my @l = @list; | ||
1..$n.map(-> {my @result = remove_at(rand(@l.length).Int, @l); @l = @result[0]; @result[1]}) | ||
} | ||
|
||
sub lotto($n, $m) { random_select($n, 1..$m) } | ||
|
||
my @picks = lotto(6, 49); | ||
say @picks; | ||
|
||
is(@picks.length, 6, "... P24 -- number of picks"); | ||
ok(@picks.uniq.length == @picks.length, "... P24 -- no duplicates"); | ||
0..5.each(-> ($i) { ok(@picks[$i] > 0 && @picks[$i] <= 49, "... P24 -- pick " ~ ~($i + 1)) }); | ||
|
||
done_testing(); | ||
|
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,39 @@ | ||
# P25 (*) Generate a random permutation of the elements of a list. | ||
# Hint: Use the solution of problem P23. | ||
|
||
# Example: | ||
|
||
# moe> random_permute(['a', 'b', 'c', 'd', 'e', 'f']) | ||
# ['b', 'a', 'd', 'c', 'e', 'f'] | ||
|
||
use Test::More; | ||
|
||
# from P20 | ||
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] | ||
} | ||
|
||
sub random_permute(@list) { | ||
my @l = @list; | ||
1..(@l.length).map(-> { | ||
if (@l.length > 1) { | ||
my @result = remove_at(rand(@l.length).Int, @l); | ||
@l = @result[0]; | ||
@result[1] | ||
} | ||
else { | ||
@l[0] | ||
} | ||
}) | ||
} | ||
|
||
my @list = ['a', 'b', 'c', 'd', 'e', 'f']; | ||
my @permuted = random_permute(@list); | ||
|
||
is(@permuted.length, @list.length, "... P25"); | ||
@permuted.each(-> ($x) { ok(@list.exists($x), "... P25 -- exists " ~ $x.Str) }); | ||
|
||
done_testing(); |