9
\$\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\$

5 Answers 5

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\$
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\$
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\$
3
  • \$\begingroup\$ 1296 bytes \$\endgroup\$
    – ceilingcat
    2 days ago
  • \$\begingroup\$ @ceilingcat thanks always for your comments. \$\endgroup\$
    – Louis Pine
    yesterday
  • \$\begingroup\$ Suggest adding a "Try it Online" or similar link. \$\endgroup\$
    – ceilingcat
    yesterday
1
\$\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\$
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\$
1
  • \$\begingroup\$ Mathematica: a builtin for everything :P \$\endgroup\$
    – Seggan
    yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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