Created
October 22, 2015 19:44
-
-
Save mayfer/dbdb41050918636324a3 to your computer and use it in GitHub Desktop.
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
file = File.open('/usr/share/dict/words').read | |
lines = file.lines | |
$words = lines.map do |word| | |
word.strip.downcase | |
end | |
def is_anagram(word, word2) | |
word.split('').sort.join == word2.split('').sort.join | |
end | |
def find_longest_anagram(words) | |
longest = 0 | |
longest_word = nil | |
words.each do |word| | |
words.each do |word2| | |
if is_anagram(word, word2) | |
if word.length > longest | |
longest_word = word | |
longest = word.length | |
end | |
end | |
end | |
end | |
gets | |
longest_word | |
end | |
# linear!! | |
def find_longest_anagram(words) | |
longest = 0 | |
longest_signature = nil | |
anagrams = Hash.new { [] } | |
words.each do |word| | |
signature = word.split('').sort.join | |
anagrams[signature] <<= word | |
if word.length > longest && anagrams[signature].length > 1 | |
longest = word.length | |
longest_signature = signature | |
end | |
end | |
gets | |
anagrams[longest_signature] | |
end | |
start = Time.now | |
puts find_longest_anagram($words).inspect | |
finish = Time.now | |
puts "Took #{finish - start} seconds" | |
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
# integer division | |
# WITHOUT using * or / operators (and no % either) | |
# return result, remainder | |
def divide(number, divisor) | |
result = 0 | |
while number > divisor | |
result += max(1, result) | |
divisor += divisor | |
number -= divisor | |
end | |
remainder = number | |
return result, remainder | |
end | |
start = Time.now | |
puts divide(110000000, 3) | |
finish = Time.now | |
puts "Took #{finish - start} seconds" |
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
file = File.open('/usr/share/dict/words').read | |
lines = file.lines | |
$words = lines.map do |word| | |
word.strip.downcase | |
end | |
$words = ["asd", "ertert", "k4l534lk5"] | |
def spellcheck_slow(word) | |
# true if in dictionary | |
# false if not | |
iter = 0 | |
$words.each do |dict_word| | |
if word == dict_word | |
puts "#{iter} iterations" | |
return true | |
end | |
iter += 1 | |
end | |
puts "#{iter} iterations" | |
return false | |
end | |
def spellcheck(word, words) | |
$iter += 1 | |
word = word.downcase | |
middle = words.length / 2 | |
if words[middle] == word | |
return true | |
elsif words[middle] != word && words.length == 1 | |
return false | |
elsif words[middle] > word | |
# look in the first half | |
words = words[0..middle] | |
return spellcheck(word, words) | |
elsif words[middle] < word | |
# look in the bottom half | |
words = words[middle..-1] | |
return spellcheck(word, words) | |
end | |
end | |
start = Time.now | |
spellcheck_slow("paroxysm") | |
finish = Time.now | |
puts "Took #{finish - start} seconds" | |
$iter = 0 | |
start = Time.now | |
spellcheck("paroxysm", $words) | |
finish = Time.now | |
puts "#{$iter} iterations" | |
puts "Took #{finish - start} seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment