Created
July 26, 2012 13:05
-
-
Save MikeHibbert/3181935 to your computer and use it in GitHub Desktop.
... And in the darkness bind them !
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
#!/usr/bin/env ruby | |
def is_valid_isbn13?(isbn13) | |
sum = 0 | |
13.times { |i| sum += i.modulo(2)==0 ? isbn13[i].to_i : isbn13[i].to_i*3 } | |
0 == sum.modulo(10) | |
end | |
def isbn13_checksum(isbn12) | |
sum = 0 | |
12.times { |i| sum += i.modulo(2)==0 ? isbn12[i].to_i : isbn12[i].to_i*3 } | |
10 - sum.modulo(10) | |
end | |
isbn_numbers = [] | |
f = File.new('isbns', 'r') | |
while (line = f.gets) | |
line.delete!(' ') | |
line.delete!('-') | |
line.chop! | |
if is_valid_isbn13?(line) | |
if isbn13_checksum(line) | |
isbn_numbers << line | |
end | |
end | |
puts line.size | |
end | |
puts isbn_numbers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment