22
\$\begingroup\$

The challenge here is to take a string and output all its rotations, by repeatedly moving the first character to the end, once per character in the string, ending with the original string:

john -> ohnj, hnjo, njoh, john

You may also cycle in the other direction, moving characters from the end:

john -> njoh, hnjo, ohnj, john

You should still output one rotation per letter even if the original word is reached before that:

heehee -> eeheeh, eheehe, heehee, eeheeh, eheehe, heehee

Character arrays are allowed, as long as the result works as shown above.

Shortest answer wins!

| improve this question | | | | |
\$\endgroup\$
  • 5
    \$\begingroup\$ If a string like heehee returns to the original order in fewer cycles than its length, do we stop there? I expect this would make a big difference for many answers. \$\endgroup\$ – xnor Dec 8 '18 at 22:43
  • \$\begingroup\$ May we cycle in the other direction? \$\endgroup\$ – xnor Dec 8 '18 at 22:45
  • 2
    \$\begingroup\$ I edited the question including your clarifications, feel free to change it if it's not what you intended. \$\endgroup\$ – xnor Dec 8 '18 at 23:28
  • 1
    \$\begingroup\$ @xnor that looks much clearer than my original post, thanks so much! \$\endgroup\$ – SimpleGeek Dec 9 '18 at 13:38
  • 1
    \$\begingroup\$ Are we allowed to input/output character arrays? (The distinction can be important in some languages.) \$\endgroup\$ – LegionMammal978 Dec 9 '18 at 18:09

47 Answers 47

1
2
0
\$\begingroup\$

Pyth, 11 bytes

VSlQ+>QN<QN

Try it online!

Pretty much just a port of my Python answer

Explanation

==================================================
assign('Q',eval_input())
for N in num_to_range(Psorted(Plen(Q))):
 imp_print(plus(gt(Q,N),lt(Q,N)))
==================================================

V                    # for N in 
 S                   # 1-indexed range
  lQ                 # length of evaluated input
    +                # concatanate
     >QN             # all characters after index N in Q (Q[N:])
        <QN          # and all characters before index N in Q (Q[:N])
| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Tcl, 78 bytes

proc R t {time {puts [set t [regsub -all (.?)(.*) $t {\2\1}]]} [string le $t]}

Try it online!

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 20 bytes

{.rotate(all 1..$_)}

Try it online!

Anonymous code block that has input/output as a list of characters. The output is a Junction object that contains all the rotated arrays. I'm not sure how legal this is, since extracting values from a Junction is not normal.

Explanation:

{                  }   # Anonymous codeblock
 .rotate(         )    # Rotate the input list
         all 1..$_     # By all of the range 1 to length of the input array
                          # all creates a junction of values
                          # And it runs the function on each value

The all can be replaced by any of any, one, [&], [|], [^].

If the Junction isn't allowed, here's an alternate solution for 22 bytes:

{.&{.rotate(++$)xx$_}}

Try it online!

There's probably a better way to reset the anonymous variable ++$ than by wrapping the code in another code block...

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Java (JDK), 85 bytes

s->{for(int i=s.length();i-->0;)System.out.println(s.substring(i)+s.substring(0,i));}

Try it online!

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Clojure, 64 bytes

