Last active
August 29, 2015 14:06
-
-
Save cboursnell/23fbb81791070f923e21 to your computer and use it in GitHub Desktop.
Recursive segmenter
This file contains hidden or 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
def segment seq, k, path | |
if k==0 | |
path << seq | |
p path | |
# puts "k==0\t#{seq}" | |
else | |
(0..seq.length-k-1).each do |i| | |
# puts "seq = #{seq}, k == #{k}, i == #{i}" | |
pa = path.clone | |
left = seq[0..i] | |
pa << left | |
right = seq[i+1..seq.length-1] | |
# puts "left: #{left}\tright: #{right}" | |
segment(right, k-1, pa) | |
end | |
end | |
end | |
a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
segment(a, 4, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment