Locate and rotate

This is a simple challenge. Your input is a single non-empty string, containing only digits 0123456789 and hashes #. It will contain exactly one run of digits, which encodes a nonnegative integer and may wrap around the end of the string, and at least one #. The integer may have leading zeroes. For example, ##44##, 013#### and 23###1 are valid inputs, while ###, 0099 and #4#4 are not.

Your task is to extract the integer n from the string, and output the string rotated n steps to the right.

Examples

• The input #1## should be rotated 1 step to the right, so the correct output is ##1#.
• The input #026### should be rotated 26 steps to the right, since the leading 0 is ignored. The correct output is 26####0.
• The input 1####2 contains the integer 21 wrapped over the end, so it should be rotated 21 steps to the right. The correct output is ##21##.

Rules and scoring

You can write a full program or a function. The lowest byte count wins, and standard loopholes are disallowed.

You can assume that the number n fits into the standard int type of your language. Conversely, if that standard int type implements arbitrary-precision integers, you must support (in theory) an arbitrarily large n.

Test cases

#1## -> ##1#
##4## -> #4###
1####1 -> ####11
1####2 -> ##21##
#026### -> 26####0
#000### -> #000###
###82399 -> ##82399#
51379#97 -> #9751379
#98##### -> ###98###
#######4## -> #4########
60752#1183 -> 8360752#11
####99366800## -> 366800######99
########9##### -> ###9##########
91#####515694837 -> 1#####5156948379
###6114558###### -> #6114558########
######219088736090042#### -> 9088736090042##########21
#46055080150577874656291186550000138168########### -> 0138168############4605508015057787465629118655000
568375993099127531613012513406622393034741346840434468680494753262730615610086255892915828812820699971764142551702608639695081452206500085233149468399533981039485419872101852######################3680 -> 99533981039485419872101852######################36805683759930991275316130125134066223930347413468404344686804947532627306156100862558929158288128206999717641425517026086396950814522065000852331494683

• Do we have to support all test cases? Some of those numbers are pretty big... Would using a language with 8-bit integers be acceptable? – Dennis Feb 24 '16 at 21:37
• @Dennis It's possible to solve the challenge with modular arithmetic without actually loading the integer into memory... but you're right, it's a hassle in many languages. Let's say you only need to handle those test cases where n fits into the native int type of your language (which may be arbitrary-precision). I'll update the challenge text later. – Zgarb Feb 24 '16 at 21:49
• What should we do if input=1234? – CalculatorFeline Feb 24 '16 at 22:00
• @CatsAreFluffy "and at least one #" – FryAmTheEggman Feb 24 '16 at 22:01

CJam, 11 bytes

q_'#%W%sim>


Note that this won't work for the last two test cases, since the involved numbers don't fit into 64 bits.

How it works

q_          e# Read all input and push it twice.
'#%       e# Split at runs of '#'.
W%     e# Reverse the resulting array.
si   e# Cast to string, then to int.
m> e# Rotate the original input that many places to the right.

• Oooh... so simple! – Luis Mendo Feb 24 '16 at 22:25

Julia, 71 65 bytes

s->join(circshift([s...],maximum(parse,split(s*s,"#",keep=1<0))))


This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.

We append the input to itself, split it into an array with # as the separator, parse each integer, and take the maximum. This defines the number of times we shift the string to the right. We splat the string into a Char array, shift, and join it back together.

Python, 66 bytes

lambda l:(2*l)[-int(''.join(l.split('#')[::-1]))%len(l):][:len(l)]


Retina, 6557 49

(\d*)#*(\d+)
$2$1 $0 ^\d+$*
+1 (.*)(.)
$2$1
<space>



Saved 8 bytes thanks to Martin!

Try it Online!

Note that this will time out / run out of memory for the very large test cases online, and on most sane machines, for some of the larger ones.

This takes the last number in the string and the first or no number in the string and puts them in front of the string. Then it converts that combined number to unary and repeatedly rotates while dropping a unary digit.

Jelly, 12 10 bytes

ẋ2~ṣ0‘ḌṂṙ@


Background

Say the input is 51379#97.

By repeating the string twice (51379#9751379#97), we can make sure that it will contain a contiguous representation of the number.

Next, we apply bitwise NOT to all characters. This attempts to cast to int, so '1' gets evaluated to 1, then mapped to ~1 = -2. On failure (#), it returns 0.

For our example, this gives

[-6, -2, -4, -8, -10, 0, -10, -8, -6, -2, -4, -8, -10, 0, -10, -8]


Next, we split at zeroes to separate the part that encodes the number from the rest.

[[-6, -2, -4, -8, -10], [-10, -8, -6, -2, -4, -8, -10], [-10, -8]]


Bitwise NOT maps n to -n - 1, so we increment each to obtain -n.

[[-5, -1, -3, -7, -9], [-9, -7, -5, -1, -3, -7, -9], [-9, -7]]


Next, we convert each list from base 10 to integer.

[-51379, -9751379, -97]


The lowest number is the negative of the one we're searching for. Since Jelly list rotation atom ṙ rotates to the left, this avoid multiplying by -1 to rotate to the right.

How it works

ẋ2~ṣ0‘ḌṂṙ@  Main link. Input: S (string)

ẋ2          Repeat the string twice.
~         Apply bitwise NOT to all characters.
This maps 'n' to ~n = -(n+1) and '# to 0.
ṣ0       Split at occurrences of zeroes.
‘      Increment all single-digit numbers.
Ḍ     Convert each list from base 10 to integer.
Ṃ    Take the minimum.
ṙ@  Rotate S that many places to the left.


MATL, 282517 16 bytes

!G1Y4XXPZcXvUYS!


8 bytes less borrowing Dennis' idea of splitting the array and reversing the order of the pieces

The two last test cases don't work because the number is too large.

EDIT (May 20, 2016) The code in the link uses Xz instead of Xv, owing to recent changes in the language.

Try it online!

!         % take implicit input: string. Transpose into column char array
G         % push input string again
1Y4       % predefined literal '\d' (for regexp)
XX        % match regexp. Gives cell array with 1 or 2 strings
P         % flip that array
ZcXv      % join the strings in that array, without spaces
U         % convert to number
YS        % rotate the transposed input that many times
!         % put back into row form (string). Implicitly display


PowerShell, 153 bytes

(But see the Extra Credit section, below)

param($a)$d=[System.collections.arraylist][char[]]$a;for($b=+("$a$a"-split"#"-ne'')[1];$b;$b--){$r=$d[-1];$d.removeAt($d.Count-1);$d.insert(0,$r)}-join$d  PowerShell doesn't have the concept of "shifting" an array, so I had to roll my own solution. Will take a long time for larger numbers, but it should eventually complete anything that fits in a 32-bit int. Takes input $a, and sets a new variable $d as a [System.Collections.ArrayList] object. This is done because, technically, arrays in PowerShell are immutable (further explained below in Extra Credit), and thus don't support arbitrary insertions or removals, which is needed for shifting. Then, we enter a for loop. The initial condition is a trick I found -- if we concatenate the input together, split on #, and ignore empties, the second element of the resulting array will be equal to our number, regardless of wrapping. We set that to $b, and decrement $b each time until it's zero. Each iteration, we set helper $r as the last element in the arraylist, remove that last element, and then insert the element onto the front ... effectively "shifting" the array to the right by one element.

Finally, we simply output with -join$d so that it's concatenated into one string. Extra Credit If the problem was shifting the array left instead of right, we can do it significantly shorter using multiple assignment. Basically, "If the assignment value contains more elements than variables, all the remaining values are assigned to the last variable." In essence, this means something like $c=@(1,2,3) and $a,$b=$c will have $a=1 an int and $b=@(2,3) an array. PowerShell, 90 bytes, does a left shift instead of a right shift param($a)$b=+("$a$a"-split"#"-ne'')[1];$a=[char[]]$a;for(;$b;$b--){$r,$a=$a;$a+=$r}-join$a  Here we once again take input, and set $b as above. We re-cast $a as a char-array, and then enter the same for loop as above. This time, though, we've not needed to support arbitrary removal/insertion, so we don't need to use the costly [System.Collections.ArrayList] object, nor the expensive method calls. Instead we simply set $r to be the first element of $a, and the remaining elements are re-saved in $a. Then we += to tack it back on to the end.

(As I said, PowerShell arrays are technically immutable, but the += operator here is overloaded - it takes an array and another object, mushes them together (technical term) into a new array, returns that and saves it as the variable name, and destroys the original array. Functionally, we've just added an element to the end of the array, but technically (and from a memory/garbage-cleanup perspective, etc.) it's a brand-new array. This can obviously become a costly operation if the array is large or complex. The flipside is that, since arrays are immutable, indexing into them or iterating over them is very cheap.)

Output remains the same action, with a -join statement to turn it into a single string.

Seriously, 21 bytes

,;;+'#@s≈MM@#@/nΣ


Try it online!

Warning: this solution is very inefficient, so the larger test cases will time out on TIO. Use the local interpreter.

Explanation:

,;;+'#@s≈MM@#@/nΣ
,;;+                   make 3 copies of input, and concatenate two of them
'#@s               split on #s
≈MM          convert strings to ints, take maximum
@#@       explode final copy of input
/n   rotate to the right n times
Σ  join

• Concat and take maximum: great idea! – Luis Mendo Feb 24 '16 at 22:36
• @LuisMendo I was amused to see Alex's answer pop up with the same strategy while I was writing up the explanation here. – Mego Feb 24 '16 at 22:37
• Looks like the only one that initially used the naïve approach was me :-) (rotating the initial string until all digits were contiguous) – Luis Mendo Feb 24 '16 at 22:41

Mathematica, 69 Bytes

#~StringRotateRight~ToExpression[""<>Reverse@TextCases[#,"Number"]]&


Find sequences of numbers in the, if there are 2 then their order needs to be reversed. Concatenate the strings (if it's only one it just returns the number string). Convert string to numeric and rotate the string that number of times.

• FromDigits works instead of ToExpression. – CalculatorFeline Feb 28 '16 at 4:22

Pyth, 22 14 bytes

.>zs-.<zxz\#\#


Try it here!

Explanation

.>zs-.<zxz\#\#    # z=input

.<z          # rotate z left by
xz\#      # index of the first occurence of a hash
# this ensures that the integer is not wrapped around the end
-       \#    # filter all hashes out
s              # cast to an integer, also removes leading zeroes
.>z               # do the final roation of the input string and print it


This works for all testcases and also almosts finishes instantly for the very big numbers.

• You can do -...\# instead of h:..."\d+"1. Also, no need to convert z into a list of chars, .> works also on a string. – Jakube Feb 25 '16 at 0:10
• @Jakube Thanks for the hint, was pretty tired when I did this. ^^ – Denker Feb 25 '16 at 7:01

JavaScript (ES6) 66

For once, the stupid negative % of javascript for negative numbers come useful

z=>(z+z).substr(-(l=z.length,[a,b]=z.match(/\d+/g),b?b+a:a)%l-l,l)

• @WashingtonGuedes no, the sum in b+a is a string concatenation. a='32',b='1', (b?b+a:a)=='132', (b|0+a)==33 – edc65 Feb 26 '16 at 7:36

Pyth, 10 bytes

.>zss_cz\#


This is a translation of Dennis' CJam answer. I'm making it a community wiki, since I didn't come up with it.

Explanation

      cz\#   split input at #
_       reverse
s        join
s         cast to integer
.>z          rotate input right


JavaScript (ES6), 67 64 bytes

s=>(l=s.length,s+s).substr(l-s.split(/#+/).reverse().join%l,l)


Another port of Dennis's CJam answer.

Edit: Saved 3 bytes by appropriating the part of edc65's answer that he didn't draw attention to.

• Using a ternary and a sum instead of reverse().join(), you should beat my score – edc65 Feb 25 '16 at 8:12
• @Downgoat Sorry, I've got them mostly right lately but I did this one late at night and so I wasn't thinking straight. – Neil Feb 25 '16 at 11:18
• @edc65 No, that made my score higher. So I copied the s+s trick instead. (I actually thought about that last night but I was too tired to try it out at the time.) – Neil Feb 25 '16 at 11:39

Perl 5, 41 bytes

39 bytes plus two for the -lF flags (-M5.01 is free): perl -lF -M5.01 script.pl

/#+/;map{unshift@F,pop@F}1..$'.$;say@F


Explanation:

• -lF reads the input, removes the trailing newline, puts the remainder into the string $_, splits it up into characters, and puts that split into the array @F. • /#+/ finds the first string of #s in $_ and sets $ equal to the stuff before it and $' equal to the stuff after it. If $ is empty then $' may contain more #s. However, $'.$ is a string whose initial substring is the number of times to rotate the array.
• Now we build the list 1..$'.$, which treats $'.$ as an integer and thus numifies it, which strips any final #s, so the list is from 1 to the number of times to rotate the array.
• For each element in that list, we rotate the array (pop the last element and unshift it onto the beginning).
• Then say all the elements of the rotated array.

Ruby - 6872 70 bytes

s=ARGV[0]
p s.split(//).rotate(-(s+s).scan(/\d+/).map(&:to_i).max)*""

• split converts string into an array
• (s+s).scan(/\d+/) concatenate string to itself and get an array of numbers (as strings)
• map(&:to_i) convert strings to ints
• max pick the largest int
• rotate max times
• *"" convert the array back into a string (shorthand for join)

Usage : ruby scriptname.rb "[string]"

• i'm new here. what's the etiquette on posting multiple answers in different languages? I added a separate answer in case one was wrong. if it's not ok to add multiple answers, let me know and i'll take it down – FuzzyTree Feb 26 '16 at 0:52
• Multiple answers in different languages is fine, even encouraged (provided that they are all correct). – Zgarb Feb 26 '16 at 0:58

05AB1E, 14 13 bytes

Well, the code is very unlikely to terminate for the numbers bigger than 100000, but if you're patient enough, there will be an output :). Code:

'#¡rJ¹sF¤rS\J


Explanation:

'#¡             # Split the input on '#'
r            # Reverse the stack
J           # Join the stack
¹          # Take the first input
s         # Swap with the number
F        # For N in range(0, number), do...
¤       #   Obtain the last character
r      #   Reverse the stack
S     #   Split everything to individual characters
\    #   Delete the last character
J   #   Join the stack


Try it online!

Uses CP-1252 encoding

VBSCRIPT, 82 99 BYTES

previous code didn't handle cases with number wrapped over the end

b=len(a):f=replace(a,"#","/",1,1):c=replace(split(f&f,"/")(1),"#",d) mod b:d=right(a,c)&left(a,b-c)


UNGOLFED

b=len(a)                                 -a->implicit input, get its length
f=replace(a,"#","/",1,1)  -replace first instance of # so we can split later
c=replace(split(f&f,"/")(1),"#",d) mod b    -get the number and calc the mod
d=right(a,c)&left(a,b-c)                    -d->implicit output


this kinda sucks ... there's probably a better way to do it, even in VBscript

• Welcome to Programming Puzzles and Code Golf Stack Exchange. This answer could be improved by adding a code breakdown and explanation below your golfed code. Also, could you save bytes by creating a function instead of a program, where a is the function input, and it returns the output? That way, you wouldn't need the inputbox and msgbox calls. – wizzwizz4 Feb 25 '16 at 17:03
• Why do you need b? – CalculatorFeline Feb 28 '16 at 4:32

Mathematica, 73 58 bytes

#~StringRotateRight~Max[FromDigits/@StringSplit[#<>#,"#"]]&


Much byte. 15 bytes saved thanks to IPoiler

• StringRotateRight saves some bytes here. – IPoiler Feb 25 '16 at 4:47

Matlab(73)

  @(a)regexprep(a,'(\d*)#*(\d*)#*','${circshift($0,[0 str2num([$2$1])])}')

• This is using another approach that i wonder if @luis used it, because refering to his description there is some points in common while (un)?fortunately i dont understand the cropped Matl language.

matlab(86)72

 @(n)circshift(n,[0 str2num(circshift(n(n~='#'),[0,-find(n=='#',1)+1]))])

• The function totates the string two times, once for integer extraction, second for the desired task, it doesnt take too much time because matlab proceeds to rotate by (Dim)modulus(Length) to the exception that it falls in segmentation failure for bigger ranges.

• Will struggle how to golf it more ....

(86)

  @(n)circshift(n,[0 str2num([strtok(n(find(n=='#',1,'last'):end),'#') strtok(n,'#')])])
`
• The difference between this function and the previous, this one does concatenate two distant occurences of integers way backwards, while the first one just rotates it.