Last active
December 16, 2015 09:59
-
-
Save caironoleto/5417306 to your computer and use it in GitHub Desktop.
Pis Pasep Generator
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
#!/bin/ruby | |
class PisPasepGenerator | |
INDEX = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2] | |
def self.generate! | |
numbers = [] | |
10.times { numbers << rand(10) } | |
calculated = numbers.each_with_index { |n, i| PisPasepGenerator::INDEX[i] * n }.inject(:+) | |
rest = calculated % 11 | |
calculated = 11 - rest | |
if calculated == 10 || calculated == 11 | |
numbers << 0 | |
else | |
numbers << calculated | |
end | |
numbers.join | |
end | |
end | |
PisPasepGenerator.generate! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment