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.
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"}
Comments
Post a Comment