Created
September 8, 2016 08:15
-
-
Save zechtz/08316295f338348cde0d79013249bd61 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
``` | |
def add_uniq number | |
# convert to a character array | |
# return only unique charachars to a new array | |
# looop through each and add | |
new_number = number.to_s.chars | |
unique = new_number.uniq | |
sum = 0 | |
unique.each do |x| | |
sum += x.to_i | |
end | |
sum | |
end | |
p add_uniq 230323 | |
# receives a number | |
# returns it in words, eg 235 => two three four | |
def number_to_words number | |
new_number = number.to_s.chars | |
message = [] | |
new_number.each do |n| | |
message << get_word(n.to_i) | |
end | |
message * " " | |
end | |
def get_word(value) | |
dictionary = { | |
0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six', 7 => 'seven', | |
8 => 'eight', 9 => 'nine' } | |
return dictionary[value] | |
end | |
p number_to_words 123 | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment