The Speed of Letters

43

1

Given a string as input, print a new string with each letter pushed to the right by its respective alphabet index.

We all know that A is a slow and Z is a fast letter. This means that Z gets shifted to the right by 25 spaces, A doesn't get shifted at all and B gets shifted by 1 space.

Your program only has to handle uppercase letters from A-Z, and no other characters, no whitespaces, no punctuation.

Note that if 2 or more letters fall onto the same space after shifting, the latest character will be used. (Example: BA ->  A)

Examples

"AZ" -> "A                         Z"

"ABC" -> "A B C"

"ACE" -> "A  C  E"

"CBA" -> "  A"

"HELLOWORLD" -> "     E H    DLL   OLO   R  W"

Rules

  • This is , so the shortest code in any language bytes wins.
  • Standard loopholes are forbidden.

  • Input must be received as a string.

  • You may print the result to stdout or return a string.
  • A single trailing whitespace and/or newline is allowed.
  • You may also use lowercase letters as input or output, but only use either case.

Ian H.

Posted 2017-10-13T13:38:38.237

Reputation: 2 431

Trailing whitespace okay? – Okx – 2017-10-13T13:56:56.760

@Okx Yes, note my previous comment. – Ian H. – 2017-10-13T13:58:27.587

1What about a lot of trailing spaces? – Okx – 2017-10-13T13:59:04.210

1@Okx Forbidden, one is all you get. – Ian H. – 2017-10-13T13:59:29.400

I assume we can use lowercase letters instead, right? – Mr. Xcoder – 2017-10-13T15:53:52.480

@Mr.Xcoder If you keep it consistently at only lowercase letters, yes. – Ian H. – 2017-10-13T19:23:41.190

Is it alright to have a single leading space? – miles – 2017-10-14T02:07:50.010

@miles Leading newlines yes, but no leading spaces. – Ian H. – 2017-10-14T06:14:32.567

Answers

13

Python 2, 81 bytes

t=[]
i=65
for c in input():t+=[' ']*26;t[ord(c)-i]=c;i-=1
print`t`[2::5].rstrip()

Try it online!

Lynn

Posted 2017-10-13T13:38:38.237

Reputation: 55 648

11

MATL, 11 bytes

