34
\$\begingroup\$

Given a positive number n, rotate its base-10 digits m positions rightward. That is, output the result of m steps of moving the last digit to the start. The rotation count m will be a non-negative integer.

You should remove leading zeroes in the final result, but not in any of the intermediate steps. For example, for the test case 100,2 => 1, we first rotate to 010, then to 001, then finally drop the leading zeroes to get 1.

Tests

n,m => Output

123,1 => 312
123,2 => 231
123,3 => 123
123,4 => 312
1,637 => 1
10,1 => 1
100,2 => 1
10,2 => 10 
110,2 => 101
123,0 => 123
9998,2 => 9899
\$\endgroup\$
6
  • 2
    \$\begingroup\$ I've edited the post (i.e. added some formatting/CGCC terms) to help make it even more understandable. Nice first challenge! \$\endgroup\$
    – lyxal
    Aug 15, 2020 at 4:25
  • 14
    \$\begingroup\$ The test cases suggest this loops around for big n, which isn't clear from the text. \$\endgroup\$
    – xnor
    Aug 15, 2020 at 4:50
  • 1
    \$\begingroup\$ You should indicate in the text that the rotation is to the right \$\endgroup\$
    – Luis Mendo
    Aug 15, 2020 at 12:49
  • 1
    \$\begingroup\$ @Shaggy Hm I guess "moving the last digit to the start" is clear enough \$\endgroup\$
    – Luis Mendo
    Aug 15, 2020 at 16:25
  • 1
    \$\begingroup\$ From the test cases, it seems the input number can be base 4 or any higher base, to handle digits up to 3? Power-of-2 bases are much more efficient and convenient to work with in binary computers, e.g. hardware rotate instructions, and bit-shifts. e.g. x86 add ecx,ecx / ror eax, cl rotates by n 2-bit digits, in 4 bytes of machine code. Nothing in the question actually says you have to rotate base-10 digits, which would be inconvenient if you get input as an int or something. But I suspect you meant that? \$\endgroup\$ Aug 16, 2020 at 8:08

43 Answers 43

1
2
1
\$\begingroup\$

Haskell, 57 bytes

f n m=read(foldr(\_ y->last y:init y)(show n)[1..m])::Int

Try it online!

f n m=            - function expecting two integers

foldr ... [1..m]  - folds `m` times..
      .. (show n)  - starting with `n` converted to string
      . (\_ y->last y:init y)  - moving the last element in head
read( ... )::Int  - convert the result to Int
\$\endgroup\$
1
\$\begingroup\$

[Excel/Google Sheets], 63 bytes

With n and m in columns a and b, and this formula in column c

=VALUE(RIGHT(A4, MOD(B4,LEN(A4)))&LEFT(A4,LEN(A4)-MOD(B4,LEN(A4

Screenshot

\$\endgroup\$
1
  • \$\begingroup\$ These should get you to 47: 1. Put LEN(A1) in C1 and MOD(B1,C1) in D1. 2. Instead of VALUE(), use --() if you need a number. \$\endgroup\$ Aug 18, 2020 at 13:57
1
\$\begingroup\$

SimpleTemplate 0.84, 106 bytes

This challenge really got my head spinning...

{@fnR N,T}{@forfrom Tto1}{@ifN matches"@(.+)(.)@"M}{@setN"#{M.2}#{M.1}"}{@/}{@/}{@set+N N,0}{@returnN}{@/}

This creates a function R that takes the Number and the amount of Times you want to swap the numbers around.

The way it works is by evaluating if the Number matches a regular expression ({@ifN matches"@(.+)(.)@"M}) and storing the Matched parts.

Then, it reconstructs the number with the last digit at the beginning ({@setN"#{M.2}#{M.1}"}).

To remove leading zeroes, I've simply stored the sum of whatever is the Number with 0, returning it after.


Ungolfed:

It is a lot easier to follow an ungolfed example:

{@fn rotate_n number, times}
    {@for i from times to 1}
        {@if number matches "@^(.+)(.)$@" matches}
            {@set number "#{matches.2}#{matches.1}"}
        {@/}
    {@/}
    {@set+ number number, 0}
    {@return number}
{@/}

The i is totally useless there, but removing it is a golfing step.



You can try this on: http://sandbox.onlinephpfunctions.com/code/91d0d49d2f750022e21cc7b1c18e6beec55f9c8b

You can change the values on line 1071.

\$\endgroup\$
1
\$\begingroup\$

Golfscript - 28 bytes

~:a;`{1/)\+}a*1/{(~:b!}do b\
  • input: 10450 4
  • output: 4501

Try it online!

Explanation

~:a;`                         # Parses the input "123 3" -> "123" on stack and a = 3
     {1/)\+}a*                # Pushes the last digit away and rejoins, repeat a times "123" -> "12" "3" -> "3" "12" -> "312" repeat
              1/{(~:b!}do b\  # Discard the leading elements until a non-zero element is found

There's room for improvement.

\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 29 25 bytes

{.(y#a),(y:-(#a)!y)_a:$x}

Try it online!

¯4 thanks to @ovs

\$\endgroup\$
1
  • \$\begingroup\$ $ for 10\ and . for 10/ saves a couple bytes, and you can go a bit shorter by doing some arithmetic on the indices with {a@ ... !n:#a:$x} \$\endgroup\$
    – ovs
    Apr 18, 2022 at 9:26
1
\$\begingroup\$

Ruby, 44 40 bytes

->a,b{a.to_s.chars.rotate(-b).join.to_i}

-4 from Dingus.

Try it online!

Ruby, 19 bytes

->a,b{a.rotate(-b)}

Try it online!

taking a list of digits

\$\endgroup\$
2
1
\$\begingroup\$

Vyxal, 3 bytes

ǔṅ⌊

Try it Online or Verify all the test cases

\$\endgroup\$
1
\$\begingroup\$

PHP, 89 bytes

list($n,$m)=explode(",",$argn);while($m--){$n=$n[-1].substr($n,0,-1);}echo ltrim($n,"0");

Try it online!

Explanation: A PHP answer that works by looping m times through the given string n moving the last character of n to the beginning of the string.

\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 18 bytes

{.y{(*|x):':x}/$x}

Try it online!

Right rotate inspired by @chrispsn.

\$\endgroup\$
1
\$\begingroup\$

Desmos, 76 70 bytes

f(n,m)=mod(n,t)10^d/t+floor(n/t)
d=floor(log(n+0^n))+1
t=10^{mod(m,d)}

Try it on Desmos!

Port of R answer.

-6 bytes thanks to Aiden Chow

\$\endgroup\$
0
\$\begingroup\$

GolfScript, 14 bytes

~\`\{1/)\+}\*~

Try it online!

~              # Puts n and m on top of the stack as integers
 \`\           # Parses n to string
    {1/)\+}    # This block goes to the top of the stack without being executed
           \*  # Executes previous block m times
             ~ # Parses the answer from string to integer, this removes the leading zeroes

What the block does:

     1/        # Parses the string to an array of strings
       )\      # Separates the last digit and puts it in the begining
         +     # Concatenates them again
\$\endgroup\$
1
  • \$\begingroup\$ The last backslash is unnecessary. \$\endgroup\$ Apr 18, 2022 at 2:31
0
\$\begingroup\$

Factor, 19 bytes

[ neg rotate dec> ]

Attempt This Online!

\$\endgroup\$
0
\$\begingroup\$

Lua 5.1 (58 bytes)

n=n..""for _=1,m do n=n:sub(-1)..n:sub(1,-2)end print(n*1)
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.