删除每N个N


41

任务

在此挑战中,您的输入是一个非空的正整数列表,以您的语言的本机格式给出。您的输出是相同列表,格式相同,但删除了一些元素。您应删除的每次出现1,第二次出现的第二次出现2,第三次出现的所有第三次3,依此类推。通常,对于每个正整数N,您应从列表中删除N第一个出现的事件。NN

考虑输入清单

[3,2,6,1,1,6,2,3,2,6,6,6,6,1,6,6,3,3,7,2]

首先,我们删除每次出现的1

[3,2,6,    6,2,3,2,6,6,6,6,  6,6,3,3,7,2]

然后每秒出现一次2

[3,2,6,    6,  3,2,6,6,6,6,  6,6,3,3,7  ]

然后每三出现一次3

[3,2,6,    6,  3,2,6,6,6,6,  6,6,  3,7  ]

数字45不会出现在输入中,因此可以跳过它们。接下来,我们删除第六次出现的6

[3,2,6,    6,  3,2,6,6,6,    6,6,  3,7  ]

只会出现一次7,因此也可以跳过。因此正确的输出是

[3,2,6,6,3,2,6,6,6,6,6,3,7]

规则和计分

您可以编写完整的程序或函数。最低字节数获胜,并且不允许出现标准漏洞。

测试用例

[1] -> []
[2] -> [2]
[1,1,1] -> []
[2,2,2] -> [2,2]
[1,1,2,2,2,3,3,3,3] -> [2,2,3,3,3]
[1,2,3,1,2,3,1,2,3,1,2,3] -> [2,3,3,2,3]
[3,2,6,1,1,6,2,3,2,6,6,6,6,1,6,6,3,3,7,2] -> [3,2,6,6,3,2,6,6,6,6,6,3,7]
[5,4,5,4,3,5,4,5,4,5,4,3,5,4,5,3,3,3,4,5,4,5,4,5,4,3,3,3,5,4] -> [5,4,5,4,3,5,4,5,4,3,5,4,5,3,3,4,5,5,4,4,3,3,5,4]
[6,4,5,8,2,9,3,1,8,5,3,5,5,6,3,5,1,2,3,9,3,5,8,7,5,9,1,3,4,8,2,3,4,7,8,5,8,5,3,1] -> [6,4,5,8,2,9,3,8,5,3,5,5,6,3,9,3,5,8,7,5,9,4,8,2,3,4,7,8,5,8,5,3]
[4,4,9,12,9,4,4,4,12,9,12,9,12,4,12,4,4,9,4,9,12,4,12,4,4,12,4,4,9,12,9,12,9,4,9,12,4,9,12,9,12,9,4,9,12,12,4,4,12,4] -> [4,4,9,12,9,4,4,12,9,12,9,12,4,12,4,9,4,9,12,4,12,4,12,4,4,9,12,9,12,4,9,12,9,9,12,9,4,9,12,12,4,4,12]
[2,2,11,11,11,11,6,2,2,6,6,2,2,2,2,6,11,6,11,11,2,11,2,6,6,11,2,11,6,2,6,6,6,6,6,11,2,11,11,6,6,6,11,6,2,6,2,6,6,2,11,2,2,11,11,11,11,2,6,2] -> [2,11,11,11,11,6,2,6,6,2,2,6,11,6,11,11,2,11,6,11,2,11,6,6,6,6,6,2,11,11,6,6,6,11,6,2,6,6,11,2,11,11,11,11,2,6]
[20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102] -> [20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,21,22,100,101,102,20,22,100,101,102,20,21,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,21,22,100,101,102,20,21,22,100,101,102,20,22,100,101,102,20,21,22,100,101,102,20,21,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,21,22,101,102,20,21,22,100,102,20,21,22,100,101,20,21,22,100,101,102,20,21,22,100,101,102,20,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,100,101,102]

排行榜

这是一个堆栈片段,用于按语言生成常规排行榜和获胜者概述。为确保您的答案显示出来,请使用以下Markdown模板以标题开头。

## Language Name, N bytes

