将列表缩小为最终数字


9

输入项

数字或数字字符串的列表(数组),如果使用起来更容易。您可以假设列表中始终至少有两个元素,并且每个元素都是自然数(大于零的整数)。

产出

单个数字或数字字符串。

问题

这个想法是通过删除列表的当前阶段中最大数字的最后一位来减少数字的列表,最后以一个数字结尾(即使有多个实例,也应只返回一个数字)

[123,343,121,76,465,786] -- The last digit in 786 is dropped, so it becomes 78
[123,343,121,76,465,78]  -- New largest number is 465, so the 5 is dropped, making it 46
[123,343,121,76,46,78]   -- Repeat until left with one number
[123,34,121,76,46,78]
[12,34,121,76,46,78]
[12,34,12,76,46,78]
[12,34,12,76,46,7]
[12,34,12,7,46,7]
[12,34,12,7,4,7]
[12,3,12,7,4,7]
[1,3,1,7,4,7]            -- If there are multiple max numbers, you **must** remove the last digit from all of them
[1,3,1,4]
[1,3,1]
[1,1]                    -- You have your answer when there is one number, or multiple numbers that are equal
1                        -- Result

漏洞

适用标准漏洞

其他限制

您的程序必须适用于随机数的任何列表(当然是由于一定原因)

测试用例

[24,72,4]
[24,7,4]
[2,7,4]
[2,4]
[2]
2

[14, 7]
[1, 7]
[1]
1

[1278,232,98273,2334]
[1278,232,9827,2334]
[1278,232,982,2334]
[1278,232,982,233]
[127,232,982,233]
[127,232,98,233]
[127,232,98,23]
[127,23,98,23]
[12,23,98,23]
[12,23,9,23]
[12,2,9,2]
[1,2,9,2]
[1,2,2]
[1]
1

计分

这是 ,因此每种语言的最短答案都会获胜!


1
如果我错过了任何事情,请告诉我。第一个问题。
亨利

14
说不上来,如果是来不及改变,但问题可能会更好,如果我们必须输出的每个阶段。我认为否则答案将非常简单。
DLosc

8
既然没有人提到过,这就是经常在沙箱中发现的东西。
詹姆斯

1
您可以添加答案不是列表中第一个条目的第一位的测试用例吗?
JAD

5
我读这个问题的答案的方式[12, 123, 124]12,这使每个张贴的答案都是错误的
与Orjan约翰森

Answers:






3

Japt8 6 5字节

-1字节感谢@Shaggy

n g g

将输入作为数字字符串数组。在线尝试!

说明

        // implicit input: array of strings
n       // sort the array
  g     // get the first element
    g   // get the first character
        // implicit output

5个字节:对数组进行排序,获取第一个元素,获取第一个字符。
毛茸茸的

@Shaggy Oh duh,我完全把这个复杂化了。谢谢!
贾斯汀·马里纳

没问题:) n v g也可以使用5个字节。欢迎来到Japt。
毛茸茸的



2

V11,5字节

ÚxV}p

在线尝试!

我正在这个waaay复杂得多,它实际上是。该答案仅按ASCII值对每一行进行排序,然后返回第一个字符。由于这是一种无聊或无聊的答案,因此以下是一个更有趣的答案,实际上实现了最初描述的算法:

V,11个字节

òún
/äîä
Lx

在线尝试!


提出问题时我也是如此。您最初的答案是我最期待的样子。笨蛋
亨利

2

果冻 3  2 字节

ṂḢ

一个完整的程序,可获取字符(字符串)列表并打印结果。

在线尝试!

怎么样?

我们只需要返回最小的前导数字...

ṂḢ - Main link: list of lists of characters
Ṃ  - minimum (lexicographical ordering ensures this will start with the minimal digit)
 Ḣ - head (get that first digit character)

没问题,它发生了。
乔纳森·艾伦

2

JavaScript(ES6),17个字节

