Charlie and the evil hyphen

8

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.

rybo111

Posted 2017-04-11T20:51:51.467

Reputation: 4 071

@LevelRiverSt The sorting is very different without the first character though – fəˈnɛtɪk – 2017-04-11T21:43:51.723

@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 – 2017-04-11T22:14:57.617

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 – 2017-04-11T22:23:59.437

1I'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 – 2017-04-11T22:55:26.700

@xnor You are supposed to return an array of the exact phonetic text by performing operations on the input I think. – fəˈnɛtɪk – 2017-04-11T22:56:29.830

@fəˈnɛtɪk But what does "performing operations on the input" mean? Could one just disregard the input and output the fixed text? I don't expect it to be competitive, but am not clear if it's allowed. – xnor – 2017-04-11T22:58:16.093

3@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 – 2017-04-11T22:59:24.643

Two questions: 1. Does start with the words in a random order mean we'll receive them as input? That's what I inferred from the comments. 2. By text variable, do you mean string? – Dennis – 2017-04-12T16:05:16.947

Answers

4

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.

Dennis

Posted 2017-04-11T20:51:51.467

Reputation: 196 637

1It amazes me how well you can golf... – Christopher – 2017-04-13T11:40:24.657

3

Python 3, 76 71 bytes

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

Try it online!

Dennis

Posted 2017-04-11T20:51:51.467

Reputation: 196 637

2

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

fəˈnɛtɪk

Posted 2017-04-11T20:51:51.467

Reputation: 4 166

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 – 2017-04-13T11:35:29.340

@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 – 2017-04-13T11:50:47.493

Ah, I misread a part from the 2nd bullet. I'll edit my code. – Kevin Cruijssen – 2017-04-13T11:58:26.743

2

PHP, 94 bytes

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

Try it online!

Jörg Hülsermann

Posted 2017-04-11T20:51:51.467

Reputation: 13 026

1

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 .•.

Magic Octopus Urn

Posted 2017-04-11T20:51:51.467

Reputation: 19 422

You could also construct the string with .•C5ê™вrΓ˜3åóMîõxÿ•u. – Emigna – 2017-06-06T20:04:30.457

Is that just a keyspace indexed using A-Za-z0-9 instead of 0-9A-Za-z? – Magic Octopus Urn – 2017-06-06T20:37:08.363

It's a base-27 map into <space>a-z – Emigna – 2017-06-07T06:20:14.523

0

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

Kevin Cruijssen

Posted 2017-04-11T20:51:51.467

Reputation: 67 575

It needs to return in the original order (Alfa Bravo Charlie...) – fəˈnɛtɪk – 2017-04-13T11:54:01.157

@fəˈnɛtɪk Fixed at the cost of 18 bytes, making it 169. – Kevin Cruijssen – 2017-04-13T12:07:54.863