How to scramble a string of text?

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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
add a comment |Â
up vote
0
down vote
favorite
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
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
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
string-manipulation
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
add a comment |Â
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
add a comment |Â
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"
I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on theToCharacterCodeprocessed byte data in some in-place way but this is actually much faster, even without all of theFromCharacterCodepost-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 usingReverseandParton 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 withStringDropis also slower thanStringTake[#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
add a comment |Â
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"
add a comment |Â
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"
add a comment |Â
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"
I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on theToCharacterCodeprocessed byte data in some in-place way but this is actually much faster, even without all of theFromCharacterCodepost-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 usingReverseandParton 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 withStringDropis also slower thanStringTake[#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
add a comment |Â
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"
I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on theToCharacterCodeprocessed byte data in some in-place way but this is actually much faster, even without all of theFromCharacterCodepost-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 usingReverseandParton 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 withStringDropis also slower thanStringTake[#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
add a comment |Â
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"
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"
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 theToCharacterCodeprocessed byte data in some in-place way but this is actually much faster, even without all of theFromCharacterCodepost-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 usingReverseandParton 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 withStringDropis also slower thanStringTake[#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
add a comment |Â
I'm actually surprised at how efficient this is. I would have assumed the fastest way would be to work on theToCharacterCodeprocessed byte data in some in-place way but this is actually much faster, even without all of theFromCharacterCodepost-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 usingReverseandParton 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 withStringDropis also slower thanStringTake[#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
add a comment |Â
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"
add a comment |Â
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"
add a comment |Â
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"
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"
edited Oct 1 at 7:05
answered Oct 1 at 2:26
kglr
164k8188388
164k8188388
add a comment |Â
add a comment |Â
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"
add a comment |Â
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"
add a comment |Â
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"
Another way:
Block[k = 1, s = "abcdef",
NestList[StringReplacePart[#, StringReverse[StringTake[#, ++k]], 1, k] &,
s, StringLength[s] - 1]]
"abcdef", "bacdef", "cabdef", "dbacef", "ecabdf", "fdbace"
answered Oct 1 at 15:06
community wiki
J. M. is somewhat okay.
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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