14
\$\begingroup\$

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
\$\endgroup\$
9
\$\begingroup\$

CJam, 41 38 bytes

lee_S+W%\{Xa-X1='`-/(Xa+\L*+}fX1>W%1f=

Test it here.

\$\endgroup\$
  • \$\begingroup\$ Upvoting as it's the only other answer that works on all of my test cases. \$\endgroup\$ – Neil Jan 8 '16 at 23:31
  • \$\begingroup\$ Shortest to pass all test cases! \$\endgroup\$ – geokavel Jan 14 '16 at 19:57
4
\$\begingroup\$

Python 3, 78 bytes.

Saved 2 bytes thanks to orlp.
Saved 7 bytes thanks to DSM.

x=input()
y=[]
for z in x:m=max(len(x)-ord(z)+96,0);y[m:m]=z
print(''.join(y))

Builds the word as a list then joins it.

\$\endgroup\$
  • \$\begingroup\$ (q-p,0)[p>q] is longer than min(q-p,0). \$\endgroup\$ – orlp Jan 7 '16 at 20:49
  • \$\begingroup\$ It is, but that doesn't do the same thing. That's always going to return 0 or a negative. \$\endgroup\$ – Morgan Thrapp Jan 7 '16 at 20:52
  • \$\begingroup\$ Sorry, I meant max(q-p,0). \$\endgroup\$ – orlp Jan 7 '16 at 20:54
3
\$\begingroup\$

Python 2, 86 bytes

a=input();k=list(a)
for i in a:k.remove(i);k.insert(ord(i)-97,i)
print"".join(k)[::-1]

Python 3, 88 bytes

a=input();k=list(a)
for i in a:k.remove(i);k.insert(ord(i)-97,i)
print("".join(k)[::-1])

Examples

Python 2:

$ python2 test.py
"flower"
rweolf

Python 3:

$ python3 test.py
flower
rweolf
\$\endgroup\$
  • 3
    \$\begingroup\$ k.remove removes the first instance, so this is going to fail for something like baa. \$\endgroup\$ – Sp3000 Jan 7 '16 at 21:50
2
\$\begingroup\$

Javascript ES6, 136 134 131 bytes

s=>([...s].map(c=>{s=s.replace(c,'');p=s.length+97-c.charCodeAt();s=s.substr(0,p)+c.toUpperCase()+s.substring(p)}),s.toLowerCase())

Note that I take great care not to move the same character twice, otherwise pizza turns into zipza when it should be zzipa. There's also an edge case dealing with not removing characters prematurely; characters becomes maybe srtrchaeac or srtrheccaa if you do it wrongly but it should be srtrhcecaa. Another tricky word is abracadabra for which the output rrabaaadcba would be incorrect; rrbcdaaabaa would be correct.

Edit: Shaved off two bytes by using substring which automatically coerces its arguments to the range 0..length.

Edit: Shaved off three bytes by changing the first substring to substr as suggested by user81665.

\$\endgroup\$
  • \$\begingroup\$ I think you could use substr instead of substring. \$\endgroup\$ – user81655 Jan 8 '16 at 1:44
  • \$\begingroup\$ slice is better (I think). \$\endgroup\$ – Mama Fun Roll Jan 8 '16 at 2:07
  • \$\begingroup\$ @ՊՓԼՃՐՊՃՈԲՍԼ He can't because passing negative numbers into slice breaks it. \$\endgroup\$ – user81655 Jan 8 '16 at 2:28
  • \$\begingroup\$ oh forgot about that. \$\endgroup\$ – Mama Fun Roll Jan 8 '16 at 2:28
  • \$\begingroup\$ Yeah, there was a little mistake with the pizza test case you put on my post, but I fixed it. \$\endgroup\$ – geokavel Jan 9 '16 at 7:23
1
\$\begingroup\$

Pyth, 18 17 bytes

uXeS,Z-lzhx;HGHzk

Test Suite.

Iterates using reduce over the input string, inserting into a string, base case empty string, at the correct position.

\$\endgroup\$
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 23 chars / 40 bytes

ᴉⓜΞăМƲ ïꝈ-ᶛą$,0),0,$;Ξ⨝

Try it here (Firefox only).

Explanation

ᴉⓜΞăМƲ ïꝈ-ᶛą$,0),0,$;Ξ⨝ // implicit: ï=input, ᴉ=input split into chars, Ξ=empty array, ᶛ=lowercase alphabet
ᴉⓜ                      // map over input chars
   ΞăМƲ ïꝈ-ᶛą$,0),0,$;   // use splice to insert map item into Ξ at requested index
                      Ξ⨝ // join Ξ
                         // implicit output
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.