The task
This is a simple challenge.Your input is a single non-empty string, containing only digits0123456789 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#### and23###1 are valid inputs, while###,0099 and#4#4 are not.
Your task is to extract the integern from the string, and output the string rotatedn 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 is26####0. - The input
1####2contains 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 numbern fits into the standardint type of your language.Conversely, if that standardint type implements arbitrary-precision integers, you must support (in theory) an arbitrarily largen.
Test cases
#1## -> ##1###4## -> #4###1####1 -> ####111####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############4605508015057787465629118655000568375993099127531613012513406622393034741346840434468680494753262730615610086255892915828812820699971764142551702608639695081452206500085233149468399533981039485419872101852######################3680 -> 99533981039485419872101852######################36805683759930991275316130125134066223930347413468404344686804947532627306156100862558929158288128206999717641425517026086396950814522065000852331494683- 7\$\begingroup\$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?\$\endgroup\$Dennis– Dennis2016-02-24 21:37:46 +00:00CommentedFeb 24, 2016 at 21:37
- \$\begingroup\$@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
nfits into the nativeinttype of your language (which may be arbitrary-precision). I'll update the challenge text later.\$\endgroup\$Zgarb– Zgarb2016-02-24 21:49:19 +00:00CommentedFeb 24, 2016 at 21:49 - \$\begingroup\$What should we do if input=
1234?\$\endgroup\$CalculatorFeline– CalculatorFeline2016-02-24 22:00:53 +00:00CommentedFeb 24, 2016 at 22:00 - 2\$\begingroup\$@CatsAreFluffy"and at least one #"\$\endgroup\$FryAmTheEggman– FryAmTheEggman2016-02-24 22:01:13 +00:00CommentedFeb 24, 2016 at 22:01
20 Answers20
CJam, 11 bytes
q_'#%W%sim>Try it online! orverify all test cases.
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.- \$\begingroup\$Oooh... so simple!\$\endgroup\$Luis Mendo– Luis Mendo2016-02-24 22:25:32 +00:00CommentedFeb 24, 2016 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 aChar array, shift, andjoin 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!
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‘ḌṂṙ@Try it online! orverify all test cases.
Background
Say the input is51379#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 to1, then mapped to~1 = -2. On failure (#), it returns0.
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 mapsn 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 theleft, 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 usesXz instead ofXv, owing to recent changes in the language.
! % take implicit input: string. Transpose into column char arrayG % push input string again1Y4 % predefined literal '\d' (for regexp)XX % match regexp. Gives cell array with 1 or 2 stringsP % flip that arrayZcXv % join the strings in that array, without spacesU % convert to numberYS % rotate the transposed input that many times! % put back into row form (string). Implicitly displayPowerShell, 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$dPowerShell doesn't have the concept of "shifting" an array, so I had to roll my own solution. Will take along 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 afor 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 arrayleft instead ofright, we can do it significantly shorter usingmultiple 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$aHere we once again take input, and set$b as above. We re-cast$a as a char-array, and then enter the samefor 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Σ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- \$\begingroup\$Concat and take maximum: great idea!\$\endgroup\$Luis Mendo– Luis Mendo2016-02-24 22:36:35 +00:00CommentedFeb 24, 2016 at 22:36
- \$\begingroup\$@LuisMendo I was amused to see Alex's answer pop up with the same strategy while I was writing up the explanation here.\$\endgroup\$user45941– user459412016-02-24 22:37:25 +00:00CommentedFeb 24, 2016 at 22:37
- \$\begingroup\$Looks like the only one that initially used the naïve approach was me :-) (rotating the initial string until all digits were contiguous)\$\endgroup\$Luis Mendo– Luis Mendo2016-02-24 22:41:00 +00:00CommentedFeb 24, 2016 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.
- \$\begingroup\$
FromDigitsworks instead ofToExpression.\$\endgroup\$CalculatorFeline– CalculatorFeline2016-02-28 04:22:13 +00:00CommentedFeb 28, 2016 at 4:22
Pyth,22 14 bytes
.>zs-.<zxz\#\#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.
- \$\begingroup\$You can do
-...\#instead ofh:..."\d+"1. Also, no need to convertzinto a list of chars,.>works also on a string.\$\endgroup\$Jakube– Jakube2016-02-25 00:10:14 +00:00CommentedFeb 25, 2016 at 0:10 - \$\begingroup\$@Jakube Thanks for the hint, was pretty tired when I did this. ^^\$\endgroup\$Denker– Denker2016-02-25 07:01:27 +00:00CommentedFeb 25, 2016 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)- 1\$\begingroup\$@WashingtonGuedes no, the sum in
b+ais a string concatenation.a='32',b='1', (b?b+a:a)=='132', (b|0+a)==33\$\endgroup\$edc65– edc652016-02-26 07:36:06 +00:00CommentedFeb 26, 2016 at 7:36
Pyth, 10 bytes
.>zss_cz\#This is a translation ofDennis' 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 rightJavaScript (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.
- \$\begingroup\$Using a ternary and a sum instead of reverse().join(), you should beat my score\$\endgroup\$edc65– edc652016-02-25 08:12:09 +00:00CommentedFeb 25, 2016 at 8:12
- \$\begingroup\$@Downgoat Sorry, I've got them mostly right lately but I did this one late at night and so I wasn't thinking straight.\$\endgroup\$Neil– Neil2016-02-25 11:18:28 +00:00CommentedFeb 25, 2016 at 11:18
- \$\begingroup\$@edc65 No, that made my score higher. So I copied the
s+strick instead. (I actually thought about that last night but I was too tired to try it out at the time.)\$\endgroup\$Neil– Neil2016-02-25 11:39:57 +00:00CommentedFeb 25, 2016 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@FExplanation:
-lFreads 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 from1to the number of times to rotate the array. - For each element in that list, we rotate the array (
popthe last element andunshiftit onto the beginning). - Then
sayall 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)*""splitconverts 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 intsmaxpick the largest introtatemaxtimes*""convert the array back into a string (shorthand forjoin)
Usage :ruby scriptname.rb "[string]"
- \$\begingroup\$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\$\endgroup\$FuzzyTree– FuzzyTree2016-02-26 00:52:11 +00:00CommentedFeb 26, 2016 at 0:52
- 1\$\begingroup\$Multiple answers in different languages is fine, even encouraged (provided that they are all correct).\$\endgroup\$Zgarb– Zgarb2016-02-26 00:58:47 +00:00CommentedFeb 26, 2016 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\JExplanation:
'#¡ # 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 stackUsesCP-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 laterc=replace(split(f&f,"/")(1),"#",d) mod b -get the number and calc the modd=right(a,c)&left(a,b-c) -d->implicit outputthis kinda sucks ... there's probably a better way to do it, even in VBscript
- \$\begingroup\$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
ais the function input, and it returns the output? That way, you wouldn't need theinputboxandmsgboxcalls.\$\endgroup\$wizzwizz4– wizzwizz42016-02-25 17:03:24 +00:00CommentedFeb 25, 2016 at 17:03 - \$\begingroup\$Why do you need
b?\$\endgroup\$CalculatorFeline– CalculatorFeline2016-02-28 04:32:59 +00:00CommentedFeb 28, 2016 at 4:32
Mathematica,73 58 bytes
#~StringRotateRight~Max[FromDigits/@StringSplit[#<>#,"#"]]&Much byte.15bytessavedthankstoIPoiler
- \$\begingroup\$
StringRotateRightsaves some bytes here.\$\endgroup\$IPoiler– IPoiler2016-02-25 04:47:25 +00:00CommentedFeb 25, 2016 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.
Explore related questions
See similar questions with these tags.









