N番目ごとのNを削除


41

タスク

この課題では、入力は正の整数の空でないリストであり、言語のネイティブ形式で与えられます。出力は、一部の要素が削除された同じ形式の同じリストです。のすべての出現1、2 2番目の出現3、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番目の発生2

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

その後、3回ごとに3

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

番号45は入力に含まれないため、スキップできます。次に、の6番目ごとの出現を削除します6

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

の発生は1つだけな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]

リーダーボード

これは、通常のリーダーボードと言語ごとの勝者の概要の両方を生成するスタックスニペットです。回答が表示されるようにするには、次のマークダウンテンプレートを使用して、見出しから回答を開始してください。

## 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

回答:


15

ゼリー10 8 バイト

=;\S€%af

2バイトのゴルフをしてくれた@ Sp3000に感謝します!

オンラインでお試しください!

使い方

=;\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 Jellyは左から右なので、最初です。=整数を比較します。例えば、[3,2,1]=;\比較3の要素と[3]2のものと[3, 2]して1のものと[3, 2, 1]与え、[1, [0, 1], [0, 0, 1]]
デニス

ああ、リストと要素をリストごとに比較していることを忘れていました。
キントピア

34

awk、10バイト

STDINでは、1行に1つの数値が入力されます。

++a[$1]%$1

説明

連想配列内の各数値のカウンターを保持し、カウンター値のモジュロnがゼロでない場合にのみ出力します。印刷は暗黙的です。ロングバージョン:

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

19

Pyth、18 15 14 10 9バイト

f%/aYTTTQ

これは、9バイトで5つの連続した変数参照を持つ、私が書いた最初のコードだと思います。

配列操作ソリューション(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
この言語
ニコ

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

5

Mathematica、40 38 36バイト

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

これは、を取得して返す無名の関数Listです。f(数値を追跡するために)実行時に名前付き関数を定義しますが、f事前に関連する定義をリセットします。

説明

Mathematicaでの関数(または関数定義)の動作は非常に強力です。Haskellの場合のように、関数は特定の型だけでなく、個々の値(または実際には引数の任意のパターン)に対してもオーバーロードおよび定義できません。しかし、a)これらの値は制御フロー中の副作用として定義でき、b)値はいつでも再定義できるという点で、Haskellよりもさらに強力です。これは、関数が実際には非常に強力なルックアップテーブルであることを意味します(オプションで、ルックアップされた値を格納するだけでなく計算できます)。

コードからgolfitudeを削除すると、次のようになります。

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

そのため、最初に入力をループし、リスト内のf[x] = xすべてに対してを定義しますxf最終的には、各番号がリストに既に表示されている頻度を追跡するために使用されます。なぜ私たちはから数えないの0ですか?リストのループはMapです。式f[x] = yy(関数定義の保存に加えて)戻ります。したがって、に設定f[x]するxと、マップは入力リスト自体に評価されます。これにより2バイトを節約できます。なぜなら、にlist明示的に再度提供する必要がないからSelectです。にのみ興味があるので、のx代わりに開始し0ても計算にはまったく影響しませんMod[f[x], x]

(通常f[_] = 0、フォールバック定義のようなものを使用してを回避Mapできますが、関数が以前に使用されたかどうかはわかりません。これにより、カウントが混乱する以前の値が定義されたままになります。)

次にSelect、2番目の引数として渡された名前のない関数が返す要素のみを保持することにより、リストをフィルターしますTrue。その関数は、最初f[x](ここxでは現在のリスト要素です)の値をインクリメントして、オカレンスをカウントし、その後、結果のカウントmoduloを取ります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.


2

PowerShell、56バイト

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

Rainer Pの回答と同様のヘルパー配列トリックを使用します。

入力を配列として受け取りますparam($a)。次に$b、オーバーロードされた乗算演算子と組み合わせたコンマ演算子を使用して、ゼロが設定された配列としてヘルパー配列を作成します。これにより、の最大数と等しく$bなるように作成されます。これを詳細に説明する「あなたの言語を紹介する答えのクイックプラグ@(0,0,0...0)$b.length$a

次は出力です。入力配列のすべての要素をループし$a|%{...}、各ループでifステートメントをチェックします。条件は、現在の要素に対応するヘルパー配列の値を事前にインクリメントし、モジュロ演算子を使用して現在の要素の倍数であるかどうかを確認します。倍数の場合、これはfalse %と等しい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]}
アレックス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

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

continueステートメントを削除することで、いくつかの文字を保存できるはずです。(未テスト)のようなもの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 !=1ましたw > 1
ドミトリー

配列も良いはずで、それらは少し短くなりますint [] R(int [] l、int n = 1)
ぼろぼろ

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

0

Mathematica、63バイト

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

ゴルフはとても面白いです!時々表示されるポップアップメッセージは無視してください。


0

ルビー、120バイト

->a{1.upto(a.max).each{|i|k=0;a.length.times.each{|j|k+=1if a[j]==i;a[j]=''if k%i==0&&a[j]==i;a[j]}};a.select{|i|i!=''}}

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)
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.