Created
May 20, 2015 18:32
-
-
Save mayfer/49da7d6df6116b681a7e 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
# no *, / or % allowed | |
def divide(number, divisor) | |
result = 0 | |
while number >= divisor | |
number -= divisor | |
result += 1 | |
end | |
remainder = number | |
return result, remainder | |
end | |
def long_division(number, divisor) | |
result_digits = [] | |
i = 0 | |
temp_number = number.to_s[i..i+divisor.to_s.length].to_i | |
while i < number.to_s.length - divisor.to_s.length | |
temp_result, temp_remainder = divide(temp_number, divisor) | |
puts "Dividing #{temp_number} with #{divisor}, got #{temp_result} with #{temp_remainder}" | |
result_digits << temp_result | |
temp_number = "#{temp_remainder.to_s}#{number.to_s[1+i]}".to_i | |
puts "#{result_digits.inspect}" | |
i += 1 | |
end | |
puts "Result #{result_digits.join('')}" | |
puts "Remainder #{temp_remainder}" | |
puts "#{i}" | |
end | |
long_division(4000000000, 7) |
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
require 'pry' | |
require 'pp' | |
$found = false | |
def word_made_of(word, units, trie=nil) | |
if trie == nil | |
trie = {} | |
end | |
units.each_with_index do |unit, index| | |
if unit && word.start_with?(unit) | |
new_word = word[unit.length..-1] | |
puts "#{word}, #{new_word}" | |
units[index] = nil | |
if new_word.length == 0 | |
trie[unit] = true | |
$found = true | |
else | |
trie[unit] = word_made_of(new_word, units, trie[unit]) | |
end | |
end | |
end | |
return trie | |
end | |
units = ["re", "lia", "tea", "tal", "ate"] | |
word = "retaliate" | |
puts word_made_of(word, units) | |
puts $found |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment