文字列の回転-最初の文字を最後まで繰り返し移動する出力文字列


22

ここでの課題は、最初の文字を最後まで繰り返して文字列を取得し、すべての回転を出力することです。文字列の文字ごとに1回、元の文字列で終了します。

john -> ohnj, hnjo, njoh, john

また、反対方向に循環して、文字を最後から移動することもできます。

john -> njoh, hnjo, ohnj, john

その前に元の単語に到達した場合でも、文字ごとに1つのローテーションを出力する必要があります。

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

結果が上記のように機能する限り、文字配列を使用できます。

最短回答が勝ちます!


5
のような文字列heeheeがその長さよりも少ないサイクルで元の順序に戻る場合、そこで停止しますか?これが多くの答えに大きな違いをもたらすと期待しています。
xnor

私たちは他の方向にサイクリングできますか?
xnor

2
私はあなたの説明を含む質問を編集しました。もしあなたが意図したものでないなら、それを自由に変更してください。
XNOR

1
@xnorは私の元の投稿よりもずっと明確に見えます、どうもありがとう!
I_P_Edwards

1
文字配列を入出力できますか?(一部の言語では区別が重要になる場合があります。)
LegionMammal978

回答:







4

Japt、5 3バイト

入力を文字配列として受け取り、文字配列の配列を出力します

£=é

ここで試してみてください

£=é     :Implicit input of character array U
£       :Map
  é     :  Rotate U one element to the right
 =      :  Reassign to U for next iteration


3

brainfuck、59バイト

,[>,]<[>>[>]+[<]<[<]>-[[>]>[>]<+[<]<[<]>-]>[.>]>[.>]<[<].<]

オンラインでお試しください!

nullバイトで区切られた各文字列を出力します。

説明:

,[>,]    # Get input
<[       # Start loop over input
  >>[>]       # Go to end of the string
  +           # Set it to one to mark it
  [<]<[<]>    # Move to the beginning of input
  -[[>]>[>]<+[<]<[<]>-]   # Transfer the first character to the end
  >[.>]>[.>]  # Print the rotated string
  <[<].       # Print a nul byte
<]       # Repeat loop while input

3

MATL6 5バイト

tf&+)

@luisのおかげで1バイト節約!

MATL Online試しください!

説明

    # Implicitly grab input string
t   # Duplicate the input
f   # Create an array [1, ..., N] where N is the number of characters in the input
&+  # Add the transpose of this array to itself to create a 2D array of indices
    #
    #   +   1  2  3  4
    #       ----------
    #   1 | 2  3  4  5
    #   2 | 3  4  5  6
    #   3 | 4  5  6  7
    #   4 | 5  6  7  8
    #
)   # Use this 2D array to index into the original string using periodic indexing
    # Implicitly display the resulting character array

@LuisMendo賢い!ありがとう!
Suever

3

Wolfram言語(Mathematica)35 26バイト

