UPC: Difference between revisions
Content added Content deleted
m (added two commas.) |
|||
Line 800: | Line 800: | ||
653483540435 |
653483540435 |
||
</pre> |
</pre> |
||
=={{header|Kotlin}}== |
|||
{{trans|C}} |
|||
<lang scala>val LEFT_DIGITS = mapOf( |
|||
" ## #" to 0, |
|||
" ## #" to 1, |
|||
" # ##" to 2, |
|||
" #### #" to 3, |
|||
" # ##" to 4, |
|||
" ## #" to 5, |
|||
" # ####" to 6, |
|||
" ### ##" to 7, |
|||
" ## ###" to 8, |
|||
" # ##" to 9 |
|||
) |
|||
val RIGHT_DIGITS = LEFT_DIGITS.mapKeys { |
|||
it.key.replace(' ', 's').replace('#', ' ').replace('s', '#') |
|||
} |
|||
const val END_SENTINEL = "# #" |
|||
const val MID_SENTINEL = " # # " |
|||
fun decodeUPC(input: String) { |
|||
fun decode(candidate: String): Pair<Boolean, List<Int>> { |
|||
var pos = 0 |
|||
var part = candidate.slice(pos until pos + END_SENTINEL.length) |
|||
if (part == END_SENTINEL) { |
|||
pos += END_SENTINEL.length |
|||
} else { |
|||
return Pair(false, emptyList()) |
|||
} |
|||
val output = mutableListOf<Int>() |
|||
for (i in 0 until 6) { |
|||
part = candidate.slice(pos until pos + 7) |
|||
pos += 7 |
|||
if (LEFT_DIGITS.containsKey(part)) { |
|||
output.add(LEFT_DIGITS.getOrDefault(part, -1)) |
|||
} else { |
|||
return Pair(false, output.toList()) |
|||
} |
|||
} |
|||
part = candidate.slice(pos until pos + MID_SENTINEL.length) |
|||
if (part == MID_SENTINEL) { |
|||
pos += MID_SENTINEL.length |
|||
} else { |
|||
return Pair(false, output.toList()) |
|||
} |
|||
for (i in 0 until 6) { |
|||
part = candidate.slice(pos until pos + 7) |
|||
pos += 7 |
|||
if (RIGHT_DIGITS.containsKey(part)) { |
|||
output.add(RIGHT_DIGITS.getOrDefault(part, -1)) |
|||
} else { |
|||
return Pair(false, output.toList()) |
|||
} |
|||
} |
|||
part = candidate.slice(pos until pos + END_SENTINEL.length) |
|||
if (part == END_SENTINEL) { |
|||
pos += END_SENTINEL.length |
|||
} else { |
|||
return Pair(false, output.toList()) |
|||
} |
|||
val sum = output.mapIndexed { i, v -> if (i % 2 == 0) v * 3 else v }.sum() |
|||
return Pair(sum % 10 == 0, output.toList()) |
|||
} |
|||
val candidate = input.trim() |
|||
var out = decode(candidate) |
|||
if (out.first) { |
|||
println(out.second) |
|||
} else { |
|||
out = decode(candidate.reversed()) |
|||
if (out.first) { |
|||
print(out.second) |
|||
println(" Upside down") |
|||
} else { |
|||
if (out.second.size == 12) { |
|||
println("Invalid checksum") |
|||
} else { |
|||
println("Invalid digit(s)") |
|||
} |
|||
} |
|||
} |
|||
} |
|||
fun main() { |
|||
val barcodes = listOf( |
|||
" # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # ", |
|||
" # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # # ", |
|||
" # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # # ", |
|||
" # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # # ", |
|||
" # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # # ", |
|||
" # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # # ", |
|||
" # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # # ", |
|||
" # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # # ", |
|||
" # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # ", |
|||
" # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # # ", |
|||
) |
|||
for (barcode in barcodes) { |
|||
decodeUPC(barcode) |
|||
} |
|||
}</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 digit(s) |
|||
[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|Perl}}== |
=={{header|Perl}}== |