11
\$\begingroup\$

The Task

The task is easy: given an Alpha-2 country code, output the Alpha-3 equivalent. The input may be in any case you choose, and the output in any consistent case of your choosing. Here are the codes:

[["AF", "AFG"], ["AX", "ALA"], ["AL", "ALB"], ["DZ", "DZA"], ["AS", "ASM"], ["AD", "AND"], ["AO", "AGO"], ["AI", "AIA"], ["AQ", "ATA"], ["AG", "ATG"], ["AR", "ARG"], ["AM", "ARM"], ["AW", "ABW"], ["AU", "AUS"], ["AT", "AUT"], ["AZ", "AZE"], ["BS", "BHS"], ["BH", "BHR"], ["BD", "BGD"], ["BB", "BRB"], ["BY", "BLR"], ["BE", "BEL"], ["BZ", "BLZ"], ["BJ", "BEN"], ["BM", "BMU"], ["BT", "BTN"], ["BO", "BOL"], ["BA", "BIH"], ["BW", "BWA"], ["BV", "BVT"], ["BR", "BRA"], ["VG", "VGB"], ["IO", "IOT"], ["BN", "BRN"], ["BG", "BGR"], ["BF", "BFA"], ["BI", "BDI"], ["KH", "KHM"], ["CM", "CMR"], ["CA", "CAN"], ["CV", "CPV"], ["KY", "CYM"], ["CF", "CAF"], ["TD", "TCD"], ["CL", "CHL"], ["CN", "CHN"], ["HK", "HKG"], ["MO", "MAC"], ["CX", "CXR"], ["CC", "CCK"], ["CO", "COL"], ["KM", "COM"], ["CG", "COG"], ["CD", "COD"], ["CK", "COK"], ["CR", "CRI"], ["CI", "CIV"], ["HR", "HRV"], ["CU", "CUB"], ["CY", "CYP"], ["CZ", "CZE"], ["DK", "DNK"], ["DJ", "DJI"], ["DM", "DMA"], ["DO", "DOM"], ["EC", "ECU"], ["EG", "EGY"], ["SV", "SLV"], ["GQ", "GNQ"], ["ER", "ERI"], ["EE", "EST"], ["ET", "ETH"], ["FK", "FLK"], ["FO", "FRO"], ["FJ", "FJI"], ["FI", "FIN"], ["FR", "FRA"], ["GF", "GUF"], ["PF", "PYF"], ["TF", "ATF"], ["GA", "GAB"], ["GM", "GMB"], ["GE", "GEO"], ["DE", "DEU"], ["GH", "GHA"], ["GI", "GIB"], ["GR", "GRC"], ["GL", "GRL"], ["GD", "GRD"], ["GP", "GLP"], ["GU", "GUM"], ["GT", "GTM"], ["GG", "GGY"], ["GN", "GIN"], ["GW", "GNB"], ["GY", "GUY"], ["HT", "HTI"], ["HM", "HMD"], ["VA", "VAT"], ["HN", "HND"], ["HU", "HUN"], ["IS", "ISL"], ["IN", "IND"], ["ID", "IDN"], ["IR", "IRN"], ["IQ", "IRQ"], ["IE", "IRL"], ["IM", "IMN"], ["IL", "ISR"], ["IT", "ITA"], ["JM", "JAM"], ["JP", "JPN"], ["JE", "JEY"], ["JO", "JOR"], ["KZ", "KAZ"], ["KE", "KEN"], ["KI", "KIR"], ["KP", "PRK"], ["KR", "KOR"], ["KW", "KWT"], ["KG", "KGZ"], ["LA", "LAO"], ["LV", "LVA"], ["LB", "LBN"], ["LS", "LSO"], ["LR", "LBR"], ["LY", "LBY"], ["LI", "LIE"], ["LT", "LTU"], ["LU", "LUX"], ["MK", "MKD"], ["MG", "MDG"], ["MW", "MWI"], ["MY", "MYS"], ["MV", "MDV"], ["ML", "MLI"], ["MT", "MLT"], ["MH", "MHL"], ["MQ", "MTQ"], ["MR", "MRT"], ["MU", "MUS"], ["YT", "MYT"], ["MX", "MEX"], ["FM", "FSM"], ["MD", "MDA"], ["MC", "MCO"], ["MN", "MNG"], ["ME", "MNE"], ["MS", "MSR"], ["MA", "MAR"], ["MZ", "MOZ"], ["MM", "MMR"], ["NA", "NAM"], ["NR", "NRU"], ["NP", "NPL"], ["NL", "NLD"], ["AN", "ANT"], ["NC", "NCL"], ["NZ", "NZL"], ["NI", "NIC"], ["NE", "NER"], ["NG", "NGA"], ["NU", "NIU"], ["NF", "NFK"], ["MP", "MNP"], ["NO", "NOR"], ["OM", "OMN"], ["PK", "PAK"], ["PW", "PLW"], ["PS", "PSE"], ["PA", "PAN"], ["PG", "PNG"], ["PY", "PRY"], ["PE", "PER"], ["PH", "PHL"], ["PN", "PCN"], ["PL", "POL"], ["PT", "PRT"], ["PR", "PRI"], ["QA", "QAT"], ["RE", "REU"], ["RO", "ROU"], ["RU", "RUS"], ["RW", "RWA"], ["BL", "BLM"], ["SH", "SHN"], ["KN", "KNA"], ["LC", "LCA"], ["MF", "MAF"], ["PM", "SPM"], ["VC", "VCT"], ["WS", "WSM"], ["SM", "SMR"], ["ST", "STP"], ["SA", "SAU"], ["SN", "SEN"], ["RS", "SRB"], ["SC", "SYC"], ["SL", "SLE"], ["SG", "SGP"], ["SK", "SVK"], ["SI", "SVN"], ["SB", "SLB"], ["SO", "SOM"], ["ZA", "ZAF"], ["GS", "SGS"], ["SS", "SSD"], ["ES", "ESP"], ["LK", "LKA"], ["SD", "SDN"], ["SR", "SUR"], ["SJ", "SJM"], ["SZ", "SWZ"], ["SE", "SWE"], ["CH", "CHE"], ["SY", "SYR"], ["TW", "TWN"], ["TJ", "TJK"], ["TZ", "TZA"], ["TH", "THA"], ["TL", "TLS"], ["TG", "TGO"], ["TK", "TKL"], ["TO", "TON"], ["TT", "TTO"], ["TN", "TUN"], ["TR", "TUR"], ["TM", "TKM"], ["TC", "TCA"], ["TV", "TUV"], ["UG", "UGA"], ["UA", "UKR"], ["AE", "ARE"], ["GB", "GBR"], ["US", "USA"], ["UM", "UMI"], ["UY", "URY"], ["UZ", "UZB"], ["VU", "VUT"], ["VE", "VEN"], ["VN", "VNM"], ["VI", "VIR"], ["WF", "WLF"], ["EH", "ESH"], ["YE", "YEM"], ["ZM", "ZMB"], ["ZW", "ZWE"]]

