Created
April 10, 2010 04:50
-
-
Save benatkin/361826 to your computer and use it in GitHub Desktop.
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
# I noticed that github's 404 page splits words, for example http://github.com/bitternessflavoraroma | |
# I wondered how I might do it myself. This is slow, but it works with my simple example on my machine. | |
$words = File.new('/usr/share/dict/words').readlines.map { |s| s.strip }.reject { |s| s.length < 2 } | |
$answers = [] | |
def split_words(str, words=[]) | |
$answers << words if str == '' | |
$words.each do |word| | |
split_words(str.sub(word, ''), words.clone << word) if str.start_with?(word) | |
end | |
end | |
def average_length(list) | |
list.map { |s| s.length }.inject(0) { |a, b| a + b } / (list.length * 1.0) | |
end | |
str = ARGV[0] | |
split_words(str) | |
highest_average_length = 0.0 | |
pick = [str] | |
$answers.each do |match| | |
if (average_length = average_length(match)) > highest_average_length | |
highest_average_length = average_length | |
pick = match | |
end | |
end | |
puts pick.join ' ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment