16
\$\begingroup\$

Given a list of countries in alphabetical order, put them in the same order as they are given in the song "Yakko's World".

Specifically, you are given the following list of countries* exactly:

Abu Dhabi, Afghanistan, Albania, Algeria, Algiers, Angola, Argentina, Australia, Austria, Bahamas, Bahrain, Bangladesh, Barbados, Belgium, Belize, Benin, Bermuda, Bhutan, Bolivia, Borneo, Botswana, Brazil, Bulgaria, Burma, Burundi, Cameroon, Canada, Caribbean, Cayman, Chad, Chile, China, Colombia, Congo, Costa Rica, Crete, Cuba, Cyprus, Czechoslovakia, Dahomey, Denmark, Djibouti, Ecuador, Egypt, El Salvador, England, Ethiopia, Fiji, Finland, France, French Guiana, Gabon, Gambia, Germany, Ghana, Greece, Greenland, Guam, Guatemala, Guinea, Guinea-Bissau, Guyana, Haiti, Honduras, Hong Kong, Hungary, Iceland, India, Indonesia, Iran, Iraq, Ireland, Israel, Italy, Jamaica, Japan, Jordan, Kampuchea, Kenya, Korea, Kuwait, Laos, Lebanon, Lesotho, Liberia, Libya, Liechtenstein, Luxembourg, Madagascar, Mahore, Malawi, Malaysia, Mali, Malta, Mauritania, Mexico, Monaco, Mongolia, Morocco, Mozambique, Namibia, Nepal, New Guinea, New Zealand, Nicaragua, Niger, Nigeria, Norway, Oman, Pakistan, Palestine, Panama, Paraguay, Peru, Poland, Portugal, Puerto Rico, Qatar, Republic Dominican, Romania, Russia, Rwanda, San Juan, Saudi Arabia, Scotland, Senegal, Sierra Leone, Somalia, Spain, Sri Lanka, Sudan, Sumatra, Surinam, Swaziland, Sweden, Switzerland, Syria, Taiwan, Tanzania, Thailand, The Netherlands, The Philippine Islands, Tibet, Tobago, Togo, Transylvania, Tunisia, Turkey, Uganda, United States, Uruguay, Venezuela, Vietnam, Yemen, Yugoslavia, Zaire, Zambia, Zimbabwe

And required to output the countries in this order:

United States, Canada, Mexico, Panama, Haiti, Jamaica, Peru, Republic Dominican, Cuba, Caribbean, Greenland, El Salvador, Puerto Rico, Colombia, Venezuela, Honduras, Guyana, Guatemala, Bolivia, Argentina, Ecuador, Chile, Brazil, Costa Rica, Belize, Nicaragua, Bermuda, Bahamas, Tobago, San Juan, Paraguay, Uruguay, Surinam, French Guiana, Barbados, Guam, Norway, Sweden, Iceland, Finland, Germany, Switzerland, Austria, Czechoslovakia, Italy, Turkey, Greece, Poland, Romania, Scotland, Albania, Ireland, Russia, Oman, Bulgaria, Saudi Arabia, Hungary, Cyprus, Iraq, Iran, Syria, Lebanon, Israel, Jordan, Yemen, Kuwait, Bahrain, The Netherlands, Luxembourg, Belgium, Portugal, France, England, Denmark, Spain, India, Pakistan, Burma, Afghanistan, Thailand, Nepal, Bhutan, Kampuchea, Malaysia, Bangladesh, China, Korea, Japan, Mongolia, Laos, Tibet, Indonesia, The Philippine Islands, Taiwan, Sri Lanka, New Guinea, Sumatra, New Zealand, Borneo, Vietnam, Tunisia, Morocco, Uganda, Angola, Zimbabwe, Djibouti, Botswana, Mozambique, Zambia, Swaziland, Gambia, Guinea, Algeria, Ghana, Burundi, Lesotho, Malawi, Togo, Niger, Nigeria, Chad, Liberia, Egypt, Benin, Gabon, Tanzania, Somalia, Kenya, Mali, Sierra Leone, Algiers, Dahomey, Namibia, Senegal, Libya, Cameroon, Congo, Zaire, Ethiopia, Guinea-Bissau, Madagascar, Rwanda, Mahore, Cayman, Hong Kong, Abu Dhabi, Qatar, Yugoslavia, Crete, Mauritania, Transylvania, Monaco, Liechtenstein, Malta, Palestine, Fiji, Australia, Sudan

You have some flexibility in the form of the input and output; you can take or output the countries as all uppercase, all lowercase, or maintaining case, and can choose to omit spaces as well.

Standard loopholes are forbidden. Since this is , the shortest code wins.


*Note that Yakko's World includes several places that aren't countries, and that several countries aren't included in the list.

\$\endgroup\$
2

9 Answers 9

8
\$\begingroup\$

Jelly, 121 bytes

“wỴḅKṭḞ2Ṅðȧ¤[Ạ⁺©iṆȦẉ&ŒÐḷ<⁸Ṛ⁵Ƙ#A½Ƈ'¥ṚPċṢSḥSQUmṘẉL>²XɼNĠ¥ṆỊẋi¥@ȯ⁺CḲKSḌȮC²ß5LṂJṁȮr]zƬe§^YVœX<ḃç%ƝṪƬṣ$¹Ṗa?⁵ḤẠÐẈ}ƬỊhR=zḢɱ¿z’œ?

Try it online!

Boring, but it's not clear what further logic could bring it down. The program is just a giant compressed integer representing a permutation, and a builtin to apply it.

\$\endgroup\$
8
\$\begingroup\$

JavaScript (Node.js), 205 bytes

-2 thanks to @l4m2

This is based on a specific implementation of sort() but probably works with several engines.

The source code contains unprintable characters.

a=>a.sort(_=>Buffer(`!IF<@E*JGL8*'M-&LDJ>!F%8+,.C?$9C-=M#10-G!G7B*D9,,2!:F"@BF5H;#3-$IK.EK;(%H+F'L'I2,-J2'7M551;,;8*! /%92 D&@J?0E:<2G3EG.K-I'`)[i/6|0]-14<<31-i++%6,i=0)

Try it online!

How?

We make the callback function of sort() returning either a negative or non-negative value, depending on whether the items must be swapped or not. We need one bit per iteration and the data string encodes \$6\$ bits per character, using the ASCII range \$[14\dots77]\$.

The list is fully sorted after \$943\$ iterations, which requires \$\lceil 943/6\rceil=158\$ characters. It happens to be equal to the number of countries in the list, but that's a pure coincidence.

Commented

a =>            // a[] = input list, in alphabetical order
a.sort(_ =>     // sort it, ignoring the arguments of the callback
  Buffer(`...`) //   turn the data string into a list of ASCII codes
  [i / 6 | 0]   //   extract the entry at index floor(i / 6)
  - 14          //   subtract a fixed encoding offset, chosen to avoid
                //   characters that can't be included as literals or
                //   must be escaped
  << 31 -       //   left-shift the result such that the sign bit
  i++ % 6,      //   is replaced with the bit at position (i mod 6)
  i = 0         //   start with i = 0
)               // end of sort()
\$\endgroup\$
2
  • \$\begingroup\$ -(x&1) => x<<31 \$\endgroup\$
    – l4m2
    Feb 3, 2023 at 1:37
  • \$\begingroup\$ and you needn't -14 but choose a better one of x and x+64 \$\endgroup\$
    – l4m2
    Feb 3, 2023 at 1:39
4
\$\begingroup\$

05AB1E, 121 bytes

•Caиβ”œ`äÔÚ×3ιØ>ãA8Cγ“Ø₄XмK
£#_¯½%˜‚-Z†b}g3EqÄúé…¹ζzŒWmRç°¶вåΔBв„6J0й%∍¬üë¯β±B[λ9fΩÙ„¶g¹`Hk¹ʒJ.≠|”ΩoK"Âc9„Ƶι–+@ÌèÊ$§₅•.I

I/O as a list. It's currently in the default titlecase, but can also be lowercase or uppercase without a change in functionality.

Try it online.

Explanation:

•Caи...$§₅• # Push compressed integer 17496935826677579314159550205809193960505801935712671712608685729996785341091982271028672893034204123066468970588179871093613064855657604902698437005706906805554153425206759252074826136162698098035155104304651724366320520871306820060929813088043319762751700033987752258296072690622
 .I         # Get the (0-based) ^'th permutation of the (implicit) input-list
            # (after which the list is output implicitly as result)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why •Caиβ”œ`äÔÚ×3ιØ>ãA8Cγ“Ø₄XмK\n£#_¯½%˜‚-Z†b}g3EqÄúé…¹ζzŒWmRç°¶вåΔBв„6J0й%∍¬üë¯β±B[λ9fΩÙ„¶g¹`Hk¹ʒJ.≠|”ΩoK"Âc9„Ƶι–+@ÌèÊ$§₅• is 17496935826677579314159550205809193960505801935712671712608685729996785341091982271028672893034204123066468970588179871093613064855657604902698437005706906805554153425206759252074826136162698098035155104304651724366320520871306820060929813088043319762751700033987752258296072690622.

\$\endgroup\$
4
\$\begingroup\$

MS-DOS .COM format (8086), 220 213 bytes

Takes a CR-LF delimited list of countries from standard input.

0000000 b4 08 b1 9e bf 11 03 57 cd 21 3c 0d 75 02 b0 24
0000020 aa 75 f5 cd 21 e2 f0 bb 36 01 b1 9e 8b ec 33 c0
0000040 8a c1 d7 d1 e0 8b f0 8d 3a 8b 15 b4 09 cd 21 b0
0000060 20 cd 29 e2 e9 cd 20 1b 96 6e 2f 40 47 3d 0c 3f
0000100 7a 03 28 9d 5d 81 44 24 45 61 6f 02 7c 84 48 20
0000120 39 76 99 1f 41 4f 1e 13 6a 8e 72 49 80 33 34 0d
0000140 43 4a 85 67 9a 62 69 18 01 3a 89 74 00 98 09 3b
0000160 0b 05 8a 36 1a 37 1c 14 10 59 0f 4c 3c 52 4e 7e
0000200 92 42 50 8c 38 12 9c 86 30 5a 1d 75 70 6c 2a 90
0000220 46 11 93 4d 04 51 55 4b 15 58 57 78 5c 22 87 31
0000240 25 56 9b 21 26 2b 66 0a 54 77 95 16 68 6d 5b 17
0000260 32 64 91 6b 19 07 2d 23 0e 94 8d 35 8f 7b 88 7f
0000300 73 97 8b 63 60 5e 06 7d 29 71 65 82 79 27 2c 53
0000320 5f 2e 3e 83 08

Input file

Assembler source (TASM):

IDEAL
P8086

MODEL   TINY
CODESEG
ORG 100H

MAIN:
    MOV AH,8    ; Read from STDIN, no echo
    MOV CL,158  ; Do for all strings
    MOV DI,OFFSET STRINGS ; String storage
STORE:
    PUSH    DI  ; Store index of string
LOAD:
    INT 21H     ; Get character
    CMP AL,0DH  ; End of line?
    JNZ LOAD1   ; No, keep going
    MOV AL,'$'  ; Replace end of string with $ terminator
LOAD1:
    STOSB
    JNZ LOAD    ; End of line? (No, keep going)
    INT 21H     ; Yes, skip LF
    LOOP    STORE ; Store next offset until done
DONE:
    MOV BX,OFFSET CONVS-1 ; Conversion table
    MOV CL,158  ; Do for all strings
    MOV BP,SP
PRINT:
    XOR AX,AX   ; Line number
    MOV AL,CL
    XLAT        ; Get conversion
    SHL AX,1    ; Turn into pointer
    MOV SI,AX
    LEA DI,[BP+SI] ; Get address of array index
    MOV DX,[DI] ; Get string address
    MOV AH,9    ; Print string
    INT 21H
    MOV AL,' '  ; Print separator
    INT 29H
    LOOP    PRINT ; Until done
    INT 20H

; Conversion table
CONVS   DB  27,150,110,47,64,71,61,12,63,122,3,40,157,93
    DB  129,68,36,69,97,111,2,124,132,72,32,57,118,153
    DB  31,65,79,30,19,106,142,114,73,128,51,52,13,67,74
    DB  133,103,154,98,105,24,1,58,137,116,0,152,9,59,11
    DB  5,138,54,26,55,28,20,16,89,15,76,60,82,78,126,146
    DB  66,80,140,56,18,156,134,48,90,29,117,112,108,42
    DB  144,70,17,147,77,4,81,85,75,21,88,87,120,92,34,135
    DB  49,37,86,155,33,38,43,102,10,84,119,149,22,104,109
    DB  91,23,50,100,145,107,25,7,45,35,14,148,141,53,143
    DB  123,136,127,115,151,139,99,96,94,6,125,41,113,101
    DB  130,121,39,44,83,95,46,62,131,8
STRINGS DB  ?

END MAIN
ENDS
\$\endgroup\$
3
\$\begingroup\$

JavaScript (Node.js), 253 bytes

a=>a.map((_,i)=>a[Buffer("lRpfbz5KcXYkGzgsdCCXPz06EgYqHhUiDmgQCY96cJaEMgw5a4ZCMDWHCCZJkzdyd3wCR3hsFntBJUZFiFJITJlQCoxXDXMxLSiAQ20XAYtlEU1bCx9PS2FRjkSNiYFmg2cTmJJilAWdKRRjnIU0OwM2GFNakGlqHVQrDzOKf05cfgQnZH1VGSGbLjxYeVkcQAB1miNekWBWXW4vB4I",'base64')[i]])

Try it online!

JavaScript (Node.js), 355 bytes not using Buffer

a=>"951a5f6f3e4a7176241b382c7420973f3d3a12062a1e15220e6810098f7a709684320c396b8642303587082649933772777c0247786c167b412546458852484c99500a8c570d73312d2880436d17018b65114d5b0b1f4f4b61518e448d89816683671398926294059d2914639c85343b033618535a90696a1d542b0f338a7f4e5c7e0427647d5519219b2e3c5879591c4000759a235e9160565d6e2f0782".replace(/../g,i=>a['0x'+i-0]+`
`)

Try it online!

Nothing but table

\$\endgroup\$
3
\$\begingroup\$

Vyxal, 140 bytes

»÷¤xŻλjb↑+k2x₴O₍‟ḂṀṫ. ɖ`‟z¦¼ẇeRʁ>j:t{*0 Nq;*₅1z⟑-¶±↓`ṡF²k•-ṡ⁼₌]L₇ḊṪṗ₃h¦SεĠT$₴∴∇ǎ)¾ŀ⟇ḟṡ₄↲ḣ×£↳ǎrq°ʁ₀∇ẋ≠↑⁽5₍↑¯₈¤⌈←±e«∷¦jẇʁ»⁺9(n›ḋ÷$)_WṘ(n~i⅛⟇)¾

Try it Online!

This uses factorial number system to encode the permutation. A shorter solution would be to index into the list of all permutations of the input but that wouldn't complete before the heat death of the universe.

This code starts with a big integer encoding the permutation on the stack:

⁺9(                    # for n in range(158):
   n›                  #   increment
     ḋ                 #   divmod
      ÷$               #   push each to stack and swap
        )              # end for
         _WṘ           # pop, wrap stack and reverse
            (          # for each:
             n~i⅛      #   index into input and push result to global array
                 ⟇     #   remove this index from input
                  )    # end for
                   ¾   # push global array
\$\endgroup\$
3
\$\begingroup\$

Zsh, 264 bytes

for c (`basenc -d --z85<<<'Mk:jJktc3vb]!]%BS)bqk06z.d<2JK4}#Q.KB7#lG!
<f0y>>wjhI.U!n%zqRCZPV+D2jvBlmbh(IbM#uNO:DLspPBde?8utzwSClw!E<z3)nwuv
P5mQJ/5B3GLh6/LvG<ZO*>E=ORx0:1udh$tAJVo9-T:5g(D(Wu4i2awQWU#baHqHs:Bl(
k((d#bWVZys6//02.J9S'|basenc --base2msbf -w8`)<<<$@[2#$c]

Try it Online! Uses z85 encoding. Reformatted the code above for readability. It doesn't work on TIO (no basenc), but here's a full version that should run on MacOS or Linux.

355 bytes, hex encoded

s=951a5f6f3e4a7176241b382c7420973f3d3a12062a1e15220e6810098f7a709684320c396b8642303587082649933772777c0247786c167b412546458852484c99500a8c570d73312d2880436d17018b65114d5b0b1f4f4b61518e448d89816683671398926294059d2914639c85343b033618535a90696a1d542b0f338a7f4e5c7e0427647d5519219b2e3c5879591c4000759a235e9160565d6e2f0782
for i j (${(s::)s})echo $@[0x$i$j+1]

Try it online! Uses the hex string in @lm42's answer. Could save 2 bytes by editing $s (so I don't need to have +1 in there), but that's a bit painful. I might add a shorter solution later, using z85 encoding.

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 212 150 bytes

