UPC: Difference between revisions
Content added Content deleted
Line 1,090: | Line 1,090: | ||
bad len # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # |
bad len # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # |
||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
|||
{{trans|C}} |
|||
<lang ruby>DIGIT_F = { |
|||
" ## #" => 0, |
|||
" ## #" => 1, |
|||
" # ##" => 2, |
|||
" #### #" => 3, |
|||
" # ##" => 4, |
|||
" ## #" => 5, |
|||
" # ####" => 6, |
|||
" ### ##" => 7, |
|||
" ## ###" => 8, |
|||
" # ##" => 9, |
|||
} |
|||
DIGIT_R = { |
|||
"### # " => 0, |
|||
"## ## " => 1, |
|||
"## ## " => 2, |
|||
"# # " => 3, |
|||
"# ### " => 4, |
|||
"# ### " => 5, |
|||
"# # " => 6, |
|||
"# # " => 7, |
|||
"# # " => 8, |
|||
"### # " => 9, |
|||
} |
|||
END_SENTINEL = "# #" |
|||
MID_SENTINEL = " # # " |
|||
def decode_upc(s) |
|||
def decode_upc_impl(input) |
|||
upc = input.strip |
|||
if upc.length != 95 then |
|||
return false |
|||
end |
|||
pos = 0 |
|||
digits = [] |
|||
sum = 0 |
|||
# end sentinel |
|||
if upc[pos .. pos + 2] == END_SENTINEL then |
|||
pos += 3 |
|||
else |
|||
return false |
|||
end |
|||
# 6 left hand digits |
|||
for i in 0 .. 5 |
|||
digit = DIGIT_F[upc[pos .. pos + 6]] |
|||
if digit == nil then |
|||
return false |
|||
else |
|||
digits.push(digit) |
|||
sum += digit * [1, 3][digits.length % 2] |
|||
pos += 7 |
|||
end |
|||
end |
|||
# mid sentinel |
|||
if upc[pos .. pos + 4] == MID_SENTINEL then |
|||
pos += 5 |
|||
else |
|||
return false |
|||
end |
|||
# 6 right hand digits |
|||
for i in 0 .. 5 |
|||
digit = DIGIT_R[upc[pos .. pos + 6]] |
|||
if digit == nil then |
|||
return false |
|||
else |
|||
digits.push(digit) |
|||
sum += digit * [1, 3][digits.length % 2] |
|||
pos += 7 |
|||
end |
|||
end |
|||
# end sentinel |
|||
if upc[pos .. pos + 2] == END_SENTINEL then |
|||
pos += 3 |
|||
else |
|||
return false |
|||
end |
|||
if sum % 10 == 0 then |
|||
print digits, " " |
|||
return true |
|||
else |
|||
print "Failed Checksum " |
|||
return false |
|||
end |
|||
end |
|||
if decode_upc_impl(s) then |
|||
puts "Rightside Up" |
|||
elsif decode_upc_impl(s.reverse) then |
|||
puts "Upside Down" |
|||
else |
|||
puts "Invalid digit(s)" |
|||
end |
|||
end |
|||
def main |
|||
num = 0 |
|||
print "%2d: " % [num += 1] |
|||
decode_upc(" # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # ") |
|||
print "%2d: " % [num += 1] |
|||
decode_upc(" # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # # ") |
|||
print "%2d: " % [num += 1] |
|||
decode_upc(" # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # # ") |
|||
print "%2d: " % [num += 1] |
|||
decode_upc(" # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # # ") |
|||
print "%2d: " % [num += 1] |
|||
decode_upc(" # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # # ") |
|||
print "%2d: " % [num += 1] |
|||
decode_upc(" # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # # ") |
|||
print "%2d: " % [num += 1] |
|||
decode_upc(" # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # # ") |
|||
print "%2d: " % [num += 1] |
|||
decode_upc(" # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # # ") |
|||
print "%2d: " % [num += 1] |
|||
decode_upc(" # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # ") |
|||
print "%2d: " % [num += 1] |
|||
decode_upc(" # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # # ") |
|||
end |
|||
main()</lang> |
|||
{{out}} |
|||
<pre> 1: [9, 2, 4, 7, 7, 3, 2, 7, 1, 0, 1, 9] Rightside Up |
|||
2: [4, 0, 3, 9, 4, 4, 4, 4, 1, 0, 5, 0] Rightside Up |
|||
3: [8, 3, 4, 9, 9, 9, 6, 7, 6, 7, 0, 6] Upside Down |
|||
4: [9, 3, 9, 8, 2, 5, 1, 5, 8, 8, 1, 1] Upside Down |
|||
5: Invalid digit(s) |
|||
6: [3, 1, 6, 3, 1, 3, 7, 1, 8, 7, 1, 7] Upside Down |
|||
7: [2, 1, 4, 5, 7, 5, 8, 7, 5, 6, 0, 8] Rightside Up |
|||
8: [8, 1, 8, 7, 7, 8, 8, 4, 1, 8, 1, 3] Upside Down |
|||
9: [7, 0, 6, 4, 6, 6, 7, 4, 3, 0, 3, 0] Rightside Up |
|||
10: [6, 5, 3, 4, 8, 3, 5, 4, 0, 4, 3, 5] Rightside Up</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |