Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 351 characters in body
Source Link
Leo
  • 12.9k
  • 1
  • 33
  • 63

Retina, 23(削除) 23 (削除ここまで) 22 bytes

-1 byte thanks to Martin Ender !

(.).
1ドル1ドル$&
.
$*
M`.
0$1$

Try it online! Try it online!

Explanation

Example input: 20378240

(.).
1ドル1ドル$&

Replace each couple of digits with the first digit repeated twice followed by the couple itself. We get 2220333788824440

.
$*

Convert each digit to unary. With parentheses added for clarity, we get (11)(11)(11)()(111)(111)...

M`.

Count the number of matches of the empty string, which is one more than the number of ones in the string. (With the last two steps we have basically taken the sum of each digit +1) Result: 60

0$1$

Match a 01 at the end of the string. This is how we checkWe have multiplied digits by 3 and 1 alternately and summed them, for divisibilitya valid barcode this should be divisible by 10 (last digit 0); but we also added 1 in the last step, so we want the last digit to be 1. Final result: 1.

Retina, 23 bytes

(.).
1ドル1ドル$&
.
$*
M`.
0$

Try it online!

Explanation

Example input: 20378240

(.).
1ドル1ドル$&

Replace each couple of digits with the first digit repeated twice followed by the couple itself. We get 2220333788824440

.
$*

Convert each digit to unary. With parentheses added for clarity, we get (11)(11)(11)()(111)(111)...

M`.

Count the number of ones. (With the last two steps we have basically taken the sum of each digit) Result: 60

0$

Match a 0 at the end of the string. This is how we check for divisibility by 10. Final result: 1.

Retina, (削除) 23 (削除ここまで) 22 bytes

-1 byte thanks to Martin Ender !

(.).
1ドル1ドル$&
.
$*
M`
1$

Try it online!

Explanation

Example input: 20378240

(.).
1ドル1ドル$&

Replace each couple of digits with the first digit repeated twice followed by the couple itself. We get 2220333788824440

.
$*

Convert each digit to unary. With parentheses added for clarity, we get (11)(11)(11)()(111)(111)...

M`

Count the number of matches of the empty string, which is one more than the number of ones in the string. (With the last two steps we have basically taken the sum of each digit +1) Result: 60

1$

Match a 1 at the end of the string. We have multiplied digits by 3 and 1 alternately and summed them, for a valid barcode this should be divisible by 10 (last digit 0); but we also added 1 in the last step, so we want the last digit to be 1. Final result: 1.

Source Link
Leo
  • 12.9k
  • 1
  • 33
  • 63

Retina, 23 bytes

(.).
1ドル1ドル$&
.
$*
M`.
0$

Try it online!

Explanation

Example input: 20378240

(.).
1ドル1ドル$&

Replace each couple of digits with the first digit repeated twice followed by the couple itself. We get 2220333788824440

.
$*

Convert each digit to unary. With parentheses added for clarity, we get (11)(11)(11)()(111)(111)...

M`.

Count the number of ones. (With the last two steps we have basically taken the sum of each digit) Result: 60

0$

Match a 0 at the end of the string. This is how we check for divisibility by 10. Final result: 1.

AltStyle によって変換されたページ (->オリジナル) /