UPC: Difference between revisions
m
calculate functions only once
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: less cryptic regex) |
m (calculate functions only once) |
||
Line 252:
=={{header|Julia}}==
{{trans|Perl 6}}
<lang julia>const pat1 = (" ## #", " ## #", " # ##", " #### #", " # ##",
<lang julia>function decodeUPC(line)▼
const pat2 = [replace(x, r"[# ]" => (x) -> x
const
"){6})\\s*# #\\s*((?:" * join(pat2, "|") * "){6})\\s*# #\\s*")▼
▲ ptod2 = Dict((b => a - 1) for (a, b) in enumerate(pat2))
const lines = [▼
" # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # ",▼
▲ reg = Regex("^\\s*# #\\s*((?:" * join(pat1, "|") *
" # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # # ",▼
▲ "){6})\\s*# #\\s*((?:" * join(pat2, "|") * "){6})\\s*# #\\s*")
" # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # # ",▼
" # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # # ",▼
" # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # # ",▼
" # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # # ",▼
" # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # # ",▼
" # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # # ",▼
" # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # ",▼
" # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # # ",▼
]▼
if (m = match(reg, line)) != nothing
mats, dig
for mat in mats
append!(dig, [ptod1[x.match] for x in eachmatch(r"(.......)", mat)
Line 271 ⟶ 281:
if haskey(ptod2, x.match)])
end
dsum
(dsum % 10 == 0) && return prod(string.(dig))
end
return ""
end
▲const lines = [
▲ " # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # ",
▲ " # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # # ",
▲ " # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # # ",
▲ " # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # # ",
▲ " # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # # ",
▲" # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # # ",
▲ " # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # # ",
▲ " # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # # ",
▲ " # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # ",
▲ " # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # # ",
▲]
for line in lines
|