Test Cases

US -> USA
GT -> GTM
VA -> VAT
IN -> IND
KP -> PRK

Scoring

Because my soft drive can only store a kilobyte, and because this is , the shortest code in bytes wins.

This is the reverse of this challenge.

\$\endgroup\$

9 Answers 9

5
\$\begingroup\$

JavaScript (ES6),  582  554 bytes

s=>("CCAPMSSS"[x="KYKMTFKPYTPMRSGSAXAQBYBJBAMOEEGWIEILSIUA".search([a,b]=s)/2]||a)+("YOTRYPRGLTLEIASNRSVK"[x]+"MMFKTMBSAARNHCTBLRNR"[x]||(c="E8bOUX3b11R1O1nadL2DIRG1ntTRlSdIeSo10M1L1RK2C2D2RL1U2i4L22N23N3RynL2ao1c3IEr2l1r11T39U9U5S15UlyNw1PN1MvEReM2uDP1l2Rw13c2O2KLSkuN2u1O1uN25I11rB10T1T1N1B1R4M6TnrGt4BrTg2GMuS1bl1E10Mr1gL1RRd2MUrL3hN1T3l10N1Ko1aoEV1ohRhL2I2BpMRPE14U4In3M3F11B7U1E1Ys9IPH24NIl1s1r21BR1rOuY1B2rBi1lnC1MM3u21G1DD3V1IN18N8NDT1rNL21Y7a1RN24N1ZMR8o4T2a10ON1".replace(/\d+/g,n=>"A".repeat(n))[parseInt(s,36)%774],c>{}?c.toUpperCase()+b:b+c))

Try it online!

How?

The most common letter that needs to be added to an alpha-2 code to get the alpha-3 code is A. Therefore, it's the best suited padding character for our main lookup string, which is expanded by replacing all integers with the corresponding number of A's.

Try it online!

The lookup function is a single modulo on the input parsed in base-36:

parseInt(s, 36) % 774

If we get a character in upper case, it means that it must be added at the end of the alpha-2 code.

"AF" ~> "G" ~> "AFG"

If we get a character in lower case, it means that it must be added in the middle of the alpha-2 code:

"AO" ~> "g" ~> "AGO"

Similarly to my answer to the other challenge, the 20 codes that do not follow the above rules are hard-coded separately, just the other way around.

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

Perl 5 -MLocale::Country -pl, 48 40 bytes

$_=country_code2code($_,,'alpha-3')||ant

Try it online!

For some reason, the AN->ANT mapping isn't in Perl's list so I had to hardcode it.

\$\endgroup\$
1
  • \$\begingroup\$ Check the comments here for why that is the case. Nice answer! \$\endgroup\$
    – Komali
    May 19, 2022 at 2:09
3
\$\begingroup\$

05AB1E, 435 426 422 bytes

.•4₆₆ÿ¸˜E¡´³*ιœýOóÖq₅;YpÂ-|ož®Ωg4∊₅“=à–ô9αA$ΔL,žÆE6óµQ£‚Ç\ΣN¬ÓóÑC∞\†ìвö—'‰ßÍ™¶HFü"í4·nÛZ0ø₅k{'¦Δ"¢h¦h)U–̱4ƒ¿¢
œ\a^»ãfzλ»Ï˜≠7>2e5Nl”SGed.iÉVó¸£₃l&C§ÚΣ6½+¢ å(₃L¦Ûā:vÊιOL†aëƵ₁fǝfVEtþΣ{õ-o¡ø4•u2ôD•46‰εcĆ2Zâæ¥ÙÚMт'•₂в£εN>.b«}˜ø.•1…¤θ30ËиʒÕf%γžBpéт(ĆVáä∞Kî%àôλ#KbɵC‘jÊα-zª+UΛGÙÀ"zFûÌιô#fиαL¸Λ¾o₄Ò{žwým«‚È3÷«εΩ)ØN÷U•u2ôD•Gò7¬ýà®vQ<ÑU•12в£ε€SN>.bý}˜áø.•Úù»ÔáÝâ|¹мœ4å)Çø{:вoÒaÿšмk*øW—Ã*‹Σc̉Δú¤š3rSwsJܲùη₅ĀPŽEf~:•u23S40∍£)˜DIk>è

Try it online or verify all test cases.

Explanation:

Step 1a: Create I/O pairs of all alpha-2 codes for which a single trailing letter has to be added for the alpha-3 conversion:

.•4₆₆ÿ¸˜E¡´³*ιœýOóÖq₅;YpÂ-|ož®Ωg4∊₅“=à–ô9αA$ΔL,žÆE6óµQ£‚Ç\ΣN¬ÓóÑC∞\†ìвö—'‰ßÍ™¶HFü"í4·nÛZ0ø₅k{'¦Δ"¢h¦h)U–̱4ƒ¿¢
œ\a^»ãfzλ»Ï˜≠7>2e5Nl”SGed.iÉVó¸£₃l&C§ÚΣ6½+¢ å(₃L¦Ûā:vÊιOL†aëƵ₁fǝfVEtþΣ{õ-o¡ø4•
              "# Push compressed string "dzaibwbrbfdmfrghitlvmdngrwknlclktzthtcugusalvgcugagmgiuzzmgrnihmhninmknlssazczlipsslchzwzaafarhkmnetcrdjerfjhtmwmlprumccnftjbebocoismhnpncnzphtkaskhdogugtnablwssosjvnyebtcafihuidirimjpkelbompashsdtwtovegelalsmctgttcystsgesbhbgcmcxjokimsmammnenopesmsygbviaumymurutlbviovakwmranqavcvubmecdeltnrrerosacihrluegggjekg"
 u             # Uppercase it
  2ô           # Split it into parts of size 2
    D          # Duplicate this list of pairs
•46‰εcĆ2Zâæ¥ÙÚMт'•
              '# Push compressed integer 5045888909142307005039435397419896531
 ₂в            # Convert it to base-26 as list: [21,8,2,6,7,1,4,1,9,0,3,10,12,17,6,4,0,16,5,9,8,2,0,1,3,1]
   £           # Split the duplicated pairs into parts of that size
    ε          # Map over each list of string-pairs:
     N>        #  Push the 1-based map-index
       .b      #  Convert it to an uppercase alphabetic letter (1=A,2=B,...,26=Z)
         «     #  Append it to each string-pair in the current list
    }˜         # After the map: flatten
      ø        # Create pairs of the two lists

Try just this step, to see all I/O pairs of step 1a.

Step 1b: Create I/O pairs of all alpha-2 codes for which a single middle letter has to be added for the alpha-3 conversion:

.•1…¤θ30ËиʒÕf%γžBpéт(ĆVáä∞Kî%àôλ#KbɵC‘jÊα-zª+UΛGÙÀ"zFûÌιô#fиαL¸Λ¾o₄Ò{žwým«‚È3÷«εΩ)ØN÷U•
               # Push compressed string "cfjmkzpkmfawlrlytdpnbimgmvmxsnaobdbsclcngnnutmbzsvfkgpmtpwsbwfaddkgqmemppgcgcdckkrmzplcvambbbnfoglgdiqpyptaeuyfmehagmqatgfgysrtntrtvskszsepfsc"
 u2ôD          # Same as above (uppercase; split into pairs; duplicate)
•Gò7¬ýà®vQ<ÑU• # Push compressed integer 5023299766197034891137684120
 12в           # Convert it to base-12 as list: [5,3,2,3,2,0,2,3,2,0,1,8,0,6,6,1,0,11,2,2,7,1,2,0,2,0]
    £          # Split the duplicated pairs into parts of that size
     ε         # Map over each list of string-pairs:
      €S       #  Convert each string to a list
        N>.b   #  Same as above: get the uppercase letter based on the index
            ý  #  Join the inner list-pairs with this letter as delimiter
     }˜áø      # Same as above, but `á` removes empty strings from the list

Try just this step, to see all I/O pairs of step 1b.

Step 1c: Create I/O pairs for all remaining alpha-2 to alpha-3 conversions (as a flattened list):

.•Úù»ÔáÝâ|¹мœ4å)Çø{:вoÒaÿšмk*øW—Ã*‹Σc̉Δú¤š3rSwsJܲùη₅ĀPŽEf~:•
               # Push compressed string "aqataaxalababihbjbenbyblreeestgssgsgwgnbieirlilisrkmcomkpprkkycymmomacpmspmrssrbsisvntfatfuaukrytmyt"
 u             # Uppercase it
  23S          # Push [2,3]
     40∍       # Extend it to size 40: [2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3]
        £      # Split the string into parts of that size

Try just this step, to see the flattened list of all the I/O pairs of step 1c.

Step 2: Combine the results of the previous steps, and use them for the input to output conversion:

)              # Wrap the entire stack into a list
 ˜             # Flatten
  D            # Duplicate this list
   Ik          # Get the index of the input-string
     >         # Increase this index by 1
      è        # And use it to index into the list
               # (after which the result is output implicitly)

See this 05AB1E tip of mine (sections How to compress strings not part of the dictionary?, How to compress large integers?, and How to compress integer lists?) to understand how the compressions work.

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

R, 74 bytes

function(c)`if`(c=="AN","ANT",countrycode::countrycode(c,"iso2c","iso3c"))

Try it rdrr.io!

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

Python 1779 bytes

a=' AF AFG AX ALA AL ALB DZ DZA AS ASM AD AND AO AGO AI AIA AQ ATA AG ATG AR ARG AM ARM AW ABW AU AUS AT AUT AZ AZE BS BHS BH BHR BD BGD BB BRB BY BLR BE BEL BZ BLZ BJ BEN BM BMU BT BTN BO BOL BA BIH BW BWA BV BVT BR BRA VG VGB IO IOT BN BRN BG BGR BF BFA BI BDI KH KHM CM CMR CA CAN CV CPV KY CYM CF CAF TD TCD CL CHL CN CHN HK HKG MO MAC CX CXR CC CCK CO COL KM COM CG COG CD COD CK COK CR CRI CI CIV HR HRV CU CUB CY CYP CZ CZE DK DNK DJ DJI DM DMA DO DOM EC ECU EG EGY SV SLV GQ GNQ ER ERI EE EST ET ETH FK FLK FO FRO FJ FJI FI FIN FR FRA GF GUF PF PYF TF ATF GA GAB GM GMB GE GEO DE DEU GH GHA GI GIB GR GRC GL GRL GD GRD GP GLP GU GUM GT GTM GG GGY GN GIN GW GNB GY GUY HT HTI HM HMD VA VAT HN HND HU HUN IS ISL IN IND ID IDN IR IRN IQ IRQ IE IRL IM IMN IL ISR IT ITA JM JAM JP JPN JE JEY JO JOR KZ KAZ KE KEN KI KIR KP PRK KR KOR KW KWT KG KGZ LA LAO LV LVA LB LBN LS LSO LR LBR LY LBY LI LIE LT LTU LU LUX MK MKD MG MDG MW MWI MY MYS MV MDV ML MLI MT MLT MH MHL MQ MTQ MR MRT MU MUS YT MYT MX MEX FM FSM MD MDA MC MCO MN MNG ME MNE MS MSR MA MAR MZ MOZ MM MMR NA NAM NR NRU NP NPL NL NLD AN ANT NC NCL NZ NZL NI NIC NE NER NG NGA NU NIU NF NFK MP MNP NO NOR OM OMN PK PAK PW PLW PS PSE PA PAN PG PNG PY PRY PE PER PH PHL PN PCN PL POL PT PRT PR PRI QA QAT RE REU RO ROU RU RUS RW RWA BL BLM SH SHN KN KNA LC LCA MF MAF PM SPM VC VCT WS WSM SM SMR ST STP SA SAU SN SEN RS SRB SC SYC SL SLE SG SGP SK SVK SI SVN SB SLB SO SOM ZA ZAF GS SGS SS SSD ES ESP LK LKA SD SDN SR SUR SJ SJM SZ SWZ SE SWE CH CHE SY SYR TW TWN TJ TJK TZ TZA TH THA TL TLS TG TGO TK TKL TO TON TT TTO TN TUN TR TUR TM TKM TC TCA TV TUV UG UGA UA UKR AE ARE GB GBR US USA UM UMI UY URY UZ UZB VU VUT VE VEN VN VNM VI VIR WF WLF EH ESH YE YEM ZM ZMB ZW ZWE'

z=lambda b:a[a.find(" "+b)+4:a.find(" "+b)+7]

Not sure what to do with the mass of data.

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

Vyxal, 420 bytes

«⟑``‟Ḃȯ†√¦∑iRṫΠʁǓ₴5]p#4Ṅl}3ẋ¶T+⟑V]ḭr≤ṁǔ
A»cP⌐kẋṠ†⟇Ǔ≈ɽ⟩›ṪwSʀ₇⌊Ǔ∇•Xwð⇩I‡ṅfβ℅₍ṗµ∆€¾aǍ⟑Ȧ2⋏$λ⁺]/|f₁Pa⟨,₁,h¨ṁ₌ε⟑‹İ⟨Nṫw%yĖ⁼*>₈˹ȯU÷s¬)∨ʀ0ŀƈ½)(m-Ẋ ǓḂ⟩[0e•₂↲S⟇Ġj⟨^ɖg[⌐₁⋏żo:ẎRʁ⌐ð%⇧↓Y*?* †8„S|⁽l3√⁺⟑«⇧2ẇ:»⟑⟇βE'ċ¬$≠∪₀↳↲æMf»₄τẇkA¨£vJfZ«ƛ¤‛H∧λŻJ@±*dCẋ°4£Mgċ ≥ƒX¢ǎd≤ǔ₈b¢&Ẋ≈•ġ.ẎAl>₅j¨∵½↳Ŀa>€¼₌Rǔb*JA⌐Ḃ∵Ḣ3\⌈|ẋ;Π1₆›Ẇ∧≬₆ETh□ʀ≬¨«⇧2ẇ:»½ǒ÷₇Π≤¶:ɽq∇¨»12τẇkAZƛ÷$f$vj;fZ«↲↵˝≥꘍≠}ĊKṫ⟑ɖhṪ⁺|oI3⌈%‟ṙK/i⁺!ṅȮiȧS'₌βP⅛‛ṙ∧6ƈ;7↔⋎¡↵G]ḟɾḋ†*↑o«⇧23f20ẋfẇWf:⁰ḟ›i

Try it Online! or Verify all test cases (takes a while, but doesn't time out)

Messy port of 05AB1E.

How?

«⟑``‟Ḃȯ†√¦∑iRṫΠʁǓ₴5]p#4Ṅl}3ẋ¶T+⟑V]ḭr≤ṁǔ
A»cP⌐kẋṠ†⟇Ǔ≈ɽ⟩›ṪwSʀ₇⌊Ǔ∇•Xwð⇩I‡ṅfβ℅₍ṗµ∆€¾aǍ⟑Ȧ2⋏$λ⁺]/|f₁Pa⟨,₁,h¨ṁ₌ε⟑‹İ⟨Nṫw%yĖ⁼*>₈˹ȯU÷s¬)∨ʀ0ŀƈ½)(m-Ẋ ǓḂ⟩[0e•₂↲S⟇Ġj⟨^ɖg[⌐₁⋏żo:ẎRʁ⌐ð%⇧↓Y*?* †8„S|⁽l3√⁺⟑«
                            # Push compressed string "dzaibwbrbfdmfrghitlvmdngrwknlclktzthtcugusalvgcugagmgiuzzmgrnihmhninmknlssazczlipsslchzwzaafarhkmnetcrdjerfjhtmwmlprumccnftjbebocoismhnpncnzphtkaskhdogugtnablwssosjvnyebtcafihuidirimjpkelbompashsdtwtovegelalsmctgttcystsgesbhbgcmcxjokimsmammnenopesmsygbviaumymurutlbviovakwmranqavcvubmecdeltnrrerosacihrluegggjekg"
⇧                           # Uppercase
 2ẇ                         # Split into chunks of two
   :                        # Duplicate
    »⟑⟇βE'ċ¬$≠∪₀↳↲æMf»      # Push compressed integer 5045888909142307005039435397419896531
                      ₄τ    # Convert to base-26 as a list: [21,8,2,6,7,1,4,1,9,0,3,10,12,17,6,4,0,16,5,9,8,2,0,1,3,1]
                        ẇ   # Split the string into chunks of that size: [["DZ", "AI", ..., "US"], ["AL", "VG", ..., "ZM"], ["GR", "NI"], ..., ["KG"]]
kA                          # Push the uppercase alphabet
  ¨£vJ                      # Zip the list with the alphabet, and for each, append the corresponding letter to each:
                            #  [["DZA", "AIA", ..., "USA"], ["ALB", "VGB", ..., "ZMB"], ["GRC", "NIC"], ..., ["KGZ"]]
      f                     # Flatten
       Z                    # Zip the ["DZ", "AI", "BW", ...] list with this
«ƛ¤‛H∧λŻJ@±*dCẋ°4£Mgċ ≥ƒX¢ǎd≤ǔ₈b¢&Ẋ≈•ġ.ẎAl>₅j¨∵½↳Ŀa>€¼₌Rǔb*JA⌐Ḃ∵Ḣ3\⌈|ẋ;Π1₆›Ẇ∧≬₆ETh□ʀ≬¨«
                            # Push compressed string "cfjmkzpkmfawlrlytdpnbimgmvmxsnaobdbsclcngnnutmbzsvfkgpmtpwsbwfaddkgqmemppgcgcdckkrmzplcvambbbnfoglgdiqpyptaeuyfmehagmqatgfgysrtntrtvskszsepfsc"
        ⇧2ẇ                 # Uppercase and split into chunks of two
           :                # Duplicate
            »½ǒ÷₇Π≤¶:ɽq∇¨»  # Push compressed integer 5023299766197034891137684120
12τ                         # Convert to base 12 as a list: [5,3,2,3,2,0,2,3,2,0,1,8,0,6,6,1,0,11,2,2,7,1,2,0,2,0]
   ẇ                        # Split the list into chunks of that size: [["CF", "JM", "KZ", "PK", "MF"], ["AW", "LR", "LY"], ..., ["PF", "SC"], []]
    kAZ                     # Zip with the uppercase alphabet
       ƛ÷$f$vj;             # Map, and for each, insert the letter between each string: [["CAF", "JAM", "KAZ", "PAK", "MAF"], ..., ["PYF", "SYC"], []]
               fZ           # Flatten and zip the ["CF", "JM", ..., "SC"] list with this
«↲↵˝≥꘍≠}ĊKṫ⟑ɖhṪ⁺|oI3⌈%‟ṙK/i⁺!ṅȮiȧS'₌βP⅛‛ṙ∧6ƈ;7↔⋎¡↵G]ḟɾḋ†*↑o«
                            # Push compressed string "aqataaxalababihbjbenbyblreeestgssgsgwgnbieirlilisrkmcomkpprkkycymmomacpmspmrssrbsisvntfatfuaukrytmyt"
⇧                           # Uppercase
 23f                        # Push list [2, 3]
    20ẋf                    # Repeat it twenty times: [2, 3, 2, 3, 2, 3, ..., 2, 3]
        ẇ                   # Split the string into chunks of that size: ["AQ", "ATA", "AX", ..., "YT", "MYT"]
         W                  # Wrap the stack into a list
          f                 # Flatten
           :                # Duplicate
            ⁰ḟ              # Find the index of the input in it
              ›             # Increment
               i            # Index into the list
\$\endgroup\$
1
\$\begingroup\$

C - 1324 Bytes

char*c="AFAFGAXALAALALBDZDZAASASMADANDAOAGOAIAIAAQATAAGATGARARGAMARMAWABWAUAUSATAUTAZAZEBSBHSBHBHRBDBGDBBBRBBYBLRBEBELBZBLZBJBENBMBMUBTBTNBOBOLBABIHBWBWABVBVTBRBRAVGVGBIOIOTBNBRNBGBGRBFBFABIBDIKHKHMCMCMRCACANCVCPVKYCYMCFCAFTDTCDCLCHLCNCHNHKHKGMOMACCXCXRCCCCKCOCOLKMCOMCGCOGCDCODCKCOKCRCRICICIVHRHRVCUCUBCYCYPCZCZEDKDNKDJDJIDMDMADODOMECECUEGEGYSVSLVGQGNQERERIEEESTETETHFKFLKFOFROFJFJIFIFINFRFRAGFGUFPFPYFTFATFGAGABGMGMBGEGEODEDEUGHGHAGIGIBGRGRCGLGRLGDGRDGPGLPGUGUMGTGTMGGGGYGNGINGWGNBGYGUYHTHTIHMHMDVAVATHNHNDHUHUNISISLININDIDIDNIRIRNIQIRQIEIRLIMIMNILISRITITAJMJAMJPJPNJEJEYJOJORKZKAZKEKENKIKIRKPPRKKRKORKWKWTKGKGZLALAOLVLVALBLBNLSLSOLRLBRLYLBYLILIELTLTULULUXMKMKDMGMDGMWMWIMYMYSMVMDVMLMLIMTMLTMHMHLMQMTQMRMRTMUMUSYTMYTMXMEXFMFSMMDMDAMCMCOMNMNGMEMNEMSMSRMAMARMZMOZMMMMRNANAMNRNRUNPNPLNLNLDANANTNCNCLNZNZLNINICNENERNGNGANUNIUNFNFKMPMNPNONOROMOMNPKPAKPWPLWPSPSEPAPANPGPNGPYPRYPEPERPHPHLPNPCNPLPOLPTPRTPRPRIQAQATREREUROROURURUSRWRWABLBLMSHSHNKNKNALCLCAMFMAFPMSPMVCVCTWSWSMSMSMRSTSTPSASAUSNSENRSSRBSCSYCSLSLESGSGPSKSVKSISVNSBSLBSOSOMZAZAFGSSGSSSSSDESESPLKLKASDSDNSRSURSJSJMSZSWZSESWECHCHESYSYRTWTWNTJTJKTZTZATHTHATLTLSTGTGOTKTKLTOTONTTTTOTNTUNTRTURTMTKMTCTCATVTUVUGUGAUAUKRAEAREGBGBRUSUSAUMUMIUYURYUZUZBVUVUTVEVENVNVNMVIVIRWFWLFEHESHYEYEMZMZMBZWZWE";f(char* i){for(;*c;c+=5)!strncmp(c, i,2)&&printf("%c%c%c",*(c+2),*(c+3),*(c+4));}

Ungolfed

char*c = "AFAFGAXALAALALBDZDZAASASMADANDAOAGOAIAIAAQATAAGATGARARGAMARMAWABWAUAUSATAUTAZAZEBSBHSBHBHRBDBGDBBBRBBYBLRBEBELBZBLZBJBENBMBMUBTBTNBOBOLBABIHBWBWABVBVTBRBRAVGVGBIOIOTBNBRNBGBGRBFBFABIBDIKHKHMCMCMRCACANCVCPVKYCYMCFCAFTDTCDCLCHLCNCHNHKHKGMOMACCXCXRCCCCKCOCOLKMCOMCGCOGCDCODCKCOKCRCRICICIVHRHRVCUCUBCYCYPCZCZEDKDNKDJDJIDMDMADODOMECECUEGEGYSVSLVGQGNQERERIEEESTETETHFKFLKFOFROFJFJIFIFINFRFRAGFGUFPFPYFTFATFGAGABGMGMBGEGEODEDEUGHGHAGIGIBGRGRCGLGRLGDGRDGPGLPGUGUMGTGTMGGGGYGNGINGWGNBGYGUYHTHTIHMHMDVAVATHNHNDHUHUNISISLININDIDIDNIRIRNIQIRQIEIRLIMIMNILISRITITAJMJAMJPJPNJEJEYJOJORKZKAZKEKENKIKIRKPPRKKRKORKWKWTKGKGZLALAOLVLVALBLBNLSLSOLRLBRLYLBYLILIELTLTULULUXMKMKDMGMDGMWMWIMYMYSMVMDVMLMLIMTMLTMHMHLMQMTQMRMRTMUMUSYTMYTMXMEXFMFSMMDMDAMCMCOMNMNGMEMNEMSMSRMAMARMZMOZMMMMRNANAMNRNRUNPNPLNLNLDANANTNCNCLNZNZLNINICNENERNGNGANUNIUNFNFKMPMNPNONOROMOMNPKPAKPWPLWPSPSEPAPANPGPNGPYPRYPEPERPHPHLPNPCNPLPOLPTPRTPRPRIQAQATREREUROROURURUSRWRWABLBLMSHSHNKNKNALCLCAMFMAFPMSPMVCVCTWSWSMSMSMRSTSTPSASAUSNSENRSSRBSCSYCSLSLESGSGPSKSVKSISVNSBSLBSOSOMZAZAFGSSGSSSSSDESESPLKLKASDSDNSRSURSJSJMSZSWZSESWECHCHESYSYRTWTWNTJTJKTZTZATHTHATLTLSTGTGOTKTKLTOTONTTTTOTNTUNTRTURTMTKMTCTCATVTUVUGUGAUAUKRAEAREGBGBRUSUSAUMUMIUYURYUZUZBVUVUTVEVENVNVNMVIVIRWFWLFEHESHYEYEMZMZMBZWZWE";

f(char* i)
{
    for(; *c; c += 5)
        !strncmp(c, i, 2) && printf("%c%c%c", *(c + 2), *(c + 3), *(c + 4));
}

Explanation

A function that receives a character string with an Alpha-2 code and converts it to Alpha-3 using a "raw" list which contains the Alpha-2 codes followed by their Alpha-3 counterpart every five(5) characters; the function looks for the given code within the list and if it is found its counterpart is displayed.

Tested on GCC, generates some warnings if not compiled with std=c89.

C99 - 1296 Bytes (By @ceilingcat)

f(int*i){for(char*c="AFAFGAXALAALALBDZDZAASASMADANDAOAGOAIAIAAQATAAGATGARARGAMARMAWABWAUAUSATAUTAZAZEBSBHSBHBHRBDBGDBBBRBBYBLRBEBELBZBLZBJBENBMBMUBTBTNBOBOLBABIHBWBWABVBVTBRBRAVGVGBIOIOTBNBRNBGBGRBFBFABIBDIKHKHMCMCMRCACANCVCPVKYCYMCFCAFTDTCDCLCHLCNCHNHKHKGMOMACCXCXRCCCCKCOCOLKMCOMCGCOGCDCODCKCOKCRCRICICIVHRHRVCUCUBCYCYPCZCZEDKDNKDJDJIDMDMADODOMECECUEGEGYSVSLVGQGNQERERIEEESTETETHFKFLKFOFROFJFJIFIFINFRFRAGFGUFPFPYFTFATFGAGABGMGMBGEGEODEDEUGHGHAGIGIBGRGRCGLGRLGDGRDGPGLPGUGUMGTGTMGGGGYGNGINGWGNBGYGUYHTHTIHMHMDVAVATHNHNDHUHUNISISLININDIDIDNIRIRNIQIRQIEIRLIMIMNILISRITITAJMJAMJPJPNJEJEYJOJORKZKAZKEKENKIKIRKPPRKKRKORKWKWTKGKGZLALAOLVLVALBLBNLSLSOLRLBRLYLBYLILIELTLTULULUXMKMKDMGMDGMWMWIMYMYSMVMDVMLMLIMTMLTMHMHLMQMTQMRMRTMUMUSYTMYTMXMEXFMFSMMDMDAMCMCOMNMNGMEMNEMSMSRMAMARMZMOZMMMMRNANAMNRNRUNPNPLNLNLDANANTNCNCLNZNZLNINICNENERNGNGANUNIUNFNFKMPMNPNONOROMOMNPKPAKPWPLWPSPSEPAPANPGPNGPYPRYPEPERPHPHLPNPCNPLPOLPTPRTPRPRIQAQATREREUROROURURUSRWRWABLBLMSHSHNKNKNALCLCAMFMAFPMSPMVCVCTWSWSMSMSMRSTSTPSASAUSNSENRSSRBSCSYCSLSLESGSGPSKSVKSISVNSBSLBSOSOMZAZAFGSSGSSSSSDESESPLKLKASDSDNSRSURSJSJMSZSWZSESWECHCHESYSYRTWTWNTJTJKTZTZATHTHATLTLSTGTGOTKTKLTOTONTTTTOTNTUNTRTURTMTKMTCTCATVTUVUGUGAUAUKRAEAREGBGBRUSUSAUMUMIUYURYUZUZBVUVUTVEVENVNVNMVIVIRWFWLFEHESHYEYEMZMZMBZWZWE";*c;c+=5)write(!strncmp(c,i,2),c+2,3);}
\$\endgroup\$
1
  • \$\begingroup\$ Suggest adding a "Try it Online" or similar link. \$\endgroup\$
    – ceilingcat
    May 15, 2022 at 20:03
1
\$\begingroup\$

Wolfram Language (Mathematica), 111 bytes

Switch[#,"HM","HMD","AN","ANT","PS","PSE",_,Association[#@"CountryCode"->#@"UNCode"&/@EntityList@"Country"]@#]&

Don't Try it online!

Try in the Wolfram Cloud!


Explanation:

Switch[#,"HM","HMD","AN","ANT","PS","PSE", If our input is equal to one of the three countries not in the Mathematica database, return the correct code.

_, Otherwise

Association[#@"CountryCode"->#@"UNCode"&/@EntityList@"Country"] Create an association between the two-letter and three-letter codes of each country

@#] and find the value matching the input.

\$\endgroup\$
2
  • \$\begingroup\$ Mathematica: a builtin for everything :P \$\endgroup\$
    – Seggan
    May 16, 2022 at 16:45
  • \$\begingroup\$ For downvoters, why? Is it just due to using a builtin? \$\endgroup\$
    – Romanp
    May 18, 2022 at 22:22
1
\$\begingroup\$

C, 216+554+1=771 bytes

#include<stdio.h>
#define G d=getc(f);w=d&31|64
FILE*f;char c,d,r[4],y,w;char*z(a,b){for(f=fopen("f","r");;){G;c=d/4&56;y=w;G;c|=d>>5&7;*r=a;r[1]=r[2]=b;r[c>26?1:2]=c&31|64;if(!c)fgets(r,4,f);if(a==y&b==w)return r;}}

It requires this file with the name "f" in the same directory, shown here as a hex dump. You can use cut -c 11-58 "data.txt" | xxd -r -p > f to recreate the file. It should have 554 bytes.

00000000  01 e6 01 18 41 4c 41 01  4c 04 3a 21 b3 a1 c4 81  |....ALA.L.:!....|
00000010  ef 01 29 01 11 41 54 41  c1 87 01 f2 c1 4d 81 57  |..)..ATA.....M.W|
00000020  41 75 c1 b4 01 ba a2 13  42 48 82 e4 c2 42 02 19  |Au......BH...B..|
00000030  42 4c 52 22 85 a2 9a 02  0a 42 45 4e 42 ad 22 d4  |BLR".....BENB.".|
00000040  22 8f 02 01 42 49 48 02  37 42 96 02 32 16 47 49  |"...BIH.7B..2.GI|
00000050  8f c2 4e 42 47 02 26 82  89 2b a8 43 4d 23 c1 c3  |..NBG.&..+.CM#..|
00000060  16 0b 19 43 59 4d 83 26  94 64 a3 0c a3 0e 08 eb  |...CYM.&.d......|
00000070  0d 0f 4d 41 43 43 58 23  63 23 8f 0b 0d 43 4f 4d  |..MACCX#c#...COM|
00000080  a3 e7 a3 e4 a3 eb 23 32  43 c9 48 d2 03 55 43 19  |......#2C.H..UC.|
00000090  03 ba a4 cb 24 2a 04 2d  24 af 45 a3 65 27 b3 96  |....$*.-$.E.e'..|
000000a0  a7 d1 25 32 05 05 45 53  54 25 14 a6 8b c6 4f 26  |..%2..EST%....O&|
000000b0  2a 26 c9 06 32 c7 a6 f0  26 14 06 41 54 46 07 41  |*&..2...&..ATF.A|
000000c0  07 4d 27 e5 44 a5 07 28  07 49 07 72 c7 4c c7 44  |.M'.D..(.I.r.L.D|
000000d0  a7 90 27 b5 27 b4 67 27  a7 2e 07 17 47 4e 42 c7  |..'.'.g'....GNB.|
000000e0  b9 28 34 08 8d 56 81 08  8e 28 d5 29 93 09 8e 29  |.(4..V...(.)...)|
000000f0  c4 29 d2 c9 51 09 05 49  52 4c 29 cd 09 0c 49 53  |.)..Q..IRL)...IS|
00000100  52 09 34 8a 2d 2a d0 6a  25 4a 4f 8b 3a 2b c5 4b  |R.4.-*.j%JO.:+.K|
00000110  49 0b 10 50 52 4b ab f2  4b 97 6b 47 2c e1 0c 36  |I..PRK..K.kG,..6|
00000120  2c c2 2c f3 8c 52 8c 59  0c a9 4c b4 6c 15 0d 8b  |,.,..R.Y..L.l...|
00000130  8d 87 2d 37 4d 79 8d 96  2d 2c ad 94 2d 88 cd 91  |..-7My..-,..-...|
00000140  4d 92 4d 75 19 14 4d 59  54 8d b8 c6 6d 0d 24 2d  |M.Mu..MYT...m.$-|
00000150  e3 0d ee ad c5 4d 53 4d  41 ad fa 4d 4d 2e a1 4e  |.....MSMA..MM..N|
00000160  b2 2e 90 0e 8c 41 8e 2e  83 2e 9a 0e 69 4e 45 0e  |.....A......iNE.|
00000170  27 ae 35 2e 66 ad d0 4e  4f 2f cd 90 2b b0 97 10  |'.5.f..NO/..+...|
00000180  b3 30 c1 b0 c7 d0 59 50  45 30 88 90 6e b0 ec d0  |.0....YPE0..n...|
00000190  54 30 32 51 81 52 a5 52  af 52 75 12 37 22 ac 33  |T02Q.R.R.Ru.7".3|
000001a0  c8 0b 2e 0c 23 8d 26 10  0d 53 50 4d 56 83 37 b3  |....#.&..SPMV.7.|
000001b0  53 4d 53 14 53 a1 93 ae  12 13 53 52 42 f3 23 13  |SMS.S.....SRB.#.|
000001c0  ac 53 07 d3 cb 13 09 53  56 4e b3 82 33 af 1a c1  |.S.....SVN..3...|
000001d0  07 13 53 47 53 13 93 45  13 0c 2b 33 c4 d3 b2 33  |..SGS..E..+3...3|
000001e0  aa d3 fa d3 e5 03 a8 53  59 34 d7 34 6a 14 3a 14  |.......SY4.4j.:.|
000001f0  28 54 6c 34 e7 34 8b 34  cf 34 f4 d4 ae d4 b2 b4  |(Tl4.4.4.4......|
00000200  6d 14 23 d4 b6 15 27 15  01 55 4b 52 c1 45 47 42  |m.#...'..UKR.EGB|
00000210  15 33 35 2d d5 59 15 5a  56 95 36 c5 36 ae 56 49  |.35-.Y.ZV.6.6.VI|
00000220  b7 86 c5 68 39 a5 1a 4d  1a b7                    |...h9..M..|
0000022a

The function takes the Alpha-2 code as two separate characters, which must be capitalized, and returns the capitalized Alpha-3 code. I don't know of an online compiler that allows you to include binary files like this, so no link, unfortunately.

Explanation:

Since we're only dealing with capital letters, we can use only the lower five bits to store a character. The basic strategy is to store the Alpha-2 codes in the file, and use the bits we save to describe how to create the Alpha-3 code; then, the program will search for the Alpha-2 code and follow the instructions.

The instruction format is simple. We have six instruction bits. The lower five bits store a 5-bit letter code which can be converted to ASCII with &31|64. If the high bit is zero, the letter is added to the end of the code. If it's one, the letter is inserted in the middle. If the instruction bits are all zero, the file stores the complete Alpha-3 code in the next 3 bytes of the file (these are visible in the hex dump).

Of the 247 conversions, 156 append a character to the end, 71 insert a character in the middle, 4 prepend a character to the beginning, and 16 conversions are replacements, which are anything not included in the other categories. Inserts and appends only use two bytes in the file, while the others use five; (156+71)*2+(4+16)*5=554 bytes.

Ungolfed version:

#include <stdio.h>
FILE* f;
char c,d,r[4],y,w;
char* z(a,b){
    for(f=fopen("f","r");;){
        // get first character
        d=getc(f); w=d&31|64;
        c=d/4&56;
        y=w;
        // get second character
        d=getc(f); w=d&31|64;
        c|=d>>5&7; // c now holds the instruction
        // prepare return string for inserts/appends
        r[0]=a;
        r[1]=r[2]=b;
        r[c>26?1:2]=c&31|64; // insert or append
        if(!c) // replace
            fgets(r,4,f);
        if(a==y&b==w)
            return r;
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Suggest c||fgets(r,4,f) instead of if(!c)fgets(r,4,f) \$\endgroup\$
    – ceilingcat
    Apr 30 at 15:23

Your Answer

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

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