''jtfy65-+(

Try it online! Or verify all test cases.

Explanation

MATL indexing is 1-based. This golfing trick is used here. This other one cannot be used because we need an empty string, not an empty numeric array.

Consider input 'ACE' as an example. Stack contents are shown bottom to top.

''     % Push empty string
       %   Stack: ''
j      % Input string
       %   Stack: '', 'ACE'
t      % Duplicate
       %   Stack: '', 'ACE', 'ACE'
f      % Indices of nonzero entries. Gives [1 2 ... n] where n is input length
       %   Stack: '', 'ACE', [1 2 3]
y      % Duplicate from below
       %   Stack: '', 'ACE', [1 2 3], 'ACE'
65     % Push 65
       %   Stack: '', 'ACE', [1 2 3], 'ACE', 65
-      % Subtract, element-wise. Characters are converted to codepoints
       %   Stack: '', 'ACE', [1 2 3], [0 2 4]
+      % Add, element-wise
       %   Stack: '', 'ACE', [1 4 7]
(      % Fill string '' with values 'ACE' at positions [1 4 7]. The original
       % empty string is extended. Non-existing values are filled with char 0,
       % which is displayed as space. Implicitly display
       %   Stack: 'A  C  E'

Luis Mendo

Posted 2017-10-13T13:38:38.237

Reputation: 87 464

5(: nice builtin – Erik the Outgolfer – 2017-10-13T14:39:15.050

@EriktheOutgolfer I think it's similar to 05AB1E's ǝ? Ah, but that doesn't seem to vectorize over the second/third inputs – Luis Mendo – 2017-10-13T14:46:06.467

1Exactly why it's unique :p and also how MATL auto-fills with 0s and displays 0 as space. – Erik the Outgolfer – 2017-10-13T14:50:05.207

1@LuisMendo Nice answer. Out of curiosity, and this question is probably applicable to most stack based langs, when you write in MATL do you need to keep track of the stack (eg, in a line above the code, or a piece of paper, etc) as you compose code? Or has it become natural enough to you that you don't? – Jonah – 2017-10-14T01:20:18.350

@Jonah Thanks! In the beginning I used to need to keep track of the stack on paper. Now it has become natural, and I only need it in more complicated programs – Luis Mendo – 2017-10-14T10:03:21.660

1Heh - learned something new today; you can index into the same position twice without any problem in MATL(AB). I already started writing a complicated loop-based answer because I assumed it would give an error otherwise. – Sanchises – 2017-10-15T12:07:00.217

7

R, 140 133 129 74 bytes

Saved a ton of bytes porting an ASCII value approach like everyone else. Sad I didn't think of it before :(

function(s){F[X-65+1:sum(X|1)]=X=utf8ToInt(s)
F[is.na(F)]=32
intToUtf8(F)}

Try it online!

original answer, 129 bytes:

function(s){o=rep(' ',(n=nchar(s))+25)
for(i in 1:n){k=substr(s,i,i)
o[x<-i+match(k,LETTERS)-1]=k
F=max(F,x)}
cat(o[1:F],sep='')}

Try it online!

generates a too-long list o of spaces, then iterates through s, replacing the values in o with the correct value and updating F, the position of the rightmost character. Then prints out the first F elements of o with no separators between them.

Giuseppe

Posted 2017-10-13T13:38:38.237

Reputation: 21 077

6

05AB1E, 20 16 bytes

-4 bytes thanks to Emigna

ð₄×svyAuykN+ǝ}ðÜ

Try it online!

Okx

Posted 2017-10-13T13:38:38.237

Reputation: 15 025

1I think you can shorten to ð₄×svyAuykN+ǝ}ðÜ at least. Also, is there a guarantee that the input string shifted isn't larger than 1000 chars? If not, g₂+ð× should work. – Emigna – 2017-10-13T14:10:56.543

6

JavaScript (ES6), 81 bytes

s=>[...s].map((c,i)=>a[i+parseInt(c,36)-10]=c,a=[])&&[...a].map(c=>c||" ").join``

Somewhat builds off of Rick Hitchcock's incomplete answer but ended up rather different.

Places the characters into their respective index of an empty array, then uses array spread ([...a]) to turn the missing elements into undefined, allowing map to replace empty elements with a space.

Test Cases

let f=
s=>[...s].map((c,i)=>a[i+parseInt(c,36)-10]=c,a=[])&&[...a].map(c=>c||" ").join``

;["AZ", "ABC", "ACE", "CBA", "HELLOWORLD"]
.forEach(t=>console.log(`"${t}"`, "=>", `"${f(t)}"`))

Justin Mariner

Posted 2017-10-13T13:38:38.237

Reputation: 4 746

Very nice! I was about to post an 88-byte solution, but yours is much better. – Rick Hitchcock – 2017-10-13T19:45:32.600

5

Perl 5, 42 bytes

41 bytes code + 1 for -p. The \x1bs in the code are literal escape characters.

Relies on ANSI escape sequences to position the cursor and therefore doesn't work on TIO.

s/./($-=-65+ord$&)?"\x1b[$-C$&\x1b[--$-D":$&/ge

Usage

perl -pe 's/./($-=-65+ord$&)?"\x1b[$-C$&\x1b[--$-D":$&/ge' <<< 'HELLOWORLD'
     E H    DLL   OLO   R  W

Dom Hastings

Posted 2017-10-13T13:38:38.237

Reputation: 16 415

1

Here's one that works on TIO but comes in at one byte more (41 bytes of code + 2 for -F): Try it online!

– Xcali – 2017-10-13T18:03:52.383

1@Xcali You should post that so I can upvote it :) – Lynn – 2017-10-13T18:46:36.903

1@Xcali Agree with Lynn too more posts are great. I like language competition too! – Dom Hastings – 2017-10-13T21:08:30.843

5

Java (OpenJDK 8), 207 191 189 183 178 174 173 170 bytes

s->{char i=0,l,c[]=new char[s.chars().map(j->j+s.lastIndexOf(j)).max().getAsInt()-64];for(;i<s.length();c[i+l-66]=l)l=s.charAt(i++);return"".valueOf(c).replace('',' ');}

Try it online!

Roberto Graham

Posted 2017-10-13T13:38:38.237

Reputation: 1 305

4

Perl 5, 41 + (-F) = 43 bytes

map$r[$i++-65+ord]=$_,@F;print$_||$"for@r

Try it online!

Just for @lynn

Xcali

Posted 2017-10-13T13:38:38.237

Reputation: 7 671

4

brainfuck, 127 bytes

