29
\$\begingroup\$

The task

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
\$\endgroup\$
4
  • 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
    Feb 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 n fits into the native int type of your language (which may be arbitrary-precision). I'll update the challenge text later. \$\endgroup\$
    – Zgarb
    Feb 24, 2016 at 21:49
  • \$\begingroup\$ What should we do if input=1234? \$\endgroup\$ Feb 24, 2016 at 22:00
  • 2
    \$\begingroup\$ @CatsAreFluffy "and at least one #" \$\endgroup\$ Feb 24, 2016 at 22:01

20 Answers 20

10
\$\begingroup\$

CJam, 11 bytes

q_'#%W%sim>

Try it online! or verify 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.
\$\endgroup\$
1
  • \$\begingroup\$ Oooh... so simple! \$\endgroup\$
    – Luis Mendo
    Feb 24, 2016 at 22:25
7
\$\begingroup\$

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.

\$\endgroup\$
7
\$\begingroup\$

Python, 66 bytes

lambda l:(2*l)[-int(''.join(l.split('#')[::-1]))%len(l):][:len(l)]
\$\endgroup\$
0
5
\$\begingroup\$

Retina, 65 57 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.

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

Jelly, 12 10 bytes

ẋ2~ṣ0‘ḌṂṙ@

Try it online! or verify all test cases.

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.
\$\endgroup\$
3
\$\begingroup\$

MATL, 28 25 17 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
\$\endgroup\$
2
\$\begingroup\$

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.

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

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
\$\endgroup\$
3
  • \$\begingroup\$ Concat and take maximum: great idea! \$\endgroup\$
    – Luis Mendo
    Feb 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
    Feb 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
    Feb 24, 2016 at 22:41
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ FromDigits works instead of ToExpression. \$\endgroup\$ Feb 28, 2016 at 4:22
1
\$\begingroup\$

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.

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

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)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @WashingtonGuedes no, the sum in b+a is a string concatenation. a='32',b='1', (b?b+a:a)=='132', (b|0+a)==33 \$\endgroup\$
    – edc65
    Feb 26, 2016 at 7:36
1
\$\begingroup\$

Pyth, 10 bytes

.>zss_cz\#

Try it online. Test suite.

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Using a ternary and a sum instead of reverse().join(), you should beat my score \$\endgroup\$
    – edc65
    Feb 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
    Feb 25, 2016 at 11:18
  • \$\begingroup\$ @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.) \$\endgroup\$
    – Neil
    Feb 25, 2016 at 11:39
1
\$\begingroup\$

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.
\$\endgroup\$
0
1
\$\begingroup\$

Ruby - 68 72 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]"

\$\endgroup\$
2
  • \$\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
    Feb 26, 2016 at 0:52
  • 1
    \$\begingroup\$ Multiple answers in different languages is fine, even encouraged (provided that they are all correct). \$\endgroup\$
    – Zgarb
    Feb 26, 2016 at 0:58
0
\$\begingroup\$

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

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

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

\$\endgroup\$
2
  • \$\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 a is the function input, and it returns the output? That way, you wouldn't need the inputbox and msgbox calls. \$\endgroup\$
    – wizzwizz4
    Feb 25, 2016 at 17:03
  • \$\begingroup\$ Why do you need b? \$\endgroup\$ Feb 28, 2016 at 4:32
0
\$\begingroup\$

Mathematica, 73 58 bytes

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

Much byte. 15 bytes saved thanks to IPoiler

\$\endgroup\$
1
  • \$\begingroup\$ StringRotateRight saves some bytes here. \$\endgroup\$
    – IPoiler
    Feb 25, 2016 at 4:47
0
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$

Your Answer

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

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