UPC: Difference between revisions

2,928 bytes added ,  4 years ago
m (→‎{{header|zkl}}: mouse nuts)
Line 303:
706466743030
653483540435</pre>
 
=={{header|Phix}}==
<lang Phix>constant numbers = {" ## #", -- 0
" ## #", -- 1
" # ##", -- 2
" #### #", -- 3
" # ##", -- 4
" ## #", -- 5
" # ####", -- 6
" ### ##", -- 7
" ## ###", -- 8
" # ##"} -- 9
 
procedure decode(string bar_code)
bar_code = trim(bar_code)
if length(bar_code)=95
and bar_code[1..3]="# #"
and bar_code[46..50]=" # # "
and bar_code[93..95]="# #" then
for reversed=false to true do
sequence r = {}
for i=1 to 12 do
integer st = iff(i<=6?i*7-3:i*7+2)
string number = bar_code[st..st+6]
if i>6 then number = substitute_all(number," #X","X #") end if
r &= find(number,numbers)-1
end for
if not find(-1,r) then
if remainder(sum(sq_mul(r,{3,1,3,1,3,1,3,1,3,1,3,1})),10) then
printf(1,"invalid checksum\n")
else
printf(1,"%v%s\n",{r,iff(reversed?" (upside down)","")})
end if
return
end if
bar_code = reverse(bar_code)
end for
end if
printf(1,"invalid\n")
end procedure
 
constant bar_codes = split("""
# # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # #
# # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # #
# # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # #
# # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # #
# # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # #
# # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # #
# # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # #
# # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # #
# # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # #
# # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # #
""","\n",true)
for i=1 to length(bar_codes) do
decode(bar_codes[i])
end for</lang>
{{out}}
<pre>
{9,2,4,7,7,3,2,7,1,0,1,9}
{4,0,3,9,4,4,4,4,1,0,5,0}
{8,3,4,9,9,9,6,7,6,7,0,6} (upside down)
{9,3,9,8,2,5,1,5,8,8,1,1} (upside down)
invalid
{3,1,6,3,1,3,7,1,8,7,1,7} (upside down)
{2,1,4,5,7,5,8,7,5,6,0,8}
{8,1,8,7,7,8,8,4,1,8,1,3} (upside down)
{7,0,6,4,6,6,7,4,3,0,3,0}
{6,5,3,4,8,3,5,4,0,4,3,5}
</pre>
 
=={{header|zkl}}==
7,794

edits