How to scramble a string of text?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1












How to implement foo function which works like this:



foo @ "abcdef" 



"fdbace"



Steps: "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"



Reverse the first two letter of the string "abcdef" which will give "bacdef". Then take this as the new string and reverse the first 3 letter. Similarly, proceed for all the characters of the string.










share|improve this question



















  • 11




    OK, I gotta say this: are you seriously asking this place to solve Pancake Scramble? Your questions are starting to form a pattern.
    – J. M. is somewhat okay.♦
    Sep 30 at 21:32











  • Possible duplicate of Looking for "Longest Common Substring" solution
    – user59769
    Oct 2 at 4:23














up vote
0
down vote

favorite
1












How to implement foo function which works like this:



foo @ "abcdef" 



"fdbace"



Steps: "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"



Reverse the first two letter of the string "abcdef" which will give "bacdef". Then take this as the new string and reverse the first 3 letter. Similarly, proceed for all the characters of the string.










share|improve this question



















  • 11




    OK, I gotta say this: are you seriously asking this place to solve Pancake Scramble? Your questions are starting to form a pattern.
    – J. M. is somewhat okay.♦
    Sep 30 at 21:32











  • Possible duplicate of Looking for "Longest Common Substring" solution
    – user59769
    Oct 2 at 4:23












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





How to implement foo function which works like this:



foo @ "abcdef" 



"fdbace"



Steps: "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"



Reverse the first two letter of the string "abcdef" which will give "bacdef". Then take this as the new string and reverse the first 3 letter. Similarly, proceed for all the characters of the string.










share|improve this question















How to implement foo function which works like this:



foo @ "abcdef" 



"fdbace"



Steps: "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"



Reverse the first two letter of the string "abcdef" which will give "bacdef". Then take this as the new string and reverse the first 3 letter. Similarly, proceed for all the characters of the string.







string-manipulation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 1 at 7:11









Kuba♦

100k11195495




100k11195495










asked Sep 30 at 20:37







user59769














  • 11




    OK, I gotta say this: are you seriously asking this place to solve Pancake Scramble? Your questions are starting to form a pattern.
    – J. M. is somewhat okay.♦
    Sep 30 at 21:32











  • Possible duplicate of Looking for "Longest Common Substring" solution
    – user59769
    Oct 2 at 4:23












  • 11




    OK, I gotta say this: are you seriously asking this place to solve Pancake Scramble? Your questions are starting to form a pattern.
    – J. M. is somewhat okay.♦
    Sep 30 at 21:32











  • Possible duplicate of Looking for "Longest Common Substring" solution
    – user59769
    Oct 2 at 4:23







11




11




OK, I gotta say this: are you seriously asking this place to solve Pancake Scramble? Your questions are starting to form a pattern.
– J. M. is somewhat okay.♦
Sep 30 at 21:32





OK, I gotta say this: are you seriously asking this place to solve Pancake Scramble? Your questions are starting to form a pattern.
– J. M. is somewhat okay.♦
Sep 30 at 21:32













Possible duplicate of Looking for "Longest Common Substring" solution
– user59769
Oct 2 at 4:23




Possible duplicate of Looking for "Longest Common Substring" solution
– user59769
Oct 2 at 4:23










3 Answers
3






active

oldest

votes

















up vote
11
down vote



accepted










s = "abcdef";

