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 169 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Python 2, 47(削除) 47 (削除ここまで) 45 bytes

lambda x,n:int(`x`[1::n+3].translate('12'*128)%(10**n/2)+10**n/9

Thanks to @xnor for golfing off 2 bytes!

Test it on Ideone.

How it works

x`x` yields a string representation of the list x.

For the first test case, this gives the string

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]

[1::n+3] retrieves every (n + 3)th character – where n is the length of x starting with the second one. Accounting 2 characters for , , we retrieve the first digit of the first number, the second digit of the second number, etc.

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]
 ^ ^ ^ ^ ^ ^ ^ ^

translate('12'*128) replaces all 256 bytes with We now take the character innumber modulo '12'*128 at10n ÷ 2 to map the index of its code pointfirst digit in the range [0, 4].

SinceFor 0123456789 have code points93579135, we get 48, ..., 5793579135 % 50000000 = 43579135.

Finally, this replaces odd digits withwe add 2 and even10n ÷ 9 to the last result, which increments – wrapping around from 9 to 0 – all digits withby 11 (no carry) or 2 (with carry).

In the test caseFor 93579135 gets replaced with43579135, we get 2222222243579135 +たす 11111111 = 54690246.

Python 2, 47 bytes

lambda x,n:int(`x`[1::n+3].translate('12'*128))

Test it on Ideone.

How it works

x yields a string representation of the list x.

For the first test case, this gives the string

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]

[1::n+3] retrieves every (n + 3)th character – where n is the length of x starting with the second one. Accounting 2 characters for , , we retrieve the first digit of the first number, the second digit of the second number, etc.

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]
 ^ ^ ^ ^ ^ ^ ^ ^

translate('12'*128) replaces all 256 bytes with the character in '12'*128 at the index of its code point.

Since 0123456789 have code points 48, ..., 57, this replaces odd digits with 2 and even digits with 1.

In the test case 93579135 gets replaced with 22222222.

Python 2, (削除) 47 (削除ここまで) 45 bytes

lambda x,n:int(`x`[1::n+3])%(10**n/2)+10**n/9

Thanks to @xnor for golfing off 2 bytes!

Test it on Ideone.

How it works

`x` yields a string representation of the list x.

For the first test case, this gives the string

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]

[1::n+3] retrieves every (n + 3)th character – where n is the length of x starting with the second one. Accounting 2 characters for , , we retrieve the first digit of the first number, the second digit of the second number, etc.

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]
 ^ ^ ^ ^ ^ ^ ^ ^

We now take the number modulo 10n ÷ 2 to map the first digit in the range [0, 4].

For 93579135, we get 93579135 % 50000000 = 43579135.

Finally, we add 10n ÷ 9 to the last result, which increments – wrapping around from 9 to 0 – all digits by 1 (no carry) or 2 (with carry).

For 43579135, we get 43579135 +たす 11111111 = 54690246.

added 891 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Python 2, 47 bytes

lambda x,n:int(`x`[1::n+3].translate('12'*128))

Maps odd digits toTest it on Ideone .

How it works

x yields a string representation of the list x.

For the first test case, this gives the string

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]

[1::n+3] retrieves every (n + 3)th character – where n is the length of x starting with the second one. Accounting 2 characters for , , we retrieve the first digit of the first number, the second digit of the second number, etc.

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]
 ^ ^ ^ ^ ^ ^ ^ ^

translate('12'*128) replaces all 256 bytes with the character in '12'*128 at the index of its code point.

Since 0123456789 have code points 48, ..., 57, this replaces odd digits with 2 and even digits towith 11. Test it on

In the test case Ideone 93579135 gets replaced with 22222222.

Python 2, 47 bytes

lambda x,n:int(`x`[1::n+3].translate('12'*128))

Maps odd digits to 2 and even digits to 1. Test it on Ideone.

Python 2, 47 bytes

lambda x,n:int(`x`[1::n+3].translate('12'*128))

Test it on Ideone .

How it works

x yields a string representation of the list x.

For the first test case, this gives the string

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]

[1::n+3] retrieves every (n + 3)th character – where n is the length of x starting with the second one. Accounting 2 characters for , , we retrieve the first digit of the first number, the second digit of the second number, etc.

[92345678, 23456789, 34567890, 45678901, 56789012, 67890123, 78901234, 89012345]
 ^ ^ ^ ^ ^ ^ ^ ^

translate('12'*128) replaces all 256 bytes with the character in '12'*128 at the index of its code point.

Since 0123456789 have code points 48, ..., 57, this replaces odd digits with 2 and even digits with 1.

In the test case 93579135 gets replaced with 22222222.

Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Python 2, 47 bytes

lambda x,n:int(`x`[1::n+3].translate('12'*128))

Maps odd digits to 2 and even digits to 1. Test it on Ideone.

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