,[[-<+<+>>]----[----<<->>]<<-[[>]>++++>[-<[-]<+>>]<[-<++++++++>]<[<]>-]>[>]<[-]+[<]>-[[>]<+[<]>-]>.[-]>[>]<[[->>+<<]<]>,]>>[.>]

Try it online!

Explanation

,[                    Take input and start main loop
  [-<+<+>>]             Make two copies of input byte
  ----[----<<->>]<<-    Subtract 64 from one of them to get position in alphabet

                        There are two zero cells between the input and the
                        remaining output cells; we wish to move these zeroes
                        to indicate where the letter is to be moved

  [                     A number of times equal to the position in the alphabet:
    [>]                   Go to current position in output string
    >++++>                Create 4 (as part of creating a space if needed)
    [-<[-]<+>>]           Move output byte back two cells; zero the previous 4 if output existed
    <[-<++++++++>]        Otherwise move a space (32) into that position
    <[<]>-                Move back to counter and decrement
  ]
  >[>]<[-]              Delete last moved byte to make room for input byte
  +[<]>-                Initialize slot at 1 so it is always nonzero in this loop
  [[>]<+[<]>-]          Move input byte into slot
  >.[-]                 Output next output byte and clear
  >[>]<                 Move to space vacated in preparation to remove gap
                        (Moves to end instead if input was A; this causes no problems)
  [[->>+<<]<]           Move values two cells right until zero reached
  >,                    Get into position and take another byte of input
]
>>[.>]                Output characters beyond end of input

Nitrodon

Posted 2017-10-13T13:38:38.237

Reputation: 9 181

3

Proton, 78 bytes

x=>{t=[' ']*26*(q=len(x))for i:0..q{t[i+ord(k=x[i])-65]=k}"".join(t).rstrip()}

Try it online!

69 bytes by porting Lynn's solution: x=>{t=[]i=65for k:x{t+=[' ']*26t[ord(k)-i]=k;i--}"".join(t).rstrip()}

HyperNeutrino

Posted 2017-10-13T13:38:38.237

Reputation: 26 575

3

Jelly, 20 bytes

ØAiЀ+JṬ€a"⁸Zḟ€0Ṫ€o⁶

Try it online!

Leaky Nun

Posted 2017-10-13T13:38:38.237

Reputation: 45 011

3

Haskell, 90 88 bytes

d=drop 1
[a]#s|s<"A"=a:d s|0<1=s
b#s=(s++" ")!!0:d b#d s
f(c:t)=['A'..c]#(' ':f t)
f s=s

Try it online!

Zgarb

Posted 2017-10-13T13:38:38.237

Reputation: 39 083

@Laikoni Oh dang, I misread that. Will fix... – Zgarb – 2017-10-13T21:04:03.263

Well, it's no longer shorter. :/ – Zgarb – 2017-10-13T21:13:54.760

2

Japt, 23 bytes

;iB ç
Ng £=hX10nY+XnG
U

Test it online!

First attempt, may be improvable...

ETHproductions

Posted 2017-10-13T13:38:38.237

Reputation: 47 880

It looks like you should be able to save a byte by assigning the initial string to V instead of U: https://ethproductions.github.io/japt/?v=1.4.5&code=CjtpQiDnCqNWPWhYMTBuWStYbkfDVg==&input=IkhFTExPV09STEQi

– Shaggy – 2017-10-13T21:02:19.330

2

J, 37 31 bytes

[`]`(' '#~(1+>./)@])}(i.@#+65-~a.&i.)

[`]`(' '#~(1+>./)@])}#\-66-3&u:

-6 bytes thanks to FrownyFrog

explanation

The entire thing is a hook:

[`]`(' '#~(1+>./)@])}  #\-66-3&u:

The right side calculates the new indexes for all the letters.

The left side uses the gerund form of Amend } first to create a string of the necessary number of spaces: (' '#~(1+>./)@]) . And then to place each letter of the original string into its appropriate index within the all-space string.

Try it online!

Jonah

Posted 2017-10-13T13:38:38.237

Reputation: 8 729

(i.@#+65-~a.&i.) ->
(i.@#+65-~3&u:) ->
(i.@#-65-3&u:) ->
(#\-66-3&u:)
– FrownyFrog – 2017-10-13T17:13:05.907

You can drop the parentheses too. – FrownyFrog – 2017-10-13T17:18:45.563

@FrownyFrog tyvm. i'd forgotten about both those golf tricks. – Jonah – 2017-10-13T17:52:06.307

You can save 3 bytes with (]' '#~1+>./) – miles – 2017-10-14T01:28:19.887

@miles. Nice. I need to make dyadic hooks part of my regular toolbox, I noticed you used them in that revision from this morning as well. – Jonah – 2017-10-14T01:49:00.657

2

Wolfram Language (Mathematica), 76 bytes

SparseArray[Reverse@MapIndexed[#&@@#2+LetterNumber@#-1->#&,#]]<>""/. 0->" "&

Takes a list of characters as input. This generates some error messages that are safe to ignore.

I included Print and Character command in the footer of the TIO link for ease of use. (Character command simply converts a string to a list of characters)

Try it online!

JungHwan Min

Posted 2017-10-13T13:38:38.237

Reputation: 13 290

LetterNumber is a built-in for finding the position of a letter in the alphabet? Holy crap, that's ridiculous. – numbermaniac – 2017-10-14T06:40:53.703

2

Haskell, 88 bytes

foldr(\c->(((['B'..c]>>" ")++[c])#).(' ':))[]
(x:r)#(y:t)|y>' '=y:r#t|1<3=x:r#t
r#t=r++t

Try it online!

Laikoni

Posted 2017-10-13T13:38:38.237

Reputation: 23 676

2

C# (.NET Core), 117 110 84 bytes

Saved 7 bytes thanks to Ayb4tu.

Changed return type from string to char[] to save 26 bytes.

n=>{int i=0,l=n.Length;var t=new char[l+26];for(;i<l;)t[i+n[i]-65]=n[i++];return t;}

Try it online!

Ian H.

Posted 2017-10-13T13:38:38.237

Reputation: 2 431

You can save 7 bytes by changing t[i+((int)n[i]-65)] to t[i+n[i]-65]. – Ayb4btu – 2017-10-14T09:28:07.617

@Ayb4btu Thanks forgot that char -> int conversions are implicit. – Ian H. – 2017-10-14T09:37:29.120

2

Haskell, 88 bytes

f s|q<-zipWith((+).fromEnum)s[0..]=[last$' ':[c|(c,i)<-zip s q,i==p]|p<-[65..maximum q]]

Try it online!

q is the list of the final indices of the letters of the input string (with an offset of 65). Loop through all indices (starting at 65) and find all letters for it, prepending a space. Take the last.

nimi

Posted 2017-10-13T13:38:38.237

Reputation: 34 639

1

Yet another 88 byte Haskell solution, see here and here.

– nimi – 2017-10-14T17:51:59.927

2

Kotlin, 207 bytes 189 bytes 187 bytes 177 bytes

fun main(){val i=(readLine()+" ".repeat(26)).toCharArray();for(x in(i.size-1) downTo 0){if(i[x]!=' '){i[x+i[x].toInt()-65]=i[x];i[x]=' '}};print(i.joinToString("").trim())}

If the leading blank should remain I would just call trimEnd() instead of trim().

Unminified:

fun main() {
    val m = (readLine() + " ".repeat(26)).toCharArray()
    for (x in (m.size - 1) downTo 0) {
        if(m[x] != ' ') {
            m[x + m[x].toInt() - 65] = m[x]
            m[x] = ' '
        }
    }

    print(m.joinToString("").trim())
}

Maybe Kotlin is not the best language for code golfing but I liked the challenge and I wanted to make myself more familiar with Kotlin's standard library.

Willi Mentzel

Posted 2017-10-13T13:38:38.237

Reputation: 121

2

C# .NET, 89 Bytes 87 Bytes

-2 bytes thanks to Lan H.

f=>{var s=new char[f.Length+26];for(int i=0;i<f.Length;i++)s[f[i]+i-65]=f[i];return s;}

Try it Online!

Emiliano

Posted 2017-10-13T13:38:38.237

Reputation: 41

Welcome to the site! :) – James – 2017-10-16T15:44:12.680

Thanks! I hope it's ok to post multiple answer in the same language – Emiliano – 2017-10-16T16:07:16.757

You can omit the curly brackets in your for-loop for -2 bytes. – Ian H. – 2017-10-16T20:50:25.857

1

q/kdb+, 37 bytes

Solution:

@[max[1+m]#" ";m:!:[x#:]+.Q.A?x;:;]x:

Examples:

q)@[max[1+m]#" ";m:!:[x#:]+.Q.A?x;:;]x:"AZ"
"A                         Z"
q)@[max[1+m]#" ";m:!:[x#:]+.Q.A?x;:;]x:"ABC"
"A B C"
q)@[max[1+m]#" ";m:!:[x#:]+.Q.A?x;:;]x:"ACE"
"A  C  E"
q)@[max[1+m]#" ";m:!:[x#:]+.Q.A?x;:;]x:"CBA"
"  A"
q)@[max[1+m]#" ";m:!:[x#:]+.Q.A?x;:;]x:"HELLOWORLD"
"     E H    DLL   OLO   R  W"

Explanation:

I think this is the same idea as the J solution, calculate the correct indices for the input array and then assign them to an empty string of correct length:

@[max[1+m]#" ";m:til[count x]+.Q.A?x;:;]x: / ungolfed solution
                                        x: / save input as x
@[            ;                     ; ;]   / apply[variable;indices;function;parameters]
                                     :     / assignment
                              .Q.A?x       / location of x in uppercase alphabet
                             +             / added to
                     count x               / length of input
                 til[       ]              / range, 0..n
               m:                          / save as m
  max[   ]                                 / maximum of list
      1+m                                  / m + 1
          #" "                             / take " ", creates empty character list

streetster

Posted 2017-10-13T13:38:38.237

Reputation: 3 635

1

Jq 1.5, 91 bytes

reduce(explode|[.,keys]|transpose[]|.[1]+=.[0]-65)as[$c,$p]([];.[$p]=$c)|map(.//32)|implode

Expanded

  reduce(  explode         # convert string to array of ordinals
         | [.,keys]        # [ [v0,v1,...], [0,1,2,...] ]
         | transpose[]     # [ [v0,0], [v1,1], [v2,2]...]
         | .[1]+=.[0]-65   # adjust position of each value
  ) as[$c,$p] (
    []
  ; .[$p]=$c               # store each value at its position
  )
| map(.//32)               # map null values to spaces
| implode                  # convert back to string

Try it online!

jq170727

Posted 2017-10-13T13:38:38.237

Reputation: 411

1

Charcoal, 16 bytes

P FS«M⌕αι→ιM⌕αι←

Try it online! Link is to verbose version of code. Explanation:

P                   Output a space to force the indent
   S                Input string
  F «               Loop over each letter
       α            Uppercase letters predefined variable
      ⌕ ι           Find index of current letter
     M   →          Move that many characters right
          ι         Implicitly print the current letter
           M⌕αι←    Move the same number of characters left

Neil

Posted 2017-10-13T13:38:38.237

Reputation: 95 035

1

APL (Dyalog), 26 bytes

Anonymous prefix lambda which takes the input string as argument and returns the output string. Assumes ⎕IO (Index Origin) 0, which is default on many systems.

{⍵@i⊢''↑⍨1+⌈/i←(⎕A⍳⍵)+⍳≢⍵}

Try it online!

{} anonymous lambda; represents the argument

≢⍵ tally of the argument

 than many ɩntegers (0…LengthOfArgument-1)

()+ plus:

  ⎕A⍳⍵ the indices of the argument in the uppercase Alphabet

i← strore in i (for indices)

⌈/ maximum (reduction)

1+ add one

''↑⍨ take that many characters from the empty string, padding with spaces as needed

 yield that (serves to separate i from '')

⍵@i amend that with the argument letters at the i indices

Adám

Posted 2017-10-13T13:38:38.237

Reputation: 37 779

1

SOGL V0.12, 10 bytes

ā,{ZFWē+1ž

Try it Here!

Explanation:

ā           push an empty array
 ,{         for each char in the input
   ZFW        get its index in the uppercase alphabet
      ē+      add to that the 0-indexed counter
        1ž    at [pop; 1] insert in the array the current character

dzaima

Posted 2017-10-13T13:38:38.237

Reputation: 19 048

1

Pyth, 44 38 bytes

Striked out 44 is still 44 :(

Bloody Pyth beginner.

Saved 6 bytes thanks to @Mr. Xcoder.

K*d+lz26Vlz K=XK-C@zN-65N@zN;.WqeHdPZK

Try it online!


How?

K*d+lz26Vlz K=XK-C@zN-65N@zN;.WqeHdPZK          Full program

K*d+lz26                                        Assign a string consisting of 
                                                  (26 + input.length) whitespaces to K
        Vlz                                     For-loop from 0 to input.length
                -C@zN-65N                       Calculate the index for the current letter
                         @zN                    The current letter
            K=XK                                Insert the current letter into K at
                                                  position i
                            ;                   End statement
                             .WqeHdPZK          While the last character of H is not a 
                                                  whitespace, pop the last character off K

Ian H.

Posted 2017-10-13T13:38:38.237

Reputation: 2 431

38 bytes: K*d+lz26Vlz K=XK-C@zN-65N@zN;.WqeHdPZK. WqeKd K=PK;K is replaced by .W (functional while) and its arguments of course, and FNrZlz can be replaced VrZlz, but rZ... means U..., and U is generated automatically by V. So FNrZlz becomes Vlz – Mr. Xcoder – 2017-10-17T10:55:40.983

1

Batch, 418 331 bytes

Works with uppercase letters only and will take some seconds for longer strings.

Learned new tricks here, the character to ASCII value conversion using %=exitcodeAscii%. Also, if defined and "array" access using call. Also, golfing by almost 100 bytes was good batch code golf training.

Note the trailing space in set z=set.

@echo off
setlocal EnableDelayedExpansion
set z=set 
%z%a=%1
:a
%z%v=64
:b
%z%/Av+=1
cmd/Cexit %v%
if %=exitcodeAscii% neq %a:~0,1% goto b
%z%/Ao=v+c
%z%a%o%=%a:~0,1%
if %o%. geq %m%. %z%m=%o%
%z%/Ac+=1
%z%a=%a:~1%
if %a%. neq . goto a
for /l %%n in (65,1,%m%)do (
if defined a%%n (call %z%r=%%r%%%%a%%n%%
)else %z%r=!r! )
echo %r%

schnaader

Posted 2017-10-13T13:38:38.237

Reputation: 1 132

1

Ruby, 68 bytes

->s{r,w="",-66;s.bytes{|b|r[(b+w+=1).times{|a|r[a]||=" "}]=b.chr};r}

Try it online!

G B

Posted 2017-10-13T13:38:38.237

Reputation: 11 099

1

IBM PC DOS 8088 Assembly, 34 33 bytes

b403 cd10 be80 00ad 8bc8 32ed ac8b de02 d880 ebc4 8ad3 b402 cd10 b40e cd10 e2ec c3

Ungolfed (unassembled):

    MOV  AH, 03H        ; get current cursor position row into DH
    INT  10H
    MOV  SI, 80H        ; point SI to PSP
    LODSW               ; fetch length into AL, increment SI to 82H
    MOV  CX, AX         ; move to CX
    XOR  CH, CH         ; clear CH
OUTPUT:
    LODSB               ; load DS:SI into AL
    MOV  BX, SI         ; input string offset into BX (so doesn't overwrite DH)
    ADD  BL, AL         ; add ASCII value of char
    SUB  BL, 'A'+83H    ; convert to numeric val (A=0, Z=25)
    MOV  DL, BL         ; DL is column number
    MOV  AH, 02H        ; BIOS set cursor position function    
    INT  10H            ; move to column
    MOV  AH, 0EH        ; BIOS display char function in AL
    INT  10H            ; write to screen
    LOOP OUTPUT
    RET                 ; return to DOS

This is a complete PC DOS executable that takes the input string from the command line and prints the new "faster" version to the screen. Requires a minimum of DOS 1.0... hopefully you've got at least that.

Output

A>SPEED.COM ABC
A B C
A>SPEED.COM AZ
A                         Z
A>SPEED.COM CBA
  A
A>SPEED.COM HELLOWORLD
     E H    DLL   OLO   R  W

640KB

Posted 2017-10-13T13:38:38.237

Reputation: 7 149

0

PHP, 127 123 bytes

function b($i){for($q=0;$q<strlen($i);$q++){$n[ord($i[$q])-65]=$i[$q];}while($x++<26){$m.=$n[$x-1]?$n[$x-1]:" ";}return$m;}

Try it online

Had to fix a bug that wouldn't output 'A'...

steenbergh

Posted 2017-10-13T13:38:38.237

Reputation: 7 772

To who-ever downvoted: You probably mis-clicked the up-button. Please correct, or drop me a line on why the downvote... Tnx – steenbergh – 2017-10-13T21:47:50.883

0

Javascript, 136 characters

Replace the variable s by the input string

for(s='',w=[],o=[],i=0;i<s.length;i++)w.push(s[i].charCodeAt(0)-65+i);w.forEach((a,b)=>o[a]=s[b]),console.log((o+'').replace(/,/g,' '));

Alexis_A

Posted 2017-10-13T13:38:38.237

Reputation: 151

0

Java 7, 135 133 bytes

String a(char[]a){char b[]=new char[37+a.length],c=0;for(;c<a.length;b[a[c]-65+c]=a[c++]);return"".valueOf(b).replaceAll("\0*$","");}

Less Golfed

String a(char[]a) {
  char b[]=new char[37+a.length],            // Create an array buffer that is the length of 
                                             // the incoming string plus the length of the 
                                             // alphabet so we have room to move characters
                                             // 37 is arbitrary. We need >26
  c=0;                                       // Also initialize our index variable. chars are
                                             // basically just bytes in java
  for(;c<a.length;                           // Loop over the input
    b[a[c]-65+c]=a[c++]);                    // 'A' is ascii 65 so if we subtract that and add
                                             // the current index, we have the resulting index
                                             // for that character.
  return"".valueOf(b).replaceAll("\0*$",""); // Convert char array to a string and remove any
                                             // trailing nulls that we didn't use
}

Try it online!

Poke

Posted 2017-10-13T13:38:38.237

Reputation: 3 075

0

Actually, 32 bytes

;ñ⌠i@O:65@-i+⌡M;Mu' *╗Z⌠i╜T╗⌡MX╜

Try it online!

Explanation

As is customary with my long Actually answers, I'm going to break down the code into sections for the explanation.

Space Padding

Input: s (a string of uppercase ASCII letters)

Output: a list L of new indexes for the letters in s on top of the stack, s below it on the stack, and a string of spaces of sufficient length to hold the output string in register 0

;ñ⌠i@O:65@-i+⌡M;Mu' *╗
                        (implicit input)
;ñ                      make a copy of s, enumerate
  ⌠i@O:65@-i+⌡M         for each [index, character] pair: calculate the new position
   i@O                    ordinal of character (in a list)
      :65@-               subtract 65
           i+             add index
               ;M       maximum index
                 u' *   add one, string of that length containing spaces
                     ╗  save to register 0

Letter Rearranging

Input: the output from the previous step

Output: the expected challenge output

Z⌠i╜T╗⌡MX╜
Z           zip the new indices with their respective letters
 ⌠i╜T╗⌡M    for each [index, character] pair: put the character in that index
  i           flatten pair
   ╜T╗        bring string from register 0, set string[index] to character, save in register 0
        X   discard the empty list from the map
         ╜  push the string in register 0
            (implicit output)

Mego

Posted 2017-10-13T13:38:38.237

Reputation: 32 998

0

AWK, 140 bytes

BEGIN{OFS=FS=""}func f(A){for(z=0;sprintf("%c",++z)!=A;);return z-65}{for(;++i<=NF;m=n>m?n:m)a[n=i+f($i)]=$i
for(;++k<=m;)$k=a[k]?a[k]:" "}1

Try it online!

Designed for uppercase letters, since I'm subtracting 65. Converting characters to numbers isn't so easy in AWK. This is a few bytes shorter than indexing into a string.

Robert Benson

Posted 2017-10-13T13:38:38.237

Reputation: 1 339

0

Lua, 126 bytes

s=""a={}i=-65(...):gsub(".",function(c)a[c:byte()+i]=c i=i+1 end)for j=0,i+91 do s=s..(a[j]or" ")end print((s:gsub(" +$","")))

Try it online!

Jonathan S.

Posted 2017-10-13T13:38:38.237

Reputation: 423

0

ARBLE, 122 bytes

s=explode&read()t={}n=0s.map(load'a,b=...k=b+byte(a)-byte"A"t[k]=a n=max&n|k')for i=1,n do t[i]=t[i]or" "end print(join&t)

Try it online!

ATaco

Posted 2017-10-13T13:38:38.237

Reputation: 7 898

0

PHP 7.1, 54+1 bytes

Insert a space between the quotation marks for older PHP.

for($r="";$c=$argn[$i++];)$r[$i+ord($c)-66]=$c;echo$r;

Run as pipe with -nR or try it online.

Titus

Posted 2017-10-13T13:38:38.237

Reputation: 13 814

0

Japt -h, 14 bytes

;åÈhYZ+BbY}Ucç

Try it

Shaggy

Posted 2017-10-13T13:38:38.237

Reputation: 24 623