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 #103 from MoeOrganization/prakashk/misc-stuff-2
Various things (sans the REPL-scope commit)
- Loading branch information
Showing
21 changed files
with
2,490 additions
and
43 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
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
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,54 @@ | ||
# P26 (**) Generate the combinations of K distinct objects chosen from the N elements of a list. | ||
|
||
# In how many ways can a committee of 3 be chosen from a group | ||
# of 12 people? We all know that there are C(12,3) = 220 | ||
# possibilities (C(N,K) denotes the well-known binomial | ||
# coefficient). For pure mathematicians, this result may be | ||
# great. But we want to really generate all the possibilities. | ||
|
||
# Example: | ||
|
||
# moe> combinations(3, ['a', 'b', 'c', 'd', 'e', 'f']) | ||
# [['a', 'b', 'c'], ['a', 'b', 'd'], ['a', 'b', 'e'], ...] | ||
|
||
use Test::More; | ||
|
||
sub concat(@a, @b) { | ||
@b.each(-> ($x) { @a.push($x) }); | ||
@a | ||
} | ||
|
||
sub combinations($n, @list) { | ||
if ($n == 1) { | ||
@list.map(-> ($x) { [$x] }); | ||
} | ||
elsif (@list.length <= $n) { | ||
[@list]; | ||
} | ||
else { | ||
concat( | ||
combinations($n - 1, @list.tail) | ||
.map( | ||
-> (@c) { | ||
[@list.head, @c].flatten | ||
} | ||
), | ||
combinations($n, @list.tail) | ||
) | ||
} | ||
} | ||
|
||
ok(combinations(3, ['a', 'b', 'c', 'd', 'e', 'f']) | ||
.eqv([["a", "b", "c"], ["a", "b", "d"], ["a", "b", "e"], ["a", "b", "f"], | ||
["a", "c", "d"], ["a", "c", "e"], ["a", "c", "f"], | ||
["a", "d", "e"], ["a", "d", "f"], | ||
["a", "e", "f"], | ||
["b", "c", "d"], ["b", "c", "e"], ["b", "c", "f"], | ||
["b", "d", "e"], ["b", "d", "f"], | ||
["b", "e", "f"], | ||
["c", "d", "e"], ["c", "d", "f"], | ||
["c", "e", "f"], | ||
["d", "e", "f"]]), | ||
"... P26"); | ||
|
||
done_testing(); |
Oops, something went wrong.