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

deleted 6 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Python 3, 39 bytes

lambda n,m:int(((n*m)[-m:]+n)[:len(n)])

Try it online! Or see the test-suite.

How?

Rotating n right by m is the same as rotating n right by m modulo length n (m%len(n)), which is the concatenation of the last m%len(n) digits with the first len(n)-m%len(n) digits.

A simple slice would give us

lambda n,m:int(n[-m%len(n):]+n[:-m%len(n)])

for 43 bytes. To remove the need for the repeated -m%len(n)m% we can instead concatenate the last m%len(n) digits with all the digits of n and then take the first len(n) digits. This is

lambda n,m:int((n[-m%len(n):]+n)[:len(n)])

for 42 bytes. The n[-m%len(n):] can then be replaced with taking the rightmost m digits of m ns concatenated together, (n*m)[-m:] giving us the 39 byte solution.

Python 3, 39 bytes

lambda n,m:int(((n*m)[-m:]+n)[:len(n)])

Try it online! Or see the test-suite.

How?

Rotating n right by m is the same as rotating n right by m modulo length n (m%len(n)), which is the concatenation of the last m%len(n) digits with the first len(n)-m%len(n) digits.

A simple slice would give us

lambda n,m:int(n[-m%len(n):]+n[:-m%len(n)])

for 43 bytes. To remove the need for the repeated -m%len(n) we can instead concatenate the last m%len(n) digits with all the digits of n and then take the first len(n) digits. This is

lambda n,m:int((n[-m%len(n):]+n)[:len(n)])

for 42 bytes. The n[-m%len(n):] can then be replaced with taking the rightmost m digits of m ns concatenated together, (n*m)[-m:] giving us the 39 byte solution.

Python 3, 39 bytes

lambda n,m:int(((n*m)[-m:]+n)[:len(n)])

Try it online! Or see the test-suite.

How?

Rotating n right by m is the same as rotating n right by m modulo length n (m%len(n)), which is the concatenation of the last m%len(n) digits with the first len(n)-m%len(n) digits.

A simple slice would give us

lambda n,m:int(n[-m%len(n):]+n[:-m%len(n)])

for 43 bytes. To remove the need for the repeated -m% we can instead concatenate the last m%len(n) digits with all the digits of n and then take the first len(n) digits. This is

lambda n,m:int((n[-m%len(n):]+n)[:len(n)])

for 42 bytes. The n[-m%len(n):] can then be replaced with taking the rightmost m digits of m ns concatenated together, (n*m)[-m:] giving us the 39 byte solution.

added 6 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Python 3,(削除) 44 (削除ここまで) 39 bytes

lambda n,m:int(((n*m)[-m:]+n)[:len(n)])

Try it online! Or see the test-suite .

How?

Rotating n right by m is the same as rotating n right by m modulo length n (m%len(n)), which is the concatenation of the last m%len(n) digits with the first len(n)-m%len(n) digits.

A simple slice would give us

lambda n,m:int(n[-m%len(n):]+n[:-m%len(n)])

for 43 bytes. To remove the need for the repeated -m%len(n) we can instead concatenate the last m%len(n) digits with all the digits of n and then take the first len(n) digits. This is

lambda n,m:int((n[-m%len(n):]+n)[:len(n)])

for 42 bytes. The n[-m%len(n):] can then be replaced with taking the rightmost m digits of mns concatenated together, (n*m)[-m:] giving us the 39 byte solution.

Python 3,(削除) 44 (削除ここまで) 39 bytes

lambda n,m:int(((n*m)[-m:]+n)[:len(n)])

Try it online! Or see the test-suite.

Python 3, 39 bytes

lambda n,m:int(((n*m)[-m:]+n)[:len(n)])

Try it online! Or see the test-suite .

How?

Rotating n right by m is the same as rotating n right by m modulo length n (m%len(n)), which is the concatenation of the last m%len(n) digits with the first len(n)-m%len(n) digits.

A simple slice would give us

lambda n,m:int(n[-m%len(n):]+n[:-m%len(n)])

for 43 bytes. To remove the need for the repeated -m%len(n) we can instead concatenate the last m%len(n) digits with all the digits of n and then take the first len(n) digits. This is

lambda n,m:int((n[-m%len(n):]+n)[:len(n)])

for 42 bytes. The n[-m%len(n):] can then be replaced with taking the rightmost m digits of mns concatenated together, (n*m)[-m:] giving us the 39 byte solution.

added 6 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Python 3, (削除) 44 (削除ここまで) 4139 bytes

f=lambdalambda n,m:int(((n*m)[-m:]+n)[:len(n)])

Try it online! Try it online! Or see the test-suite test-suite.

Python 3, (削除) 44 (削除ここまで) 41 bytes

f=lambda n,m:int(((n*m)[-m:]+n)[:len(n)])

Try it online! Or see the test-suite.

Python 3, (削除) 44 (削除ここまで) 39 bytes

lambda n,m:int(((n*m)[-m:]+n)[:len(n)])

Try it online! Or see the test-suite.

added 25 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293
Loading
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293
Loading

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