# Charlie and the evil hyphen

Here's the phonetic alphabet:

Alfa
Bravo
Charlie
Delta
Echo
Foxtrot
Golf
Hotel
India
Juliett
Kilo
Lima
Mike
November
Oscar
Papa
Quebec
Romeo
Sierra
Tango
Uniform
Victor
Whiskey
X-ray
Yankee
Zulu


In the fewest bytes possible:

1. Input will be these words in a random order, with the first letters removed.
2. Your goal is to arrange the words back to their original order using only the letters you now have. In other words: work out how '[A]lfa' comes before '[B]ravo', and so on. Your code must not refer the original list, index keys, etc.
3. Restore the original first letters of each word.

# Rules

• The hyphen in X-ray is probably evil, but needs to stay, for I am also evil.
• The result can be a string (comma or newline separated, for example), or an array.
• The spellings and letter cases are non-negotiable.
• No need to show example output in answers.
• @LevelRiverSt The sorting is very different without the first character though – fəˈnɛtɪk Apr 11 '17 at 21:43
• @fəˈnɛtɪk tagging each word fragment with its first letter is arguably a method of sorting. I think sparklepony's proposal is within the OP's intent. Really the OP should answer this, but I would be inclined to closevote again due to nonobservable requirements if the OP said no. It gets messy (what if you use a loop to fish them out one by one, add a letter and print - then you're doing both together. )I'm with Martin's meta OP in cases like this. (and the post he referenced with 19 upvotes and 0 downvotes) – Level River St Apr 11 '17 at 22:14
• If anyone feels this might be useful, a string which has uniquely identified each of the input words from the start of the string is "l,r,ha,e,c,ox,ol,ot,nd,uli,il,im,ik,ov,s,ap,ue,o,ie,ang,n,i,h,-,a,u" – fəˈnɛtɪk Apr 11 '17 at 22:23
• I'm not sure I understand what the output is intended to be. Is it simply the exact phonetic text (or line array) kolmogorov complexity style, except you have a mangled version as input? – xnor Apr 11 '17 at 22:55
• @xnor It might work better then if you are given a random selection of the phonetic alphabet shuffled then you need to return it in order, in which case the fixed text would not be a valid output. – fəˈnɛtɪk Apr 11 '17 at 22:59

# Jelly, 21 bytes

“ṣȤW¶¬þḷẒ⁶4ẹ’Œ?ịṢØA;"


Input and output are string arrays.

Try it online!

### Background

While there are 26! = 403291461126605635584000000 different inputs, we can sort the input so we have to deal only with one permutation of this phonetic alphabet. Sorting lexicographically, we get

-ray
ango
ankee
apa
cho
elta
harlie
hiskey
ictor
ierra
ike
ilo
ima
lfa
ndia
niform
olf
omeo
otel
ovember
oxtrot
ravo
scar
uebec
uliett
ulu


We can simply index into this array to sort it by the removed letters. The (1-based) indices are

14 22  7  6  5 21 17 19 15 25 12 13 11 20 23  4 24 18 10  2 16  9  8  1  3 26


All that's left to do is to prepend the uppercase letters of the alphabet to the strings in the result.

### How it works

“ṣȤW¶¬þḷẒ⁶4ẹ’Œ?ịṢØA;"  Main link. Argument: A (string array)

“ṣȤW¶¬þḷẒ⁶4ẹ’          Replace the characters by their indices in Jelly's code page
and convert the result from bijective base 250 to integer.
This yields k := 214215675539094608445950965.
Œ?        Construct the k-th permutation (sorted lexicographically)
of [1, ..., n], with minimal n (26 here). This yields
[14, 22,  7,  6,  5, 21, 17, 19, 15, 25, 12, 13, 11,
20, 23,  4, 24, 18, 10,  2, 16,  9,  8,  1,  3, 26].
Ṣ      Yield A, sorted.
ị       For each j in the array to the left, select the j-th string
from the array to the right.
ØA    Yield "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
;"  Concatenate zipwith; prepend the j-th character from the
alphabet to the j-th string of the permuted input.

• It amazes me how well you can golf... – Christopher Apr 13 '17 at 11:40

# Python 3, 76 71 bytes

lambda x,S=sorted:S(map(str.__add__,'XTYPEDCWVSMKLAIUGRHNFBOQJZ',S(x)))


Try it online!

# JavaScript, 169/195 bytes

If replacing the first letter can be done prior to sorting 169 bytes

x=>x.map(k=>{for(i=0;i<26;)if(!k.indexOf("l,r,ha,e,c,ox,ol,ot,nd,uli,il,im,ik,ov,s,ap,ue,o,ie,ang,n,i,h,-,a,u".split,[i++]))return String.fromCharCode(i+64)+k}).sort()


Try it online!

If sorting must be done prior to adding the first letter 195 bytes

x=>x.sort((a,b)=>g(a)-g(b)).map(a=>String.fromCharCode(g(a)+64)+a)
g=k=>{for(i=0;i<26;)if(!k.indexOf("l,r,ha,e,c,ox,ol,ot,nd,uli,il,im,ik,ov,s,ap,ue,o,ie,ang,n,i,h,-,a,u".split,[i++]))return i}


Try it online!

A full 20 bytes of each of these programs is just String.fromCharCode() to regain the character...

Another 69 bytes is the string which uniquely identifies the start of each of the words in the phonetic alphabet.

## Explanation of first example

for(i=0;i<26;)if(!k.indexOf("l,r,ha,e,c,ox,ol,ot,nd,uli,il,im,ik,ov,s,ap,ue,o,ie,ang,n,i,h,-,a,u".split,[i++]))return i

The string hardcoded here is separated into a list of the unique identifying characters that start the phonetic alphabet words after the first character is removed.

return String.fromCharCode(i+64)+k


This information is used to find the index where the string falls in the alphabet in order to put the starting character back on.

.sort()


JavaScript implicitly sorts as strings by the ASCII values of the characters

## Explanation of the second example

The function g=k=>... finds the index for which the string should fall in the phonetic alphabet.

.sort((a,b)=>g(a)-g(b))


Sorts the strings by the numbers returned by performing g(...)

.map(a=>String.fromCharCode(g(a)+64)+a)


Replaces the first character of each of the phonetic alphabet strings

• Why the .sort() in your 169 byte answer? In the question it states "3. Restore the original first letters of each word.", but it doesn't mention the output has to be sorted as well. – Kevin Cruijssen Apr 13 '17 at 11:35
• @KevinCruijssen Your goal is to arrange the words back to their original order using only the letters you now have. In other words: work out how '[A]lfa' comes before '[B]ravo', and so on. Your code must not refer the original list, index keys, etc. – fəˈnɛtɪk Apr 13 '17 at 11:50
• Ah, I misread a part from the 2nd bullet. I'll edit my code. – Kevin Cruijssen Apr 13 '17 at 11:58

# PHP, 94 bytes

<?for(sort($_GET);$i<26;sort($r))$r[]=XTYPEDCWVSMKLAIUGRHNFBOQJZ[$i].$_GET[+$i++];print_r($r);


Try it online!

# 05AB1E, 29 27 bytes

.•C5ê™вrΓ˜3åóMîõxÿ•uS|{)øJ{


Try it online!

.•C5ê™вrΓ˜3åóMîõxÿ•uS       # Push exploded XTYPEDCWVSMKLAIUGRHNFBOQJZ.
|{)    # Push input in array, sorted.
øJ{ # Zip together, join, resort.


-2 thanks to Emigna's magical .•.

• You could also construct the string with .•C5ê™вrΓ˜3åóMîõxÿ•u. – Emigna Jun 6 '17 at 20:04
• Is that just a keyspace indexed using A-Za-z0-9 instead of 0-9A-Za-z? – Magic Octopus Urn Jun 6 '17 at 20:37
• It's a base-27 map into <space>a-z – Emigna Jun 7 '17 at 6:20

# Java 7, 169 bytes

import java.util.*;List c(List l){Collections.sort(l);int i=0;for(Object s:l)l.set(i,""+"XTYPEDCWVSMKLAIUGRHNFBOQJZ".charAt(i)+l.get(i++));Collections.sort(l);return l;}


Try it here.

Explanation:

import java.util.*;                            // Import used for List and Collections

void c(List l){                                // Method with list parameter and no return-type
Collections.sort(l);                         //  Sort the given list
int i=0; for(Object s:l)                     //  Loop over the Strings in the list
l.set(i,""+                                //   Replace the String-item in the list with:
+"XTYPEDCWVSMKLAIUGRHNFBOQJZ".charAt(i)  //    The leading capital character
+l.get(i++);                             //    Followed by itself
//  End of loop (implicit / single-line body)
}                                              // End of method

• It needs to return in the original order (Alfa Bravo Charlie...) – fəˈnɛtɪk Apr 13 '17 at 11:54
• @fəˈnɛtɪk Fixed at the cost of 18 bytes, making it 169. – Kevin Cruijssen Apr 13 '17 at 12:07