Letters, Get Moving! Pt. 2

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

geokavel

Posted 2016-01-07T17:37:23.170

Reputation: 6 352

Answers

9

CJam, 41 38 bytes

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

Test it here.

Martin Ender

Posted 2016-01-07T17:37:23.170

Reputation: 184 808

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

4

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.

Morgan Thrapp

Posted 2016-01-07T17:37:23.170

Reputation: 3 574

(q-p,0)[p>q] is longer than min(q-p,0). – orlp – 2016-01-07T20:49:59.853

It is, but that doesn't do the same thing. That's always going to return 0 or a negative. – Morgan Thrapp – 2016-01-07T20:52:01.757

Sorry, I meant max(q-p,0). – orlp – 2016-01-07T20:54:40.807

3

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

Zach Gates

Posted 2016-01-07T17:37:23.170

Reputation: 6 152

3k.remove removes the first instance, so this is going to fail for something like baa. – Sp3000 – 2016-01-07T21:50:54.337

2

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.

Neil

Posted 2016-01-07T17:37:23.170

Reputation: 95 035

I think you could use substr instead of substring. – user81655 – 2016-01-08T01:44:45.600

slice is better (I think). – Mama Fun Roll – 2016-01-08T02:07:09.267

@ՊՓԼՃՐՊՃՈԲՍԼ He can't because passing negative numbers into slice breaks it. – user81655 – 2016-01-08T02:28:45.640

oh forgot about that. – Mama Fun Roll – 2016-01-08T02:28:55.660

Yeah, there was a little mistake with the pizza test case you put on my post, but I fixed it. – geokavel – 2016-01-09T07:23:48.580

Ugh, and I thought I'd checked against my answer too. Very sorry about that, and also the other typos that crept in (my fault for doing it so late at night.) – Neil – 2016-01-09T13:01:02.740

1

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.

Maltysen

Posted 2016-01-07T17:37:23.170

Reputation: 25 023

1

, 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

Mama Fun Roll

Posted 2016-01-07T17:37:23.170

Reputation: 7 234