将输入作为字符串数组。

a=>a.sort()[0][0]

尝试一下

输入逗号分隔的数字列表。

o.innerText=(f=
a=>a.sort()[0][0]
)((i.value="1278,232,98273,2334").split`,`);oninput=_=>o.innerText=f(i.value.split`,`)
<input id=i><pre id=o>


1

,,, 3 个字节

⫰1⊣

说明

⫰1⊣

⫰    pop the whole stack and push the minimum element
 1   push 1
  ⊣  pop the minimum and 1 and push the first character of it

1

Braingolf,17个字节

VVR{Mvd<M&$_R}vvx

在线尝试!

说明

VVR{Mvd<M&$_R}vvx  Implicit input from commandline args
VVR                Create stack2 and stack3, return to stack1
   {.........}     Foreach item in stack..
    M              ..Move item to next stack
     v             ..Switch to next stack
      d            ..Split item into digits
       <M          ..Move first item to next stack
         &$_       ..Clear stack
            R      ..Return to stack1
              vv   Switch to stack3
                x  Reduce to lowest value
                   Implicit output of last item on stack

换句话说,它构造一个仅由每个项目的第一位组成的堆栈,然后输出最低位。

这项挑战为我提供了一些将内置函数添加到Braingolf的有用想法,现在,由于添加了“特殊” foreach循环,Braingolf可以用5个字节来做到这一点:

Braingolf,5个字节[非竞争]

(d<)x

说明

(d<)x  Implicit input from commandline args
(..)   Special foreach loop, iterates over the stack, moving each item to a special
       Sandboxed stack environment, and prepends the last item of the sandboxed
       stack to the real stack at the end of each iteration
 d<    Split into digits, move first digit to end of stack
    x  Reduce to lowest value
       Implicit output of last item on stack

在线尝试!

我通常不反对仅仅为了完成一个挑战而添加内建函数,但是我可以看到新的(...)foreach循环有很多用途,因此我并不真正考虑仅为此挑战添加功能。


失败[12,23,12]。预期的输出是2,您返回了1
奥利维尔·格雷戈尔

@OlivierGrégoire的预期输出是1[12,23,12] > [12,2,12] > [1,2,1] > [1,1]
Skidsdev



0

,5字节

将输入数字列表作为命令行参数。

@@SSg

在线尝试!

交替:

MN@Zg

在线尝试!

说明

在这两个程序中,g都是命令行参数列表。

@@SSg

SS使用字符串比较进行排序,因此不管数字大小如何,都将前几位数最小的数字放在第一位。一元@给出列表或标量的第一个元素。我们应用两次以得到排序后第一个数字的第一个数字。

    g  [24 72 491]
  SS   [24 491 72]
 @     24
@      2

交替:

MN@Zg

Z是拉链 它的一元版本可用于转置列表。转置列表的第一个元素是所有数字的第一个数字的列表。@获取数字列表;MN采取最低要求。

    g  [24 72 491]
   Z   [[2 7 4] [4 2 9]]
  @    [2 7 4]
MN     2

0

PHP,27字节

<?=substr(max($_GET),0,-1);

(哇,完全误解了这个问题。这不起作用。稍后将进行编辑。)


0

Pyth9 7字节

hSmsh`d

在线尝试!

说明

这基本上返回最小的前导数字。

       Q    # Implicit input
  msh`d     # For each number in Q, convert to string, take the first character, convert to integer
hS          # Return the minimum

0

Python 3,33个字节

lambda l:min(str(x)[0]for x in l)

在线尝试!

@DJMcMayhem和@totallyhuman有更好的解决方案,但我的假设是数字输入而不是字符串输入。


0

Pyth,3个字节

hhS

输入是数字的字符串表示形式的列表。

在线试用

说明:

hhS
    # Q=input
  S # Sort Q
 h  # First Element of sorted list
h   # First element of string
    # Implicitly print result
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.