高尔夫我的Ada阵列


10

背景

Ada是一种编程语言,其简洁性并不十分出名。

但是,其数组文字语法在理论上可以允许相当简洁的数组规范。这是数组文字 语法的简单EBNF描述(可传递到bottlecaps.de

array ::= positional_array | named_array
positional_array ::= expression ',' expression (',' expression)*
                   | expression (',' expression)* ',' 'others' '=>' expression
named_array ::= component_association (',' component_association)*
component_association ::= discrete_choice_list '=>' expression
discrete_choice_list ::= discrete_choice ('|' discrete_choice)*
discrete_choice ::= expression ('..' expression)? | 'others'

为了简单起见,我们将自己限制为一维整数数组。这意味着我们将仅对表达式值使用整数。也许在将来的挑战中,我们可以尝试更高级的方法(例如声明变量和多维数组)。不会有高尔夫整数文字

为了清楚起见,以下是Ada数组文字和python式等效表示形式的一些示例:

(1, 2, 3) = [1, 2, 3]
(1, others => 2) = [1, 2, 2, ..., 2]
(others => 1) = [1, 1, ..., 1]
(1 => 1, 2 => 3) = [1, 3]
(1|2 => 1, 3 => 2) = [1, 1, 2]
(1 => 1, 3 => 2, others => 3) = [1, 3, 2, 3, 3, ..., 3]

挑战

这项挑战的目标是为给定的输入数组输出最短的字节数Ada数组文字。请注意,Ada数组可以从所需的任何索引开始,因此,只要每个值都是连续的,就可以选择希望起始索引为什么。在此示例中,我选择从1开始,这是Ada惯用的,但是您可以选择从任何其他整数开始。

输入值

您的输入将包含整数列表,以任何方便的形式。

输出量

您的输出将是一个字符串,该字符串表示表示输入整数列表的最短有效Ada数组文字。您可以在该数组上使用任何希望的起始索引,但是您的选择(无论是什么)都必须在答案中指定(起始索引也可以是动态的)。

如示例中所示,整数将表示为带符号的十进制数。此挑战不包括整数打高尔夫球。

例子

这里有些例子:

Simple: [1, 2, 3] -> (1,2,3)
Range: [1, 1, 1, 1, 1, 1, 1,] -> (1..7=>1)
Others: [1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1] -> (6=>2,others=>1)
Multiple Ranges: [1,1,1,1,1,2,2,2,2,2,1,1,1,1,1,2,2,2,2,2,1,1,1,1,1] -> (6..10|16..20=>2,others=>1)
Tiny Ranges: [1,1,2,2,1,1,1,1,1] -> (3|4=>2,others=>1)
Far Range: [[1]*5, [2]*100, [3]*5] -> (1..5=>1,6..105=>2,others=>3)
Alternation: [1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2] -> (1|3|5|7|9|11|13|15|17=>1,others=>2)
Big Number: [1234567890,1,1234567890] -> (2=>1,1|3=>1234567890)
Big-ish Number: [1234567,1,1234567] -> (1234567,1,1234567)
Solo: [-1] -> (1=>-1)
Huge Input: [[0],[1]*1000000000] -> (0,others=>1)
Positional Others: [1, 2, 3, 3, 3, 3, 3, 3] -> (1,2,others=>3)
Range and Choice, no Others: [1,1,1,12,12,3,3,3,3,3,3,3,3,3,3,4] -> (1..3=>1,4|5=>12,6..15=>3,16=>4)

最低要求

  • 支持至少100个数字和长度至少为256个数字的输入。

  • 为所有此类输入产生正确的结果

    • 包括在末尾加上“其他”
    • 包括为单个项目数组放置索引
  • 在不到一分钟的时间内终止(最好在TIO上)上述每个输入。

以字节为单位的最短解决方案获胜!

参考实施

在线尝试!

此实现使用输入作为其数组,每个字符为一个数字。大写字母是大数值的特殊常量。程序参数是要使用的“开始索引”。

TIO链接中的“代码”部分是该问题的正确解决方案,而“页眉”和“页脚”实现了测试结构。


3
是否存在“ Far Range”情况只是为了表明如果我们选择就可以采用该格式输入,还是要突出显示我们必须能够处理该输入格式以及常规数组?另外,最后一个测试用例不应该只是输出(-1)吗?
毛茸茸的

3
“ Far Range”情况只是为了节省空间而编写的,实际输入将是由110个整数组成的平面数组,但输出是正确的。其目的是演示“其他”关键字应在具有较长表示的较短范围内进行的情况。(106..110=>3,others=>2可能会更长)最后一种情况需要有一个索引,因为语法不允许使用单元素位置数组(positional_array ::= expression ',' expression (',' expression)*
LambdaBeta

1
1个(1=>1,others=>1)(1..100000000=>1)

2
您能否确认这(1|3=>1234567,2=>1)是另一个有效输出[1234567,1,1234567]
阿诺尔德

1
我们可以使用Ada作为我们选择的语言吗?
本杰明·厄克特

Answers:


5

的JavaScript(ES6), 307个  304字节

@KevinCruijssen节省了2个字节

这令人尴尬的长...

a=>[b=([...a,m=''].map(o=(v,i)=>(i?p==v?!++n:m=o[(o[p]=[o[p]&&o[p]+'|']+(n?i-n+(n>1?'..':'|')+i:i))[m.length]?(x=i-n,j=p):j]:1)&&(p=v,M=n=0)),Object.keys(o).map(k=>j-k|!m[6]?o[k]+'=>'+k:O,O='others=>'+j).sort()),1/a[1]?[...a]:b,j-a.pop()?b:a.slice(0,x-1)+[,O]].map(a=>M=M[(s=`(${a})`).length]||!M?s:M)&&M

在线尝试!


305个字节(-2)通过为重复的变量创建一个变量'others=>'
凯文·克鲁伊森

@KevinCruijssen谢谢!(注意:在您的版本中,t先使用它,然后再定义它;之所以不会崩溃,是因为前两个测试用例根本不使用它;不过,可以很容易地免费进行修复,尽管如此。)
Arnauld

喔好吧。我并没有真正弄清您的答案以了解在哪里使用了什么。我只是注意到您有'others'两次,并尝试为其创建一个变量而不更改输出。;)谢谢您的解释,并通过使用了不错的逗号打法[,O]。:)
凯文·克鲁伊森