(fn[w](take(count w)(rest(iterate #(str(subs % 1)(first %))w))))

Try it online!

See the pre-golfed code below for an explanation. Returns a (lazy) list of strings.

(defn cycle-word [word]
  (->> ; Take the word
       word

       ; Create an infinite list of iterations of strings where
       ;   the first character is moved to the end of the subs(tring)
       (iterate #(str (subs % 1) (first %)))

       ; Drop the first result since we don't want it to start with the original word
       (rest)

       ; Then take as many iterations from the infinite list as the word has characters
       (take (count word))))

(cycle-word "john")
=> ("ohnj" "hnjo" "njoh" "john")
| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 5 bytes

hÅ╫o;

Try it online!

Explanation

h       length of array/string without popping
 Å      start block of length 2
  ╫     left-rotate bits in int, list/str
   o    print TOS without popping
    ;   discard TOS
| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Pyth, 7 bytes

_.e.>Qk

Accepts input as a string or as a list of characters, output is a list of the same. Try it online here.

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Ruby, 33 bytes

->s{s.chars.map{s=s[1..-1]+s[0]}}

Try it online!

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

JavaScript, 36 bytes

Saved 3 bytes thanks to @Shaggy by using shift() instead of splice().

a=>a.map(x=>a.push(a.shift())&&a+"")

Try it online!

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

C (clang), 57 54 bytes

i;f(int*a){for(i=0;a[i++];printf("%S%.*S ",a+i,i,a));}

Try it online!

Takes input as a wide string.

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ Loop until a[i++] to save 7 bytes. \$\endgroup\$ – user77406 Dec 12 '18 at 6:15
  • \$\begingroup\$ Oh, no char*. You'd still save bytes, though (just make i global and f(char*a). \$\endgroup\$ – user77406 Dec 12 '18 at 6:26
0
\$\begingroup\$

Runic Enchantments, 59 bytes

>iu0l:1-}[{ R{:0=1\
/:$ak$0l1-[{U$:/?-/
\}?*2=0:-1{]{~ /;$~

Try it online!

Can handle inputs up to 8 characters (spaces need to be escaped, however).

Essentially just a bunch of stack manipulation, with integer 0 denoting the end of the stack and the original length used as a counter held at the end.

Without using the stack-of-stacks commands [ and ] the solution is slightly larger (63 bytes) but can handle strings up to 16 characters:

>iu0l1-}l1-sRl1-s:0 \
/s:$ak$0l1-sU$:/?-1=/
\-1l}?*5=0:-1{~/;${

Try it online!

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Reticular, 56 38 bytes

iSL:%=@qB[:%`@d]:%`*[[o]:%`1-*p$]:%`*;

Try it online!

Explanation with input "Hee"

iS                # Read input string and put its chars into an array
L:%=              # Get length of string and save it as `%`
@qB               # Reverse array and push its items to the stack
                   Stack: [H, e, e]
[:%`@d]:%`*       # Duplicates the top length(str) items in the stack
                   a total of length(str) number of times
                   Stack before: [H, e, e]
                   Stack after: [H, e, e, H, e, e, H, e, e, H, e, e]
[[o]:%`1-*p$]:%`* # Output the top length(str) items from the stack
                    with a trailing newline, and then
                    pop the next item in the stack.
                    Iterate this `%` number of times
                    Stack before: [H, e, e, H, e, e, H, e, e, H, e, e]

                    Stack after 1st iteration: [e, e, H, e, e, H, e, e]
                    Output: Hee
                    Stack after 2nd iteration: [e, H, e, e]
                    Output: Hee
                            eeH
                    Stack after 3rd iteration: []
                    Output: Hee
                            eeH
                            eHe

;                 # Exit
| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

PHP, 63 bytes

for($s=$argv[1];$x++<strlen($s);)echo$s=substr($s,1).$s[0]," ";

Try it online!

Output

$php strrot.php john
ohnj hnjo njoh john

$php strrot.php heehee
eeheeh eheehe heehee eeheeh eheehe heehee
| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

C# (.NET Core), 103, 79 bytes

Without LINQ.

EDIT: ASCII-only golfing -24 bytes with a better use of a for loop and substring return!

p=>{var s="";for(int j=0;j++<p.Length;s+=p+" ")p=p.Substring(1)+p[0];return s;}

Try it online!

| improve this answer | | | | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 79 \$\endgroup\$ – ASCII-only Jan 18 '19 at 3:08
0
\$\begingroup\$

cQuents, 11 bytes

=A#|LA)&_lZ

Try it online!

Explanation

=A#|LA)&_lZ

=A              first term in sequence is the input
  #|LA)         add second input n: length of input
       &        output first n terms in sequence
                each term equals:
        _l                        unary rotate left of 
          Z                                            previous term

Replace _l with _r to rotate right instead of left.

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

perl 5, 18 (-n) bytes

Thanks to Xcali

s/./say$',$`,$&/ge

TIO

Original answer (23 bytes)

/^.+?(?{say$'.$&})(?!)/

TIO

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ 18 bytes \$\endgroup\$ – Xcali May 14 '19 at 17:04
0
\$\begingroup\$

Pepe, 64 52 bytes

rEEEEeREEeRREeeeREEReREEEeeReReeeReeERREeeerEEEEERee

Try it online! Uses newlines for separation.

| improve this answer | | | | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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