9
\$\begingroup\$

There are 8 Australian states and territories, each with a 2 or 3 letter abbreviation:

  • ACT: Australian Capital Territory
  • NSW: New South Wales
  • NT: Northern Territory
  • QLD: Queensland
  • SA: South Australia
  • TAS: Tasmania
  • VIC: Victoria
  • WA: Western Australia

Your task is to write a function/program which takes a valid Australian state abbreviation in upper case, and returns the correct full name (case-sensitive as above).

No extra whitespace permitted, other than a single trailing newline where this is an unavoidable side-effect of every program in that language.

For example:

f("TAS") => "Tasmania"

Standard rules (shortest code in bytes wins!) and loopholes apply.

\$\endgroup\$
5
  • 6
    \$\begingroup\$ I think we need to create the [state-abbreviation] tag at this point. \$\endgroup\$
    – Cyoce
    Commented Jun 5, 2017 at 4:57
  • \$\begingroup\$ What about "New South (Far) Wales"? (For those who don't realize, it's "NSFW") \$\endgroup\$ Commented Jun 5, 2017 at 5:05
  • 3
    \$\begingroup\$ while I was playing around with a solution, NSW was expanding to "New South Western ales" which I quite liked :) \$\endgroup\$ Commented Jun 5, 2017 at 5:38
  • \$\begingroup\$ Weird. I created the tag and added it, but someone removed it again. \$\endgroup\$ Commented Jun 5, 2017 at 23:10
  • \$\begingroup\$ state-abbreviation might be too specific. maybe make the tag something about abbreviations/acronyms? \$\endgroup\$
    – Sparr
    Commented Jun 6, 2017 at 18:01

10 Answers 10

9
\$\begingroup\$

Retina, 135 121 bytes

13 bytes golfed thanks to @Neil by using clever space insertion tactics, which inspired one more byte being golfed

AS
asmania
^W
Western 
NT
NorthernT
T$
 Territory
A
Australia
aC
an Capital
IC
ictoria
LD
ueensland
SW
ew SWales
S
South 

Try it online!

Note the trailing spaces on the fourth and the last lines.

\$\endgroup\$
4
  • \$\begingroup\$ If you replace AS with asmania first, then you can replace all the remaining As with Australia. \$\endgroup\$
    – Neil
    Commented Jun 5, 2017 at 9:12
  • \$\begingroup\$ You can also get creative with your space insertion, something like this: Try it online! \$\endgroup\$
    – Neil
    Commented Jun 5, 2017 at 9:17
  • \$\begingroup\$ (But for that, \B[A-Z] would have saved you two bytes.) \$\endgroup\$
    – Neil
    Commented Jun 5, 2017 at 9:26
  • \$\begingroup\$ @Neil Thanks for the tips, I used your suggestions to golf one more byte. \$\endgroup\$
    – user41805
    Commented Jun 5, 2017 at 12:39
5
\$\begingroup\$

Jelly, 56 bytes

OS%15ị“ÞÑṿ“ıÐṁṾṗ“¡ʋẹḢ⁾ḅ“¢T¡ż¶““msẒw²ẉṪP“ØÑĊ“©$Ḅ3ẊḳƓ“ÇH°»

A monadic link taking and returning lists of characters.

See all cases at Try it online!

Note: Reusing the words "Australia" and "Territory" does not help reduce the byte count.

How?

Taking the sum of the ordinals of the eight state abbreviations modulo 15 gives 8 unique values. Taking the results of those modulo 9 maintains uniqueness.

OS%15ị“ ... » - Main link: list of characters a
O             - cast to ordinals
 S            - sum
  %15         - modulo 15
      “ ... » - list of compressed strings:
              -   "Victoria"
              -   "Western Australia"
              -   "Northern Territory"
              -   "South Australia"
              -   ""
              -   "Australian Capital Territory"
              -   "Tasmania"
              -   "New South Wales"
              -   "Queensland"
     ị        - index into (1-indexed and modular - hence the single empty entry)
\$\endgroup\$
1
  • \$\begingroup\$ Sad that reusing Australia and Territory doesn't help :/ \$\endgroup\$ Commented Jun 5, 2017 at 5:20
5
\$\begingroup\$

Haskell, 193 192 bytes

f"NSW"="New South Wales"
f"QLD"="Queensland"
f"ACT"=a++"n Capital"++t
f"VIC"="Victoria"
f"TAS"="Tasmania"
f"SA"="South "++a
f"NT"="Northern"++t
f"WA"="Western "++a
a="Australia"
t=" Territory"

Somewhat naive solution, but I wanted to go for a fairly simple challange for my first. I'm not even sure if better is possible in Haskell.

Saved a byte by moving the space into t

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$
    – L3viathan
    Commented Jun 6, 2017 at 16:50
3
\$\begingroup\$

Mathematica, 177 bytes

Obvious solution: (199 bytes)

<|"ACT"->"Australian Capital Territory","NSW"->"New South Wales","NT"->"Northern Territory","QLD"->"Queensland","SA"->"South Australia","TAS"->"Tasmania","VIC"->"Victoria","WA"->"Western Australia"|>

Improved solution: (182 bytes, only runnable on my Wolfram Mathematica 10.2 computer)

{,"Northern Territory","Queensland",,,,,"Australian Capital Territory","New South Wales",,,"Tasmania",,,"Western Australia","Victoria",,,,,,,,,,,,"South Australia"}[[Hash@#~Mod~29]]&

Best solution:

{,"Northern Territory","Queensland",a="Australia",,,,a<>"n Capital Territory","New South Wales",,,"Tasmania",,,"Western "<>a,"Victoria",,,,,,,,,,,,"South "<>a}[[Hash@#~Mod~29]]&

Unfortunately repeat " Territory" can save only 0 bytes.

It seems that the default Hash function of Mathematica change over versions. But adding a method will make code longer. Using the default Hash function of Mathematica sandbox now can make code shorter by about 9 bytes.

Mathematica (sandbox - now - version 11), 168 bytes

{a="Australia","Western "<>a,,,,"South "<>a,,a<>"n Capital Territory",,,,"Tasmania","Northern Territory",,,,"New South Wales","Victoria","Queensland"}[[Hash@#~Mod~20]]&
\$\endgroup\$
3
  • \$\begingroup\$ Damn, only 21 bytes better than trivial answer. \$\endgroup\$ Commented Jun 5, 2017 at 5:37
  • 5
    \$\begingroup\$ Wait, so Mathematica has no builtin for this? \$\endgroup\$
    – Neil
    Commented Jun 5, 2017 at 9:18
  • \$\begingroup\$ @Neil I should have thought about that first. \$\endgroup\$
    – user202729
    Commented Jun 5, 2017 at 11:08
2
\$\begingroup\$

Python, 181 bytes

1 byte saved thanks to @Jonathan Allan

lambda a,s='Australia',t=' Territory':[s+'n Capital'+t,'New South Wales','Northern'+t,'Queensland','South '+s,'Tasmania','Victoria','Western '+s]['ACNSNTQLSATAVIWA'.index(a[:2])//2]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ t=' Territory' should save a byte I think. \$\endgroup\$ Commented Jun 5, 2017 at 5:14
2
\$\begingroup\$

Sed, 167 157 bytes

s/T$/ Territory/
s/N /Northern /
s/AS/asmania/
s/IC/ictoria/
s/LD/ueensland/
s/W$/Wales/
s/A/Australia/
s/C/n Capital/
s/NS/New S/
s/^W/Western /
s/S/South /
\$\endgroup\$
1
  • \$\begingroup\$ I did originally defer ern , but that cost more than it saved for just two usages. \$\endgroup\$ Commented Jun 5, 2017 at 17:36
1
\$\begingroup\$

PHP, 148 143 bytes

<?=strtr(["0n Capital 3",Victoria,Queensland,West20,10,Tasmania,North23,"New 1 Wales"][md5(hj.$argn)%8],[Australia,"South ","ern ",Territory]);

Saved 5 bytes thanks to Jörg Hülsermann.

\$\endgroup\$
0
0
\$\begingroup\$

C#, 289 bytes


Data

  • Input String s The abbreviation of the state.
  • Output String The expanded state name

Golfed

(string s)=>{return new System.Collections.Generic.Dictionary<string,string>(){{"ACT","Australian Capital Territory"},{"NSW","New South Wales"},{"NT","Northern Territory"},{"QLD","Queensland"},{"SA","South Australia"},{"TAS","Tasmania"},{"VIC","Victoria"},{"WA","Western Australia"}}[s];};

Ungolfed

( string s ) => {
    return new System.Collections.Generic.Dictionary<string, string>() {
        { "ACT", "Australian Capital Territory" },
        { "NSW", "New South Wales" },
        { "QLD", "Queensland" },
        { "TAS", "Tasmania" },
        { "VIC", "Victoria" },
        { "NT", "Northern Territory" },
        { "SA", "South Australia" },
        { "WA", "Western Australia" }
    }[ s ];
};

Ungolfed readable

// Takes a state name abbreviated 
( string s ) => {
    
    // Creates a dictionary with the state name abbreviated and the full state name and returns the one that match
    return new System.Collections.Generic.Dictionary<string, string>() {
        { "ACT", "Australian Capital Territory" },
        { "NSW", "New South Wales" },
        { "QLD", "Queensland" },
        { "TAS", "Tasmania" },
        { "VIC", "Victoria" },
        { "NT", "Northern Territory" },
        { "SA", "South Australia" },
        { "WA", "Western Australia" }
    }[ s ];
};

Full code

using System;
using System.Collections.Generic;

namespace Namespace {
   class Program {
      static void Main( String[] args ) {
         Func<String, String> f = ( string s ) => {
             return new System.Collections.Generic.Dictionary<string, string>() {
                 { "ACT", "Australian Capital Territory" },
                 { "NSW", "New South Wales" },
                 { "QLD", "Queensland" },
                 { "TAS", "Tasmania" },
                 { "VIC", "Victoria" },
                 { "NT", "Northern Territory" },
                 { "SA", "South Australia" },
                 { "WA", "Western Australia" }
             }[ s ];
         };
         
         List<String>
             testCases = new List<String>() {
                 "ACT",
                 "NSW",
                 "QLD",
                 "TAS",
                 "VIC",
                 "NT",
                 "SA",
                 "WA",
             };
         
         foreach( String testCase in testCases ) {
             Console.WriteLine( $"Input: {testCase}\nOutput: {f( testCase )}\n");
         }

         Console.ReadLine();
      }
   }
}

Releases

  • v1.0 - 289 bytes - Initial solution.

Notes

  • None
\$\endgroup\$
1
  • \$\begingroup\$ (string s) can just be s, You can use an implicit return: s=>new System.... and you can remove the trailing semicolon. \$\endgroup\$ Commented Jun 5, 2017 at 14:23
0
\$\begingroup\$

Rexx (Regina), 148 147 bytes

a=Australia
t=Territory
s=South
ac=a'n' Capital t
ns=New s Wales
nt=Northern t
qs=Queensland
sa=s a
ta=Tasmania
vi=Victoria
wa=Western a
arg x 3
say value(x)

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Seems to output in uppercase? \$\endgroup\$ Commented Jun 5, 2017 at 23:08
0
\$\begingroup\$

JavaScript (ES6), 167 bytes

s=>({A:`${A='Australia'}n Capital ${T='Territory'}`,N:s[2]?'New South Wales':'Northern '+T,T:'Tasmania',Q:'Queensland',S:'South '+A,V:'Victoria',W:'Western '+A}[s[0]])

f=
s=>({A:`${A='Australia'}n Capital ${T='Territory'}`,N:s[2]?'New South Wales':'Northern '+T,T:'Tasmania',Q:'Queensland',S:'South '+A,V:'Victoria',W:'Western '+A}[s[0]])

console.log('ACT NSW NT QLD SA TAS VIC WA'.split(' ').map(f))

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