Partition[#,Tr[1^#],1,-1]&

オンラインでお試しください!

入力として文字のリストを受け取ります。

Partition(ただし、StringPartition以下で使用されるバリアントではありません)、入力を循環として処理するためのオプションの4番目の引数があります(そして、その方法を正確に指定するため)。 -in関数。

Wolfram言語(Mathematica)、44バイト

Rest@StringPartition[#<>#,StringLength@#,1]&

オンラインでお試しください!

同じですが、入力として文字列を受け取ります。

ターン"john"には"johnjohn"、すべての長さ-取りStringLength["john"]生産、オフセット1で、この文字列の部分文字列を{"john","ohnj","hnjo","njoh","john"}、次にこれらの最初の低下します、Rest


文字配列が許可されているため、Rest@Partition[#~Join~#,Length@#,1]&36バイトになります。
LegionMammal978

@ LegionMammal978ありがとう!私はまだ何も考えていませんが、おそらく文字配列の短いアプローチもあります。
ミシャラヴロフ

2

アタッシュ、13バイト

Rotate#{1:#_}

オンラインでお試しください!

説明

Rotate#{1:#_}
      #          fork(f, g) = ${ f[x, g[x]] }; this forks:
Rotate               rotate's the input by
       {1:#_}        each number from 1 to the length of the input

代替案

15バイト{_&Rotate!1:#_}

16バイト{Rotate[_,1:#_]}

16バイトRotate@Rotations

16バイトRotate#(1&`:@`#)

17バイトRotate#{1+Iota@_}

18バイトRotate#(1&`+@Iota)

19バイトRotate#(Succ=>Iota)





2

C(32ビット)、 58 51 50バイト

ceilingcatのおかげで素敵なラウンド数の場合は-1バイト

i;f(s){for(i=0;i++<printf("%s%.*s\n",s+i,i,s)-2;);}

オンラインでお試しください!

デゴルフ

i;           // "Global" i.
f(s){   // s is pointer to string, which conveniently fits in a 32 bit integer.
    for(i=0; // Initialize i.
        // Increment i and take its complement, and add it to the
        // return value of printf(); which just happens to be strlen(s)+1.
        // ~i + strlen(s) + 1 == strlen(s) + 1 - i - 1, so the last printed
        // string is the original string.
        ~++i + printf("%s%.*s\n",s+i,i,s);
        // The printf prints two strings: first until the terminating \0,
        // the second until a \0 or until i chars have been printed. It also
        // prints a linefeed.
}

提案する~++i+printf("%s%.*s\n",s+i,i,s)代わりにi++<printf("%s%.*s\n",s+i,i,s)-2
ceilingcat

@ceilingcatいつものようにありがとう!

@ceilingcat本当に呼ばれるべきflooringcatです。

1

、10バイト

⮌Eθ⭆θ§θ⁻μκ

オンラインでお試しください!リンクは、コードの詳細バージョンです。説明:

  θ         Input string
 E         Map over characters
    θ       Input string
   ⭆        Map over characters and join
      θ     Input string
     §      Circularly indexed by
       ⁻    Difference between
        μ   Inner index
         κ  Outer index
⮌           Reversed
            Implicitly print each string on its own line

反対方向に回転させるには、交換してくださいMinusPlus




1

Perl 6、32バイト

{m:ex/^(.*)(.+)$/».&{[R~] @$_}}

オンラインでお試しください!

m:ex/^(.*)(.+)$/ exm与えられた正規表現を徹底的にアタッチし、可能なすべての場所で入力文字列を分割します。ただし、2番目のサブ文字列には少なくとも1つの文字が必要です。次に、結果のMatchオブジェクトの各キャプチャグループが、逆の文字列連結演算子[]を使用して単一の文字列に縮小()されR~ます。




1

Powershell、44バイト

($s=$args|% t*y)|%{$h,$t=$s;-join($s=$t+$h)}

テストスクリプト:

$f = {

($s=$args|% t*y)|%{$h,$t=$s;-join($s=$t+$h)}

}

@(
    ,('john', 'ohnj', 'hnjo', 'njoh', 'john')
    ,('heehee', 'eeheeh', 'eheehe', 'heehee', 'eeheeh', 'eheehe', 'heehee')
) | % {
    $s,$expected = $_
    $result = &$f $s
    "$result"-eq"$expected"
    $result
}

出力:

True
ohnj
hnjo
njoh
john
True
eeheeh
eheehe
heehee
eeheeh
eheehe
heehee




1

ルビー、39バイト

->s{a=s.chars.to_a;a.map{a.rotate!*''}}

オンラインでお試しください!


1
サイトへようこそ!TIOリンクが回答に対応しているようには見えません。また、あなたの答えは、私たちの入力/出力の要件に合わないようです。関数またはSTDIN / STDOUTのいずれかを使用できますが、変数の再割り当ては許可されていません。
ウィートウィザード

ありがとう、ガーフ。どうやってこれらの両方を台無しにしたかわからない。これですべて良いはずです。
acornellier

1

JavaScript、48 43 36バイト

-5バイトは@Bubblerから提供 * -7バイトは@Shaggyから提供

入力は文字配列で、出力は文字配列の配列です。

s=>s.map(_=>([a,...b]=s,s=[...b,a]))

オンラインでお試しください!




@Shaggyこれは有効なバイト数とエントリですか?ない[..."john"]関数呼び出しの前に列に入力された文字列の操作としてカウントされませんか?
guest271314

@ guest271314、入力は文字であり、出力はチャレンジスペックとデフォルトのI / Oで許可されている文字配列の配列です。
シャギー

@Shaggy更新。上記のコメントをお願いできますか?または、混乱を避けるためにあなたのコメントを答えに含めるべきですか?それとも、どちらも必要ですか?
guest271314


1

MBASIC69 66バイト

-3バイト、ØrjanJohansenのおかげ

1 INPUT S$:L=LEN(S$):FOR I=1 TO L:S$=MID$(S$+S$,2,L):PRINT S$:NEXT

短縮できると思います1 INPUT S$:L=LEN(S$):FOR I=1 TO L:S$=MID$(S$+S$,2,L):PRINT S$:NEXT
Ørjanヨハンセン

@ØrjanJohansenありがとうございます。
wooshinyobject

1

brainfuck、38バイト

,[>>,]<<<+>[[.>>]<<[<<]>-[+>.>-].<<+>]

オンラインでお試しください!

空白文字としてヌル文字を使用するというJoKingのアイデアに基づいています。このコードは、印刷する現在の文字をマークし、左端に達するまでループします。

,[>>,]<<    input string with empty cells in between
<+>         set first marker
[           main loop
  [.>>]     print remaining characters
  <<[<<]    return to start
  >-[+>.>-] print until marker (remove marker)
  .         print null
  <<+       set new marker
  >         restart loop with next character to the left
]           stop if there's no character to the left
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.