Damm algorithm: Difference between revisions

→‎{{header|Ruby}}: made Ruby example more like other examples (test return boolean) and more idiomatic to Ruby (function name, brackets, single statement trailing if)
(→‎{{header|Ruby}}: made Ruby example more like other examples (test return boolean) and more idiomatic to Ruby (function name, brackets, single statement trailing if))
Line 1,996:
 
=={{header|Ruby}}==
<lang ruby>def dammCheck(damm_valid? nbr )
idx = 0
for i in 0 .. nbr.length - 1
a = nbr[i].to_i
return false if a == nil then return false end
idx = @table[idx][a]
end
idx == 0
print( "this number is " )
if idx == 0; print( "valid!" )
else print( "invalid!" )
end
puts
end
 
@table = Array.new( [
[9,40,3,1,7,5,9,8,6,1,74,2,0,5], [27,50,89,2,1,5,4,38,6,73],9,0]
[
[4,2,0,36,18,7,1,3,5,9,8,6,4,2], [1,7,5,0,9,28,1,53,4,82,6,3],
[46,1,2,3,0,64,85,9,7,1,3,5,98], [13,6,7,54,2,0,9,5,8,3,4,2,61],
[5,8,6,19,7,2,0,1,3,0,4],5 [8,9,74,8]5, [3,6,7,4,2,0,91,5,8,17],
[59,4,3,8,6,91,7,2,0,1,3,45], [2,5,8,91,4,5,3,6,27,9,0,1,7],
]
[9,4,3,8,6,1,7,2,0,5], [2,5,8,1,4,3,6,7,9,0]
]
)
 
while true
print( "Number to check: " )
dammCheck(if damm_valid? gets.chomp )
print( puts "this number is valid!" )
putselse
puts "this number is invalid!"
end
end</lang>
{{out}}<pre>Number to check: 5724