2

05AB1E136个 134 132 字节

"',ý'(ì')«ˆ"©.V"θ…ˆ†=>쪮.V"Uγ¨D€gPi˜IX.V}\ÙεQƶ0KDāαγ€g£}D2Fε¾iεнyg≠iyθyg<i'|ë„..}ý}}ë˜}'|ý„=>«Iyнн<è«}Ю.VgFDN._ć'>¡X.V}\¼}¯éIgi¦}н

编辑:现在修复所有测试用例。

在线尝试验证所有测试用例(“巨大输入”除外,因为它太大了)。

说明:

"',ý'(ì')«ˆ"       # Push this string (function 1), which does:
 ',ý              '#  Join a list by ","
    '(ì           '#  Prepend a "("
       ')«        '#  Append a ")"
          ˆ        #  Pop and add it to the global array
            ©      # Store this string in the register (without popping)
             .V    # And execute it as 05AB1E code on the (implicit) input-list
"θ…ˆ†=>쪮.V"      # Push this string (function 2), which does:
 θ                 #  Pop and push the last element of the list
  …ˆ†=>ì           #  Prepend dictionary string "others=>"
        ª          #  Append that to the list which is at the top of the stack
         ®.V       #  And execute function 1 from the register     
             U     # Pop and store this string in variable `X`
γ                  # Get the chunks of equal elements in the (implicit) input-list
 ¨                 # Remove the last chunk
  D                # Duplicate the list of remaining chunks
   g              # Get the length of each
     Pi     }      # If all chunk-lengths are 1:
       ˜           #  Flatten the list of remaining chunks
        I          #  Push the input-list
         X.V       #  Execute function 2 from variable `X`
             \     # Discard the top of the stack (in case we didn't enter the if-statement)
Ù                  # Uniquify the (implicit) input-list
 ε                 # Map each unique value `y` to:
  Q                #  Check for each value in the (implicit) input-list if it's equal to `y`
                   #  (1 if truthy; 0 if falsey)
   ƶ               #  Multiply each by its 1-based index
    0K             #  Remove all 0s
      D            #  Duplicate it
       ā           #  Push a list [1, length] without popping the list itself
        α          #  Get the absolute difference at the same indices
         γ         #  Split it into chunks of the same values
          g       #  Get the length of each
            £      #  And split the duplicated indices-list into those parts
                   # (this map basically groups 1-based indices per value.
                   #  i.e. input [1,1,2,1,1,2,2,1,1] becomes [[[1,2],[4,5],[8,9]],[[3],[6,7]]])
 }D                # After the map: duplicate the mapped 3D list
   2F              # Loop 2 times:
     ε             #  Map the 3D list of indices to:
      ¾i           #   If the counter_variable is 1:
        ε          #    Map each list `y` in the 2D inner list to:
         н         #     Leave the first value
         ygi      #     And if there is more than one index:
             yθ    #      Push the last value as well
             yg<i  #      If there are exactly two indices:
              '|  '#       Push string "|"
             ë     #      Else (there are more than two indices)
              „..  #       Push string ".."
                 #      And join the first and last value by this string
        }}         #    Close the if-statement and map
      ë            #   Else:
       ˜           #    Flatten the 2D list
      }'|ý        '#   After the if-else: join by "|"
          „=>«     #   Append "=>"
       yнн         #   Get the very first index of this 2D list
          <        #   Decrease it by 1 to make it 0-based
      I    è       #   And index it into the input-list to get its value again
            «      #   Which is also appended after the "=>"
                 #  After the map: triplicate the result
       ®.V         #  Execute function 1 from the register
       g           #  Get the amount of items in the triplicated list
        F          #  Loop that many times:
         D         #   Duplicate the list
          N._      #   Rotate it the index amount of times
          ć        #   Extract the head; pop and push remainder and head
           '>¡    '#   Split this head by ">"
              X.V  #   And then function 2 is executed again from variable `X`
        }\         #  After the loop: discard the list that is still on the stack
          ¼        #  And increase the counter_variable by 1
                 # After looping twice: push the global array
     é             # Sort it by length
      Igi }        # If the input only contained a single item:
         ¦         #  Remove the very first item
           н       # And then only leave the first item
                   # (which is output implicitly as result)

看到这个05AB1E尖矿(部分如何压缩字符串不是字典的一部分吗?理解为什么…ˆ†=>"others=>"

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.