N您提交的文件大小在哪里。如果您提高了分数,则可以通过打败旧分数保持标题。例如:

## Ruby, <s>104</s> <s>101</s> 96 bytes

如果要在标题中包含多个数字,请确保实际分数是标题中的最后一个数字:

## Perl, 43 + 2 (-p flag) = 45 bytes

您还可以将语言名称设置为链接,然后该链接将显示在页首横幅代码段中:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

Answers:


15

果冻10 8 字节

=;\S€%af

感谢@ Sp3000打高尔夫球2个字节!

在线尝试!

这个怎么运作

=;\S€%af    Main link. Input: A (list)

 ;\         Compute the cumulative concatenations of the elements of A, i.e., the
            list of A's prefixes.
=           Compare the elements of the nth prefix with the nth element of A.
   S€       Compute the sum of the resulting arrays.
     %      Take the nth sum modulo the nth element of A.
      a     Logical AND; replace the nth remainder with the nth element of A
            if the remainder is non-zero.
        f   Filter; remove the zeroes (which do not occur in A) from the result.

比较步骤如何工作?和是[5,4,3,2,1] [[5],[5,4],[5,4,3],[5,4,3,2],[5,4, 3,2,1]]或[[1],[2,1],[3,2,1],[4,3,2,1],[5,4,3,2,1]]?
quintopia '16

@quintopia Jelly从左到右,所以这是第一个。=比较整数。例如,[3,2,1]=;\比较3用的元件[3]2用的那些[3, 2]1与那些[3, 2, 1],给予[1, [0, 1], [0, 0, 1]]
丹尼斯

啊,我想念它正在将一个列表与一个列表逐个列表进行比较。
quintopia '16

34

awk,10个字节

预期在STDIN上输入,每行一个数字。

++a[$1]%$1

说明

在关联数组中为每个数字保留一个计数器,仅当计数器的模数n不为零时才打印。打印是隐式的。长版:

++a[$1]%$1{print $0}

19

Pyth,18 15 14 10 9个字节

f%/aYTTTQ

我认为这是我编写的第一个包含9个字节的五个连续变量引用的代码。

我希望数组操作解决方案(u.DG%HgxHGH{QQ,14个字节)不会那么长。

f%/aYTTTQ       Implicit: Q=input
                 lambda T:
    Y              Variable: starts as empty list.
   a T             Append T to Y. Mutates Y.
  /   T           Number of elts of Y that equal T.
 %     T         Modulo by T
f       Q       Filter that lambda over Q.

在这里尝试。


9

Python,57个字节

lambda l:[n for i,n in enumerate(l)if l[:i+1].count(n)%n]

8

Perl 6,28个字节

{$_=$;grep {++.{$^n}%$n},@_} # 28 bytes
{
  $_=$;        # put $_ in a clean state
  grep {
    ++.{$^n}   # increment $_{ $n } and declare $n as an argument
    % $n       # check if the count is not divisible by $n
  }, @_        # the input
}

用法:

# give it a lexical name for ease of use
my &code = {...}

sub check ( @a, @b ){
  say try { so all code(@a) »==« @b } // False
}

check [1], []; # True
check [2], [2]; # True
check [1,1,1], []; # True
check [2,2,2], [2,2]; # True
check [1,1,2,2,2,3,3,3,3], [2,2,3,3,3]; # True
check [1,2,3,1,2,3,1,2,3,1,2,3], [2,3,3,2,3]; # True
check [3,2,6,1,1,6,2,3,2,6,6,6,6,1,6,6,3,3,7,2], [3,2,6,6,3,2,6,6,6,6,6,3,7]; # True

仔细检查是否抛出了正确的元素

# have to change it to a pure number
# when checking $_         V
my &code = {$_=$;grep {++.{+$^n}%$n},@_}
# only matters here because we are using
# a subclass of Int but want it to use
# the same key as a normal Int

sub F ( Int $v ) { IntStr.new: $v, "Fail($v)" }
# prove that it passes through unchanged
say [F(2)];
# (Fail(2))

say code [3,2,6,F(1),F(1),6,F(2),3,2,6,6,6,F(6),F(1),6,6,F(3),3,7,F(2)];
# (3 2 6 6 3 2 6 6 6 6 6 3 7)

7

严重的是22 17字节

k╗,;`;;╜o;╗c%`M@░

十六进制转储:

6bbb2c3b603b3bbd6f3bbb6325604d40b0

在线试用

说明:

k╗                                Put empty list in reg0
  ,;                              Two copies of input
    `        `M                   Map over the list
     ;;                           Make 2 extra copies of n
       ╜o                         Load reg0 and push the n onto it
         ;╗                       Put a copy back in reg0
           c                      Count the number of copies of n in the list
            %                     Take the result modulo n
               @░                 Filter the original list with the resulting list

10
这种语言...
Nico

6

JavaScript ES6,34个字节

a=>a.filter(v=>f[v]=-~f[v]%v,f=[])

原来与Brad的Perl算法相同。

编辑:由于@ edc65,节省了2个字节。


好!保存2个字节,删除内部括号a=>a.filter(v=>f[v]=-~f[v]%v,f=[])
edc65 '16

5

Mathematica,40 38 36字节

Select[(f@#=#)&/@#,++f[#]~Mod~#>0&]&

这是一个未命名的函数,接受并返回List。它f在执行时定义了一个命名函数(以跟踪数字),但是需要f事先重置相关定义。

说明

Mathematica中函数(或函数定义)的工作方式非常强大。就像在Haskell中一样,函数不仅可以重载并为某些类型定义,而且还可以为单个值(或实际上为参数的任意模式)定义。但是它比Haskell更为强大,因为a)这些值可以在控制流中定义为副作用,并且b)可以随时重新定义这些值。这意味着函数实际上是功能非常强大的查找表(可以选择计算查找的值,而不仅仅是存储它)。

如果我们从代码中删除golfitude,它将看起来像这样:

g[list_] := (
  Map[
    (f[#] = #) &,
    list
  ];
  Select[
    list,
    Mod[++f[#], #] > 0 &
  ]
)

因此,首先,我们遍历输入并f[x] = xx列表中的所有元素定义。f最终将用于跟踪每个数字在列表中出现的频率。为什么我们不从中算起0?列表上的循环是一个Map。该表达式f[x] = y返回y(除了存储函数定义之外)。因此,通过设置f[x]x,地图将评估为输入列表本身。这可以节省两个字节,因为这时我们并不需要提供不list明确再次Select。从开始x而不是0根本不影响计算,因为我们只对感兴趣Mod[f[x], x]

(通常,我们可以简单地使用诸如f[_] = 0回退定义之类的方法来避免使用Map,但是我们不知道我们的函数是否曾经使用过,否则会留下一些先前定义的值,这会干扰我们的计数。)

然后Select通过仅保留那些作为第二个参数传递的未命名函数的元素来过滤列表True。该函数的第一增量的值f[x](其中x是当前的列表元素),以计数的出现,然后它需要得到的计数模x。我们要丢弃产生此结果的所有元素0


5

CJam,17个字节

Lq~{:X+_Xe=X%},p;

打... J?真的不确定我对这个挑战有什么期望。请注意,这""是CJam对空数组的表示。

在线试用 | 测试套件(最后一种情况对于永久链接来说太长了)

说明

L                     Push empty array (let's call it L)
 q~                   Push input and evaluate

   {         },       Filter the array elements by...
    :X                   Save number to variable X
      +                  Append to L

       _                 Duplicate
        Xe=              Count occurences of X
           X%            Take modulo X

                      The top element is popped to determine whether or not to keep that
                      element, with the updated L kept on the stack for the next iteration

               p      Print the resulting filtered array
                ;     Pop L, which is now equal to the input array

4

JavaScript ES6,55个字节

a=>a.filter((v,i)=>a.filter((w,j)=>j<=i&v==w).length%v)

说明

a=>                            //a -> input array
 a.filter(                     //filter array. only keep elements if inside function returns truthy
      (v,i)=>                  //inside function to decide whether to keep items. v -> item; i -> index
           a.filter((w,j)=>    //get all ocurrences of v that occur before index i
                j<=i&v==w      //(this is done by keeping all items w at index j, if j <= i and v == w
           ).length%v          //get length (count ocurrences), and mod v.
                               //this will only be falsy if the number of ocurrences of v up to this index is divisible by v. (0 -> falsy, positive -> truthy) 
 )                             //so, we don't keep every second 2, every third 3, etc.

3

J,18个字节

#~((0<]|+/@:=){:)\

用法:

   (#~((0<]|+/@:=){:)\) 1 2 3 1 2 3 1 2 3 1 2 3
2 3 3 2 3

非常简单的方法。我们最多计算一个数字的出现次数,并且仅在数字除以计数后才选择数字。

稍后会有更多解释。

在这里在线尝试。


2

PowerShell,56个字节

param($a)$b=,0*($a|sort)[-1];$a|%{if(++$b[$_-1]%$_){$_}}

使用与Rainer P的答案类似的辅助数组技巧,该答案是我独立开发的,但显然得到了FGITW的帮助。

使用将输入作为数组param($a)。然后,$b通过使用逗号运算符和重载的乘法运算符一起,将辅助程序数组创建为零填充数组。这将创建$b等于@(0,0,0...0)$b.length等于最大数量$a
(快速插入我的“展示您的语言” 答案,我将在此进行详细说明)

接下来是我们的输出。我们遍历输入数组的每个元素,$a|%{...}每个循环检查一条if语句。有条件的预增加与当前元素相对应的辅助数组中的值,然后使用模运算符检查它是否是当前元素的倍数。如果它是倍数,则%遗嘱等于0虚假,因此if不会执行。否则,我们输出当前元素。

利用隐式类型转换来节省输出格式。如果一个函数或程序返回多个元素,并且您将结果保存到变量中,那么PowerShell将动态地将该变量创建为数组。例:

PS C:\Tools\Scripts\golfing> $testc = .\remove-every-nth-n.ps1 @(2,2,2)

PS C:\Tools\Scripts\golfing> $testc
2
2

PS C:\Tools\Scripts\golfing> $testc.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------             
True     True     Object[]                                 System.Array

1

R,110 98 99 92字节

function(a){for(i in 1:max(a))for(j in seq_along(b<-which(a==i)))if(j%%i<1)a[b[j]]=0;a[a>0]}

使用测试用例2/3编辑完整的重写修复程序错误编辑2使用@ Alex-A,节省7个字节


1
92个字节:function(a){for(i in 1:max(a))for(j in seq_along(b<-which(a==i)))if(j%%i<1)a[b[j]]=0;a[a>0]}
Alex A.

1

MATL,20字节

tu"t@=f@@h0h)0w(]tg)

这使用该语言/编译器的当前版本(10.2.1)

在线尝试!

说明

tu        % implicitly input array. Duplicate and get unique elements
"         % for each unique element, say "i"
  t       % duplicate
  @=f     % indices of occurrences of i
  @@h0h   % build index representing i-th occurrences (Matlab's i:i:end)
  )       % keep only indices of i-th occurrences
  0w(     % set those entries to 0
]         % end for loop
tg)       % keep nonzeros only. Implicit display

1

R,63个字节

f=function(z){n=seq(z);for(i in n)z[which(z==i)[n*i]]=0;z[z>0]}

1

C#,224个字节

List<int>R(List<int>l,int n=1){l=l.Where(w=>w!=0&&w!=1).ToList();for(int i=0,t=0;i<l.Count;i++){if(l[i]==n&&++t==n){l[i]=0;t=0;}}return !l.Any()||n>l.Max()?l:R(l,++n);}

此代码使用递归。使用using224字节的语句(方法代码本身为160字节)。

在线尝试。

List <int> R(List <int> l, int n = 1)
{
    l = l.Where(w => w > 1).ToList();
    for (int i = 0, t = 0; i < l.Count; i++)
    {
        if (l[i] == n && ++t == n)
        {
            l[i] = 0;
            t = 0;
        }
    }
    return !l.Any() || n > l.Max() ? l : R(l, ++n);
}

通过删除继续语句,您应该可以节省一些字符。像(未经测试的)一样for(int i=0,t=0;i<l.Count;i++)if(l[i]==n&&++t==n)l[i]=t=0;
彼得·泰勒

@ peter-taylor,您说得对,谢谢。此外,还必须添加一些代码来修复错误。
德米特里·斯蒂芬诺夫

如果您已导入,System.Linq!l.Any()短于l.Count<1,短于l.Count==0
彼得·泰勒

@ peter-taylor谢谢,我也替换w != 0 && w !=1w > 1
德米特里·斯蒂芬诺夫

数组也应该很好,并且它们会更短int [] R(int [] l,int n = 1)
糟糕的2016年

0

C#-177字节

void r(List<int> i){for(int c=1,k=1,m=i.Max();k<=m;k++){var n=new List<int>();foreach(var o in i)if(o==k&&c++==k)c = 1;else n.Add(o);i=n;}Console.WriteLine(string.Join(" ",i));}

不打高尔夫球

void r(List<int> i)
{
    for (int c = 1, k = 1, m = i.Max(); k <= m; k++)
    {
        var n = new List<int>();
        foreach (var o in i)
            if (o == k && c++ == k)
                c = 1;
            else
                n.Add(o);
        i = n;
    }
    Console.WriteLine(string.Join(" ", i));
}

4
我相信您将不得不计算using语句,在这种情况下,它的长度为241个字节。
LegionMammal978 '16

0

Mathematica,63个字节

Fold[Delete[#,Position[#,#2][[#2;;;;#2]]~Check~{}]&,#,Union@#]&

高尔夫很有趣!忽略偶尔弹出的消息。



0

TI-BASIC,47个字节

Input X
For(I,1,dim(∟X
∟X(I
If fPart(sum(∟X=Ans),1,I)/Ans
Ans→L₁(1+dim(L₁
End
L₁

这使用了这样的事实:在新的计算器上,该L₁初始化和清除。请注意,尝试在TI-BASIC中显示空白列表会引发错误。


0

APL,16个字符

{⍵/⍨×⍵|+/¨⍵=,\⍵}

用英语:

  • ,\⍵:向量的向量前缀,直到参数的第n个元素
  • +/¨⍵=:每个前缀向量,计算等于第n个元素本身的个数
  • ×⍵|:mod的符号(即:如果除法的余数为0,则为0,否则为1)
  • ⍵/⍨:仅保留mod为0的元素

0

球拍179字节

(λ(l)(define m(apply max l))(let g((n 1)(c 0))(set! l(for/list((i l))(if(= i n)(begin 
(set! c(+ 1 c))(if(= 0(modulo c n))0 i))i)))(if(< n m)(g(+ 1 n)0)(filter(λ(x)(> x 0))l))))

取消高尔夫:

(define f
  (λ(l)
    (define m (apply max l))
    (let loop ((n 1) (c 0))
      (set! l (for/list ((i l))
                (if (= i n)
                    (begin
                      (set! c (+ 1 c))
                      (if (= 0 (modulo c n))
                          0 i ))                  ; replace by 0
                    i )))
      (if (< n m)
          (loop (+ 1 n) 0)
          (filter (λ(x)(> x 0)) l)                ; remove all 0s
          ))))

测试:

(f '[1]) 
(f '[2]) 
(f '[1 1 1]) 
(f '[2 2 2]) 
(f '[1 1 2 2 2 3 3 3 3])
(f '[1 2 3 1 2 3 1 2 3 1 2 3]) 
(f '[3 2 6 1 1 6 2 3 2 6 6 6 6 1 6 6 3 3 7 2])

输出:

'()
'(2)
'()
'(2 2)
'(2 2 3 3 3)
'(2 3 3 2 3)
'(3 2 6 6 3 2 6 6 6 6 6 3 7)
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.