Skip to content

Instantly share code, notes, and snippets.

@savonarola
Created January 24, 2013 11:50
Show Gist options
  • Save savonarola/4620562 to your computer and use it in GitHub Desktop.
Save savonarola/4620562 to your computer and use it in GitHub Desktop.
Generate all finite sequences of letters (a-z)
def each_str
l = 0
while true
s = 'a' * (l + 1)
while true
yield s
if s[l] == 'z'
i = l
i -= 1 while s[i] == 'z' && i >= 0
if i >= 0
s[i] = (s[i].ord + 1).chr
(i + 1).upto(l) {|ii| s[ii] = 'a' }
i = l
else
break
end
else
s[l] = (s[l].ord + 1).chr
end
end
l += 1
end
end
concat $ map (`replicateM` ['a'..'z']) [1..]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment