扩大澳大利亚州


9

澳大利亚有8个州和地区,每个州和地区的字母缩写为2或3:

  • ACT:澳大利亚首都领地
  • 新南威尔士州:新南威尔士州
  • NT:北领地
  • 昆士兰州:昆士兰州
  • SA:南澳大利亚
  • 塔斯马尼亚州(TAS):塔斯马尼亚州
  • VIC:维多利亚
  • 西澳大利亚州:西澳大利亚州

您的任务是编写一个函数/程序,该函数/程序必须使用有效的澳大利亚州大写字母缩写,并返回正确的全名(如上所述,区分大小写)。

除单个尾随换行符外,不允许有多余的空格,这是该语言中每个程序不可避免的副作用。

例如:

f("TAS") => "Tasmania"

标准 规则(以字节为单位的最短代码为准!),并且存在漏洞。


6
我认为我们现在需要创建[ state-abbreviation ]标签。
Cyoce

那“新南(远)威尔士”呢?(对于那些没有意识到的人,它是“ NSFW”)
NoOneIsHere17年

3
当我在寻找解决方案时,新南威尔士州正在扩展到我非常喜欢的“ New South Western麦芽啤酒” :)
Steve Bennett

奇怪的。我创建了标签并添加了标签,但是有人再次将其删除。
史蒂夫·本内特

状态缩写可能太具体了。也许使标签有关缩写/缩写词?
Sparr

Answers:


9

视网膜135121字节

@Neil通过巧妙的空间插入策略打高尔夫球了13个字节,这启发了再打一个字节

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

在线尝试!

注意第四行和最后一行的尾随空格。


如果先替换ASasmania,则可以用替换所有剩余AAustralia
尼尔

您还可以通过插入空格来发挥创意,如下所示:在线尝试!
尼尔

(但是,\B[A-Z]那样可以为您节省两个字节。)
尼尔(Neil

@Neil感谢您的提示,我使用了您的建议再打一个字节。
user41805

5

果冻,56 字节

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

用于获取和返回字符列表的单子链接。

在“ 在线试用”中查看所有案例

注意:重用单词“ Australia”和“ Territory”无助于减少字节数。

怎么样?

将八个状态缩写的序数的总和取模15得出8个唯一值。取那些模9的结果可保持唯一性。

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)

遗憾的是,重用澳大利亚和领地无济于事:/
史蒂夫·本内特

5

Haskell,193192字节

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"

有点天真的解决方案,但是我想第一次尝试一个相当简单的挑战。我什至不确定Haskell是否可以更好。

通过将空间移到t中节省了一个字节


1
欢迎来到PPCG!
L3viathan

3

Mathematica,177个字节

明显的解决方案:(199字节)

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

改进的解决方案:(182字节,只能在我的Wolfram Mathematica 10.2计算机上运行)

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

最佳解决方案:

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

不幸的是,重复" Territory"只能保存0个字节。

似乎Mathematica的默认哈希函数会在版本之间进行更改。但是添加方法会使代码更长。现在,使用Mathematica沙箱的默认哈希函数可以使代码缩短约9个字节。

Mathematica(沙盒-现在-版本11),168字节

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

该死的,只比平凡的答案好21个字节。
史蒂夫·本内特

5
等等,因此Mathematica没有内置功能吗?
尼尔

@Neil我应该首先考虑一下。
user202729

2

Python,181字节

@Jonathan Allan节省了1个字节

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]

在线尝试!


t=' Territory'我应该节省一个字节。
乔纳森·艾伦,

2

桑达,167个 157字节

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 /

我最初确实推迟了ern ,但是这比仅两次使用节省的成本要多。
Toby Speight

1

PHP,148143字节

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

由于JörgHülsermann节省了5个字节。


0

C#,289字节


数据

  • 输入 String s状态的缩写。
  • 输出 String扩展状态名称

打高尔夫球

(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];};

不打高尔夫球

( 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 ];
};

非高尔夫可读

// 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 ];
};

完整代码

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();
      }
   }
}

发布

  • 1.0 - 289 bytes-初始溶液。

笔记

  • 没有

(string s)可以是s,您可以使用隐式返回:s=>new System....并且可以删除结尾的分号。
TheLethalCoder


0

JavaScript(ES6),167字节

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]])

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.