Created
June 10, 2010 00:23
-
-
Save vaskoz/432396 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 luhn?(ccn) | |
ccn = ccn.split(//).map! {|x| x.to_i} | |
1.upto(ccn.size/2) do |i| | |
n = ccn[-i*2] * 2 | |
ccn[-i*2] = n < 10 ? n : (n/10) + (n%10) | |
end | |
ccn.inject(0) {|sum, x| sum += x} % 10 == 0 | |
end | |
puts "Luhn Data" | |
puts luhn?("4111111111111111") | |
puts luhn?("4111111111111112") | |
puts luhn?("5105105105105100") | |
puts luhn?("4222222222222") | |
def ccexp?(s) | |
parts = s.split('/').map! {|x| x.to_i} | |
parts[1] -= 2000 if parts[1] > 100 # shouldn't need this. | |
(parts.last*12 + parts.first - 1) <= (12*10 + 5) | |
end | |
puts "Credit Card Expire" | |
puts ccexp?("01/2000") | |
puts ccexp?("1/2000") | |
puts ccexp?("06/2010") | |
puts ccexp?("07/2010") | |
puts ccexp?("05/2015") | |
def mask(s) | |
s[6..-5] = '*' * (s.size-10) | |
s | |
end | |
puts "Masking" | |
puts mask("4111111111111111") | |
puts mask("512351235123") | |
def fibsum(n) | |
return n if n <= 2 | |
two = [1, 1] | |
sum = 2 | |
3.upto(n) do | |
two[0], two[1] = two[1], two[0] + two[1] | |
sum += two[1] | |
end | |
sum | |
end | |
puts "Fib Sum solution" | |
puts fibsum(1) | |
puts fibsum(2) | |
puts fibsum(3) | |
puts fibsum(4) | |
puts fibsum(5) | |
puts fibsum(6) | |
puts fibsum(7) | |
puts fibsum(25) | |
def morse(s) | |
morse_dictionary = { | |
"a" => ".-", | |
"b" => "-...", | |
"c" => "-.-.", | |
"d" => "-..", | |
"e" => ".", | |
"f" => "..-.", | |
"g" => "--.", | |
"h" => "....", | |
"i" => "..", | |
"j" => ".---", | |
"k" => "-.-", | |
"l" => ".-..", | |
"m" => "--", | |
"n" => "-.", | |
"o" => "---", | |
"p" => ".--.", | |
"q" => "--.-", | |
"r" => ".-.", | |
"s" => "...", | |
"t" => "-", | |
"u" => "..-", | |
"v" => "...-", | |
"w" => ".--", | |
"x" => "-..-", | |
"y" => "-.--", | |
"z" => "--..", | |
" " => " " | |
} | |
s.downcase! | |
result = "" | |
1.upto(s.size) do |i| | |
result << morse_dictionary[s[i-1, 1]] | |
end | |
result | |
end | |
puts "Morse Code problem" | |
puts morse("a") | |
puts morse("B") | |
puts morse("Hello World") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment