零的权重


21

给定一个有序的数字列表(可能带有前导零),垂直排列数字,然后让所有零完全下降到底部,所有突出部分都下降到最底部的开放插槽。输出结果整数,除去前导零。

工作实例

假设我们得到以下输入:

['0000312941295', '239124000124581598', '32852353800451258', '10235923505185190', '1491359102149']

首先,我们将其垂直排列:

0000312941295
239124000124581598
32852353800451258
10235923505185190
1491359102149

然后,逐列地将零“滴过”其他数字,使它们停留在底部并“推”其他数字。这将导致前几个步骤如下:

2000312941295
339124000124581598
12852353800451258
10235923505185190
0491359102149
^

2300312941295
329124000124581598
14852353800451258
10235923505185190
0091359102149
 ^

2390312941295
328124000124581598
14252353800451258
10935923505185190
0001359102149
  ^

...

2391312941295
328524538124581598
14232323525451258
10915991001185190
0000350000049
                ^

接下来,放下所有悬垂物,就好像重力将它们像沙子一样拉下来。

2391312941295
3285245381245 1598
14232323525458258
10915991001181190
00003500000495
             ^

2391312941295
3285245381245  598
14232323525458158
10915991001181290
000035000004951
              ^

...

2391312941295
3285245381245
14232323525458159
10915991001181258
000035000004951908
                 ^

最后,输出这些数字,删除前导零。对于我们的工作示例,输出:

[2391312941295, 3285245381245, 14232323525458159, 10915991001181258, 35000004951908]

再举一个例子,假设输入[1234000,12345678,1234,12340608,12341234]

1234000
12345678
1234
12340608
12341234

删除零:

1234  
12345678
1234163
12340208
12340004

删除其余的突出数字:

1234  
1234567
12341638
12340208
12340004

输出为[1234, 1234567, 12341638, 12340208, 12340004]

规则

  • 输入中可能包含前导零。输出中不得包含前导零。
  • 如果适用,您可以假定输入/输出将适合您语言的本机Integer类型。
  • 输入和输出可以通过任何方便的方法给出。
  • 完整的程序或功能都是可以接受的。如果是函数,则可以返回输出而不是打印输出。
  • 禁止出现标准漏洞
  • 这是因此所有常用的高尔夫规则都适用,并且最短的代码(以字节为单位)获胜。

2
我们可以假设输出数字不会超过我们语言的精度吗?(JS 1423232352545815914232323525458160
舍入

@ETHproductions我认为这是PPCG的默认设置。
暴民埃里克

and all overhangs drop to the bottom-most open slot是解决我的挑战的好方法:)。
魔术章鱼缸

1
@PeterCordes是的,输入内容可能包含前导零,因此它应该能够处理。我想对于大多数语言来说,这意味着将输入作为字符串。
AdmBorkBork

1
@PeterCordes输入和输出不一定必须以10为底(在允许的默认I / O方法中),只要使用其他基础不会使任务变得琐碎(这是一个标准漏洞)。其次,我想我没有指定前导零必须去除彻底,尽管这是意图。我将裁定不允许使用空格替换零,因为输出与output . 1234截然不同1234
AdmBorkBork '18

Answers:


10

果冻,8字节

Z¬Þ€UZṚḌ

在线尝试!

怎么运行的

Z¬Þ€UZṚḌ  Main link. Argument: M (2D array / matrix)

Z         Zip; transpose M by reading columns as rows.
 ¬Þ€      Sort each row of the transpose by logical NOT, pushing 0's to the end.
    U     Upend; reverse all sorted rows of the transpose.
     Z    Zip again, restoring rows from columns. Overhangs rise to the top.
      Ṛ   Reverse the order of the rows.
       Ḍ  Decimal; convert the rows from base 10 to integer.

4
什么?当我阅读问题时,我在想:“最后,没有人能为此拉出少于20个字节”。疯狂
Cruncher '18

1
Sort each row of the transpose by logical NOT, pushing 0's to the end.是否保证这是稳定的排序?
Cruncher '18

1
是的,Jelly使用Python的sorted,可以保证稳定。
丹尼斯

我有点像这个可交换的版本:ṚZẸÞ€ZṚḌ:)
乔纳森·艾伦


4

外壳,12个字节

md↔TmoÖ±miT↔

在线尝试!

说明

md↔Tm(Ö±mi)T↔  -- input as list of strings, example: ["103","32","258"]
            ↔  -- reverse: ["258","32","103"]
           T   -- transpose: ["231","520","83"]
    m(    )    -- with each line do (example with "520")
        mi     -- | convert each char to digit: [5,2,0]
      Ö±       -- | sort by signum (stable): [0,5,2]
               -- : [[2,3,1],[0,5,2],[8,3]]
   T           -- transpose: [[2,0,8],[3,5,3],[1,2]]
  ↔            -- reverse: [[1,2],[3,5,3],[2,0,8]]%
md             -- convert each to integer: [12,353,208]

4

Python 2,118字节

lambda l:[int(''.join(z))for z in zip(*map(lambda*a:['']*a.count(None)+[e for e in a if'0'<e]+['0']*a.count('0'),*l))]

在线尝试!

非高尔夫版本

def f(list_):
 max_len = max(len(x) for x in list_)
 transposed_list = zip(*[list(row)+(max_len-len(row))*[None] for row in list_])
 weighted_list = [['']*column.count(None)+[cell for cell in column if cell != '0' and cell != None]+['0']*column.count('0') for column in transposed_list]
 return [int(''.join(row)) for row in zip(*weighted_list)]

前两行等效于map(lambda*a...),如果一个列表比另一个列表短,则用map填充默认值None
e>'0'等价于cell != '0' and cell != None,因为如果它是数字(1〜9),它将具有更高的代码点,并且(any)字符串高于None


愿意发布此版本的非公开版本吗?
彼得·科德斯

@PeterCordes添加了取消版本的版本和一些晦涩之处的简短解释


2

视网膜0.8.295 92个字节

m(+`^((.)*)(.+)(¶(?<-2>.)*)(?(2)_)$
$1$4$3
+`^((.)*)0(.*¶(?>(?<-2>.)*))([^0])
$1$4${3}0
^0+

在线尝试!说明:第一阶段丢弃突出的数字,因为这使得第二阶段更容易(编辑:对于保存3个字节更容易)使零下降。然后,第三阶段删除前导零。


2

红宝石,104字节

->a{a.map{|x|x.ljust(99).chars}.transpose.map{|x|x.sort_by{|x|x<?1?x:?!}}.transpose.map{|x|x.join.to_i}}

在线尝试!

说明

->a{
  a.map{|x|x.ljust(99).chars}  # Fill spaces to make this rectangular
    .transpose.map{|x|
       x.sort_by{|x|x<?1?x:?!} # Treat all chars but " 1" as ! (see ascii table)
    }.transpose.map{|x|x.join.to_i} # Convert back to numbers
                       # note: if no leading 0s, eval x*'' , doesn't work here
}

1

APL(Dyalog Unicode),26 字节SBCS

匿名默认前缀函数,以字符矩阵为参数并返回数字列表。

⍎⍤1∘⍉' 0'(∩∘⊃,~,∩∘⊃∘⌽)⍤1⍨⍉

在线尝试!

 转置输入(因为我们需要在列上工作)

' 0'(... )⍤1⍨ 与应用以下隐性函数到每个行(张量秩1的子阵列)' 0'作为参数(交换参数):

 行和的交叉点
 和
 第一' 0'
 (即row∩' ',来自每个行中的所有空格)

, 其次是…

~ 设定差异
 (即row~' 0';行但没有空格和零)

, 其次是…

 行和的交叉点
 和
 所述第一
 的
 反转' 0'
 (即row∩'0',所有从各行的零)

⍎⍤1 评估每一行(张量秩1的子阵列)
 的
 的是,转置(即,每个柱;现在修改的输入行)


带括号的位很聪明。我花了一段时间了解那里的意图,尽管我知道个人的花魁会做什么。有一次,我的理解是,它很容易被击败:⍎⍤1∘⍉{⍵[⍋3|2×' 0'⍳⍵]}⍤1∘⍉⎕io←0)可能golfable进一步,例如,我没有探索二元
NGN

实际上,以上内容作为完整的程序(而不是火车)较短:⍎⍤1⍉{⍵[⍋3|2×' 0'⍳⍵]}⍤1⍉⎕
ngn

@ngn这样的方法值得一提。预处理和后处理是显而易见的。
亚当

1

Perl 5,-p0 77字节

旧样式计数:79个字节(+2用于p0

将输入作为STDIN上的行而不使用最终换行符(否则,所有内容都将被视为突出显示,并且当输入字符串崩溃时,最终换行符将升至顶部)。例如:

0000312941295
239124000124581598
32852353800451258
10235923505185190
1491359102149

使突出部分下降并将其0下降到一个正则表达式中有点棘手

#!/usr/bin/perl -p0
s/^.{@{+}}\K((0)|.+$)(.*
.{@{+}})((?(2)[1-9]|$))/$4$3$1/m while/|/g;s/^0+//mg

在线尝试!


0

红宝石,203字节

->a{t=->b{b[0].zip(*b[1..-1])}
m=a.map{|s|s.size}.max
a.map!{|s|(s+" "*m)[0...m].chars}
a=t[a].map{|c|c.size.times{|i|" 0"[c[i]]&&(c.slice!(i)==?0?c.push(?0):c.unshift(" "))}
c}
t[a].map{|r|r.join.to_i}}

在线尝试!

一个lambda接受一个字符串数组并返回一个int数组。我觉得我在想什么。这感觉非常好:/

->a{
  t = -> b { b[0].zip(*b[1..-1]) }     # t is a lambda that transposes 2D arrays
  m = a.map{ |s| s.size }.max          # m is the maximum input string length
  a.map!{ |s| (s+" "*m)[0...m].chars } # Right-pad every string to the same length
  a = t[a].map{ |c|                    # Transpose a, and for every row (column)
    c.size.times{ |i|                  # For every character in the column
      " 0"[c[i]] && (                  # If the character is 0 or space
        c.slice!(i) == ?0 ?            # Remove it from the column, and if it was 0,
          c.push(?0) :                 # Push a 0 to the end (bottom) of the column, else
          c.unshift(" ")               # Add a space to the top of the column
      )
    }
    c
  }
  t[a].map{ |r| r.join.to_i }          # Transpose a back, and parse each row to int
}


@Unihedron Wow,您击败了我50%。您肯定有我的+1。
benj2240 '18

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.