Created
July 22, 2010 00:19
-
-
Save simplesessions/485372 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
# Solution to the following: | |
# Given a dictionary, output all word pairs where both words share all their letters except the last two, which are distinct and reversed. | |
# | |
# Notes: | |
# - Use a reasonable dictionary of your choice | |
# - Potential word pairs are: "ear, era" ; "revies, revise" ; "burglaries, burglarise" | |
# - "shear, era" is not a valid pair because "she" != "e" | |
# - "bell, bell" is not a valid pair because the last two letters are not distinct | |
# - This will be benchmarked | |
# - Work on this problem in C, C++, Java, Perl, PHP, Python or a similar language. | |
# - Don't e-mail to ask for clarifications on this question. | |
# Make reasonable assumptions and justify them in a README if you feel the need | |
time = Time.now.to_f | |
dictionary, results = [], [] | |
# copy the contents of the file to our dictionary | |
File.open("dictionary.txt") do |file| | |
while line = file.gets | |
dictionary << line.gsub(/(\n)/, '') | |
end | |
end | |
dictionary.uniq! | |
# compile our word pairs from the dictionary | |
while curr = dictionary.delete_at(0) do | |
next unless curr.size >= 2 && curr[-1] != curr[-2] | |
search = curr[0...-2] + curr[-2..-1].reverse | |
results << (curr + ', ' + search) if dictionary.delete(search) | |
end | |
time = Time.now.to_f - time | |
puts results | |
puts "Found #{results.size} pairs in #{time.to_s} seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment