14
2
The first Letters, Get Moving! was very popular, but had limited participation. This one will be easier to solve, but hopefully involve some tricks in golfing.
You are given a string of only lowercase letters. For each letter, with position in the alphabet m, move it so it becomes the mth letter from the end. If the value of m is longer than the length of the string, move it to the very front. Output only the fully transformed string.
Examples:
"giraffe"
- 'g' is the 7th letter in the alphabet, it is already the 7th letter from the back, so leave it.
- 'i' is the 9th letter, since 9 is bigger than the length of the word, it goes to the front, so the string becomes
igraffe
- 'r' is the 18th letter, like 'i' it goes to the front:
rigaffe
- 'a' is the 1st letter, it goes to the very end:
rigffea
- 'f' is the 6th letter, it becomes the 6th from the back:
rfigfea
- the next 'f' is also the 6th letter, so it also goes to 6th from the back :
rffigea
- 'e' is the 5th letters, it goes to 5th from the back:
rfefiga
"flower"
- 'f' (6) =>
flower
- 'l' (12) =>
lfower
- 'o' (15) =>
olfwer
- 'w' (23) =>
wolfer
- 'e' (5) =>
weolfr
- 'r' (18) =>
rweolf
"pineapple"
- 'p' (16) =>
pineapple
- 'i' (9) =>
ipneapple
- 'n' (14) =>
nipeapple
- 'e' (5) =>
nipaepple
- 'a' (1) =>
nipepplea
- 'p' (16) =>
pnipeplea
- 'p' (16) =>
ppnipelea
- 'l' (12) =>
lppnipeea
- 'e' (5) =>
lppneipea
(make sure you move the e that hasn't been moved already! Here it doesn't matter, but below it does.)
Thanks to @Neil for improving the test cases with these 3 additions:
"pizza"
- 'p' (16) =>
pizza
- 'i' (9) =>
ipzza
- 'z' (26) =>
zipza
- 'z' (26) =>
zzipa
(moving the second z!) - 'a' (1) =>
zzipa
"abracadabra"
- 'a' (1) =>
bracadabraa
- 'b' (2) =>
racadabraba
- 'r' (18) =>
racadabraba
- 'a' (1) =>
rcadabrabaa
- 'c' (3) =>
radabrabcaa
- 'a' (1) =>
rdabrabcaaa
- 'd' (4) =>
rabrabcdaaa
- 'a' (1) =>
rbrabcdaaaa
- 'b' (2) =>
rrabcdaaaba
- 'r' (18) =>
rrabcdaaaba
- 'a' (1) =>
rrbcdaaabaa
"characters"
- 'c' (3) =>
haractecrs
- 'h' (8) =>
arhactecrs
- 'a' (1) =>
rhactecrsa
- 'r' (18) =>
rhactecrsa
- 'a' (1) =>
rhctecrsaa
- 'c' (3) =>
rhtecrscaa
- 't' (20) =>
trhecrscaa
- 'e' (5) =>
trhcrescaa
- 'r' (18) =>
rtrhcescaa
- 's' (19) =>
srtrhcecaa
Upvoting as it's the only other answer that works on all of my test cases. – Neil – 2016-01-08T23:31:19.803
Shortest to pass all test cases! – geokavel – 2016-01-14T19:57:16.363