≔⪪S, θ≔I”)¶‴k~r~↷⊙α¤ψςF8→⊖"]´τDβ²×ju÷↷Hγ0÷≕⊙≧↖Gⅈσ¦⊕ANQWo0Ex⟧/DT✳o?L:Jμ∧LY<γ⊗⬤νGΠ↗φWε﹪πχ5₂{“⊗K»§″^⁶↘;R⁷↧C�nεKΣ⌊⪫lY×≔>¦³mZ⎇Π⊟⁵↶⦃~D”ηW⁻θυ«⊞υ§ιη≧÷Lιη»⪫υ, 

Try it online! Link is to verbose version of code. Explanation: Now uses the reverse factorial number system.

≔⪪S, θ

Split the input on commas.

≔I”...”η

Start with a large compressed integer.

W⁻θυ«

Repeat until all of the countries have been extracted.

⊞υ§ιη

Cyclically index the remaining countries with the large integer and push the result to the predefined empty list.

≧÷Lιη

Integer divide the integer by the count of remaining countries.

»⪫υ, 

Output all of the countries joined by commas.

\$\endgroup\$
2
\$\begingroup\$

Python 2, 226 bytes

lambda x:list(permutations(x))[eval('...'.decode('zip'))]
from itertools import*

Attempt This Online! (Times out on ATO)