FoldList[
StringJoin[
StringReverse[StringTake[#1, 1, #2]],
StringTake[#1, #2 + 1, -1]
] &,
s,
Range[2, StringLength[s]]
]



"abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"







share|improve this answer






















  • I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on the ToCharacterCode processed byte data in some in-place way but this is actually much faster, even without all of the FromCharacterCode post-processing. On a string of length 5000 it's actually faster even than in place reversal of the bytes themselves in an array which really surprised me. To add to that, it's even faster than the corresponding code using Reverse and Part on the list of bytes which has all sorts of odd implications.
    – b3m2a1
    Oct 1 at 0:02







  • 1




    @b3m3a1 Yeah, I was also somewhat surprised when I tried to optimize the code. Usually, String operations in Mathematice tend to be rather slow...
    – Henrik Schumacher
    Oct 1 at 0:11










  • I wonder what part is the real holdup. I'm really surprised that working with packed arrays of integers is slower than working with strings. Also it seems writing your solution with StringDrop is also slower than StringTake[#1, #2 + 1 ;;] which is again very surprising. The specially made operation should be the fastest one, to my eyes.
    – b3m2a1
    Oct 1 at 0:15







  • 2




    It turns out String is really fast now
    – b3m2a1
    Oct 1 at 0:56

















up vote
3
down vote













FoldList[StringReplace[#, StartOfString ~~ p : Repeated[_, #2] :> 
StringReverse[p]] &, "abcdef" , Range[2, 6]]



"abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"




Also



ClearAll[f1, f2]
f1[k_] := Module[c = TakeDrop[Characters[#], k],
StringJoin[c[[1]][[-1 ;; 1 ;; -2]], c[[1]][[1 + Boole[OddQ[k]] ;; ;; 2]], c[[2]]] ] &
f2 = Table[f1[k]@# , k, StringLength @ #] &;

f2 @ "abcdef"



"abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"







share|improve this answer





























    up vote
    3
    down vote













    Another way:



    Block[k = 1, s = "abcdef",
    NestList[StringReplacePart[#, StringReverse[StringTake[#, ++k]], 1, k] &,
    s, StringLength[s] - 1]]
    "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"





    share|improve this answer






















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "387"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182894%2fhow-to-scramble-a-string-of-text%23new-answer', 'question_page');

      );

      Post as a guest





























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      11
      down vote



      accepted










      s = "abcdef";

      FoldList[
      StringJoin[
      StringReverse[StringTake[#1, 1, #2]],
      StringTake[#1, #2 + 1, -1]
      ] &,
      s,
      Range[2, StringLength[s]]
      ]



      "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"







      share|improve this answer






















      • I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on the ToCharacterCode processed byte data in some in-place way but this is actually much faster, even without all of the FromCharacterCode post-processing. On a string of length 5000 it's actually faster even than in place reversal of the bytes themselves in an array which really surprised me. To add to that, it's even faster than the corresponding code using Reverse and Part on the list of bytes which has all sorts of odd implications.
        – b3m2a1
        Oct 1 at 0:02







      • 1




        @b3m3a1 Yeah, I was also somewhat surprised when I tried to optimize the code. Usually, String operations in Mathematice tend to be rather slow...
        – Henrik Schumacher
        Oct 1 at 0:11










      • I wonder what part is the real holdup. I'm really surprised that working with packed arrays of integers is slower than working with strings. Also it seems writing your solution with StringDrop is also slower than StringTake[#1, #2 + 1 ;;] which is again very surprising. The specially made operation should be the fastest one, to my eyes.
        – b3m2a1
        Oct 1 at 0:15







      • 2




        It turns out String is really fast now
        – b3m2a1
        Oct 1 at 0:56














      up vote
      11
      down vote



      accepted










      s = "abcdef";

      FoldList[
      StringJoin[
      StringReverse[StringTake[#1, 1, #2]],
      StringTake[#1, #2 + 1, -1]
      ] &,
      s,
      Range[2, StringLength[s]]
      ]



      "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"







      share|improve this answer






















      • I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on the ToCharacterCode processed byte data in some in-place way but this is actually much faster, even without all of the FromCharacterCode post-processing. On a string of length 5000 it's actually faster even than in place reversal of the bytes themselves in an array which really surprised me. To add to that, it's even faster than the corresponding code using Reverse and Part on the list of bytes which has all sorts of odd implications.
        – b3m2a1
        Oct 1 at 0:02







      • 1




        @b3m3a1 Yeah, I was also somewhat surprised when I tried to optimize the code. Usually, String operations in Mathematice tend to be rather slow...
        – Henrik Schumacher
        Oct 1 at 0:11










      • I wonder what part is the real holdup. I'm really surprised that working with packed arrays of integers is slower than working with strings. Also it seems writing your solution with StringDrop is also slower than StringTake[#1, #2 + 1 ;;] which is again very surprising. The specially made operation should be the fastest one, to my eyes.
        – b3m2a1
        Oct 1 at 0:15







      • 2




        It turns out String is really fast now
        – b3m2a1
        Oct 1 at 0:56












      up vote
      11
      down vote



      accepted







      up vote
      11
      down vote



      accepted






      s = "abcdef";

      FoldList[
      StringJoin[
      StringReverse[StringTake[#1, 1, #2]],
      StringTake[#1, #2 + 1, -1]
      ] &,
      s,
      Range[2, StringLength[s]]
      ]



      "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"







      share|improve this answer














      s = "abcdef";

      FoldList[
      StringJoin[
      StringReverse[StringTake[#1, 1, #2]],
      StringTake[#1, #2 + 1, -1]
      ] &,
      s,
      Range[2, StringLength[s]]
      ]



      "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"








      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 1 at 15:52

























      answered Sep 30 at 21:13









      Henrik Schumacher

      41k258124




      41k258124











      • I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on the ToCharacterCode processed byte data in some in-place way but this is actually much faster, even without all of the FromCharacterCode post-processing. On a string of length 5000 it's actually faster even than in place reversal of the bytes themselves in an array which really surprised me. To add to that, it's even faster than the corresponding code using Reverse and Part on the list of bytes which has all sorts of odd implications.
        – b3m2a1
        Oct 1 at 0:02







      • 1




        @b3m3a1 Yeah, I was also somewhat surprised when I tried to optimize the code. Usually, String operations in Mathematice tend to be rather slow...
        – Henrik Schumacher
        Oct 1 at 0:11










      • I wonder what part is the real holdup. I'm really surprised that working with packed arrays of integers is slower than working with strings. Also it seems writing your solution with StringDrop is also slower than StringTake[#1, #2 + 1 ;;] which is again very surprising. The specially made operation should be the fastest one, to my eyes.
        – b3m2a1
        Oct 1 at 0:15







      • 2




        It turns out String is really fast now
        – b3m2a1
        Oct 1 at 0:56
















      • I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on the ToCharacterCode processed byte data in some in-place way but this is actually much faster, even without all of the FromCharacterCode post-processing. On a string of length 5000 it's actually faster even than in place reversal of the bytes themselves in an array which really surprised me. To add to that, it's even faster than the corresponding code using Reverse and Part on the list of bytes which has all sorts of odd implications.
        – b3m2a1
        Oct 1 at 0:02







      • 1




        @b3m3a1 Yeah, I was also somewhat surprised when I tried to optimize the code. Usually, String operations in Mathematice tend to be rather slow...
        – Henrik Schumacher
        Oct 1 at 0:11










      • I wonder what part is the real holdup. I'm really surprised that working with packed arrays of integers is slower than working with strings. Also it seems writing your solution with StringDrop is also slower than StringTake[#1, #2 + 1 ;;] which is again very surprising. The specially made operation should be the fastest one, to my eyes.
        – b3m2a1
        Oct 1 at 0:15







      • 2




        It turns out String is really fast now
        – b3m2a1
        Oct 1 at 0:56















      I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on the ToCharacterCode processed byte data in some in-place way but this is actually much faster, even without all of the FromCharacterCode post-processing. On a string of length 5000 it's actually faster even than in place reversal of the bytes themselves in an array which really surprised me. To add to that, it's even faster than the corresponding code using Reverse and Part on the list of bytes which has all sorts of odd implications.
      – b3m2a1
      Oct 1 at 0:02





      I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on the ToCharacterCode processed byte data in some in-place way but this is actually much faster, even without all of the FromCharacterCode post-processing. On a string of length 5000 it's actually faster even than in place reversal of the bytes themselves in an array which really surprised me. To add to that, it's even faster than the corresponding code using Reverse and Part on the list of bytes which has all sorts of odd implications.
      – b3m2a1
      Oct 1 at 0:02





      1




      1




      @b3m3a1 Yeah, I was also somewhat surprised when I tried to optimize the code. Usually, String operations in Mathematice tend to be rather slow...
      – Henrik Schumacher
      Oct 1 at 0:11




      @b3m3a1 Yeah, I was also somewhat surprised when I tried to optimize the code. Usually, String operations in Mathematice tend to be rather slow...
      – Henrik Schumacher
      Oct 1 at 0:11












      I wonder what part is the real holdup. I'm really surprised that working with packed arrays of integers is slower than working with strings. Also it seems writing your solution with StringDrop is also slower than StringTake[#1, #2 + 1 ;;] which is again very surprising. The specially made operation should be the fastest one, to my eyes.
      – b3m2a1
      Oct 1 at 0:15





      I wonder what part is the real holdup. I'm really surprised that working with packed arrays of integers is slower than working with strings. Also it seems writing your solution with StringDrop is also slower than StringTake[#1, #2 + 1 ;;] which is again very surprising. The specially made operation should be the fastest one, to my eyes.
      – b3m2a1
      Oct 1 at 0:15





      2




      2




      It turns out String is really fast now
      – b3m2a1
      Oct 1 at 0:56




      It turns out String is really fast now
      – b3m2a1
      Oct 1 at 0:56










      up vote
      3
      down vote













      FoldList[StringReplace[#, StartOfString ~~ p : Repeated[_, #2] :> 
      StringReverse[p]] &, "abcdef" , Range[2, 6]]



      "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"




      Also



      ClearAll[f1, f2]
      f1[k_] := Module[c = TakeDrop[Characters[#], k],
      StringJoin[c[[1]][[-1 ;; 1 ;; -2]], c[[1]][[1 + Boole[OddQ[k]] ;; ;; 2]], c[[2]]] ] &
      f2 = Table[f1[k]@# , k, StringLength @ #] &;

      f2 @ "abcdef"



      "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"







      share|improve this answer


























        up vote
        3
        down vote













        FoldList[StringReplace[#, StartOfString ~~ p : Repeated[_, #2] :> 
        StringReverse[p]] &, "abcdef" , Range[2, 6]]



        "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"




        Also



        ClearAll[f1, f2]
        f1[k_] := Module[c = TakeDrop[Characters[#], k],
        StringJoin[c[[1]][[-1 ;; 1 ;; -2]], c[[1]][[1 + Boole[OddQ[k]] ;; ;; 2]], c[[2]]] ] &
        f2 = Table[f1[k]@# , k, StringLength @ #] &;

        f2 @ "abcdef"



        "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"







        share|improve this answer
























          up vote
          3
          down vote










          up vote
          3
          down vote









          FoldList[StringReplace[#, StartOfString ~~ p : Repeated[_, #2] :> 
          StringReverse[p]] &, "abcdef" , Range[2, 6]]



          "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"




          Also



          ClearAll[f1, f2]
          f1[k_] := Module[c = TakeDrop[Characters[#], k],
          StringJoin[c[[1]][[-1 ;; 1 ;; -2]], c[[1]][[1 + Boole[OddQ[k]] ;; ;; 2]], c[[2]]] ] &
          f2 = Table[f1[k]@# , k, StringLength @ #] &;

          f2 @ "abcdef"



          "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"







          share|improve this answer














          FoldList[StringReplace[#, StartOfString ~~ p : Repeated[_, #2] :> 
          StringReverse[p]] &, "abcdef" , Range[2, 6]]



          "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"




          Also



          ClearAll[f1, f2]
          f1[k_] := Module[c = TakeDrop[Characters[#], k],
          StringJoin[c[[1]][[-1 ;; 1 ;; -2]], c[[1]][[1 + Boole[OddQ[k]] ;; ;; 2]], c[[2]]] ] &
          f2 = Table[f1[k]@# , k, StringLength @ #] &;

          f2 @ "abcdef"



          "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 1 at 7:05

























          answered Oct 1 at 2:26









          kglr

          164k8188388




          164k8188388




















              up vote
              3
              down vote













              Another way:



              Block[k = 1, s = "abcdef",
              NestList[StringReplacePart[#, StringReverse[StringTake[#, ++k]], 1, k] &,
              s, StringLength[s] - 1]]
              "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"





              share|improve this answer


























                up vote
                3
                down vote













                Another way:



                Block[k = 1, s = "abcdef",
                NestList[StringReplacePart[#, StringReverse[StringTake[#, ++k]], 1, k] &,
                s, StringLength[s] - 1]]
                "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"





                share|improve this answer
























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  Another way:



                  Block[k = 1, s = "abcdef",
                  NestList[StringReplacePart[#, StringReverse[StringTake[#, ++k]], 1, k] &,
                  s, StringLength[s] - 1]]
                  "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"





                  share|improve this answer














                  Another way:



                  Block[k = 1, s = "abcdef",
                  NestList[StringReplacePart[#, StringReverse[StringTake[#, ++k]], 1, k] &,
                  s, StringLength[s] - 1]]
                  "abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  answered Oct 1 at 15:06


























                  community wiki





                  J. M. is somewhat okay.




























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182894%2fhow-to-scramble-a-string-of-text%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)