Here's a hexdump of the string:

00000000  78 9c 0d 8d b9 11 00 31 08 03 5b 02 63 be 72 78 | x......1..[.c.rx |
00000010  6b b8 f2 cf 89 46 1b 68 05 9f a6 20 4f 1d b4 76 | k....F.h... O..v |
00000020  b1 5b d3 26 b5 aa dc 8e 51 19 16 08 af 8e 15 64 | .[.&....Q......d |
00000030  38 2b d7 6d e2 68 8d ea be 91 2e b8 53 1e ab 92 | 8+.m.h......S... |
00000040  7b ac b7 0f f0 59 3e 5a 3a 49 79 96 70 96 77 1f | {....Y>Z:Iy.p.w. |
00000050  32 76 12 8d 00 73 1a e7 32 39 cf 4e c2 d5 5b 3e | 2v...s..29.N..[> |
00000060  3e 45 8d 93 ad d5 ac eb 56 06 c5 06 33 1a f7 05 | >E......V...3... |
00000070  5e a3 88 42 54 21 32 32 25 c4 7a e7 0d 22 6b 11 | ^..BT!22%.z.."k. |
00000080  76 47 2d 88 9d 8e bd 5d ec 89 5a 88 a7 ed cc f9 | vG-....]..Z..... |
00000090  01 6a 32 44 1f                                  | .j2D.            |

Verify the bytecount here.

Python, 287 bytes

lambda x:[*permutations(x)][0x7b615ec218d9684ced86cf775d91acba8a10d91e8c0ba9575c4d35ad7acdd4ab961fec9af76bf28dfd2052f527c7eb3b2f31ef5ffc7e51db33e6055b85bf5395efeb0474c9e9ec3d1ebd7cd57f98c80c580ee7a4ee71483aac1176338387311cfecd066f8aa84e78a359328c58af2acf0a9ecdbbe]
from itertools import*

Attempt This Online! (Times out on ATO)

Port of Kevin Cruijssen's 05AB1E answer. Gets the (a very big number)th permutation of the input list.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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