与えられた長さのマジックナンバー


13

プログラムは(n説明のために)入力を受け取り、n繰り返し桁のない数字の長さのすべての順列を出力する必要があります。ここで、インデックスの前後の各数字は、数字の位置で割り切れます。

ここでマジックナンバーについて読むことができます

ルール:

  • 1 <= n <= 10
  • 数字を繰り返すことはできません
  • 先頭の0が存在する必要があります(該当する場合)
  • 介して第1 x(1等の最初の文字で始まる)番号の桁目で割り切れなければならないx、すなわちで、3068531で割り切れる、302で割り切れるが、3063で割り切れる、30684で割り切れる、及び306855によってdivislbeあります。
  • プログラムは、整数を入力として(コマンドラインから、関数の引数などとして)受け取り、ルールを満たすすべての順列を出力する必要があります。
  • 出力は1つ以上の空白文字で区切る必要があります
  • 順列ゼロで始まる場合があります(したがって、技術的には魔法の数字ではありません)。
  • 出力の順序は関係ありません
  • 予期しない入力を処理する必要ありませ
  • バイト単位の最小文字が勝ちます

与えられた1:

0
1
2
3
4
5
6
7
8
9

与えられた2:

02
04
06
08
10
12
14
16
18
20
24
26
28
30
32
34
36
38
40
42
46
48
50
52
54
56
58
60
62
64
68
70
72
74
76
78
80
82
84
86
90
92
94
96
98

与えられた10:

3816547290

オリジナルのパズル(オプションA)は、Pizza HutとJohn H. Conwayの功績によるものです。@Megoと@ sp3000のリンクに感謝します



6
@DavisDude「関連」は「複製」を意味しません。関連リンクを投稿する目的は、そのチャレンジがサイドバーに「リンク」として表示されることです。
マーティンエンダー

1
関連資料
-Sp3000

3
先頭の0は、それらを含む出力番号を含める必要がありますか?
xnor

4
出力に関しては印刷空白に言及しますが、関数の場合、最も自然な形式の出力はおそらくリストを返すでしょう。それは許されますか?
デニス

回答:


4

ゼリー20 17 16 バイト

QḣQV%S
ØDṗçÐḟRj⁷

これは非常に遅く、メモリを集中的に使用します... オンラインで試してみてください!

使い方

ØDṗçÐḟRj⁷  Main link. Input: n (integer)

ØD         Yield d := '0123456789'.
  ṗ        Compute the nth Cartesian power of d.
      R    Range; yield [1, ..., n].
    Ðḟ     Filter false; keep strings of digits for which the following yields 0.
   ç         Apply the helper link to each digit string and the range to the right.
       j⁷  Join the kept strings, separating by linefeeds.


QḣQḌ%S     Helper link. Arguments: s (digit string), r (range from 1 to n)

Q          Unique; deduplicate s.
 ḣ         Head; get the prefixes of length 1, ..., n or less.
           If s had duplicates, the final prefixes fill be equal to each other.
  Q        Unique; deduplicate the array of prefixes.
   V       Eval all prefixes.
    %      Compute the residues of the kth prefixes modulo k.
           If s and the array of prefixes have different lengths (i.e., if the
           digits are not unique), some right arguments of % won't have corr. left
           arguments. In this case, % is not applied, and the unaltered right
           argument is the (positive) result.
     S     Add all residues/indices. This sum is zero iff all digits are unique
           and the kth prefixes are divisible by k.

3
これが遅い場合...私の答えは眠そうなナメクジ
ルイスメンドー

6

JavaScript(Firefox 30-57)、77バイト

f=n=>n?[for(s of f(n-1))for(c of"0123456789")if(s.search(c)+(s+c)%n<0)s+c]:[""]

編集:@ edc65のおかげで1バイトを保存しました。


宝石!...of"012...
-edc65

@ edc65うーん、私はそれを見過ごしていたとは信じられません。
ニール

3

Pyth、19バイト

jf!s%VsM._TS;.PjkUT

デモンストレーション

ブルートフォースソリューション。続く説明。FryAmTheEggmanによるインスピレーション


22バイト

juf!%sThH{I#sm+LdTGQ]k

デモンストレーション

数値は文字列として構築および保存され、分割可能性を確認するためにのみintに変換されます。

説明:

juf!%sThH{I#sm+LdTGQ]k
 u                 Q]k    Apply the following input many times, starting with ['']
             m    G       For each string at the previous step,
              +LdT        Append each digit to it
            s             Concatenate
         {I#              Filter out strings with repeats
  f                       Filter on
     sT                   The integer
    %  hH                 Mod the 1 indexed iteration number
   !                      Is zero.
j                         Join on newlines.

私は好奇心が強いです。Pythを学ぶためには、どれほど自虐的でなければならないのですか / s-
デイビスデュード

2
@DavisDude人々がそれを見るときに考えるよりも簡単だと思います。最も恐ろしい部分が始まっています。
入場したら、入場

1
デバッグモードがあなたをどれだけ助けてくれるのか、それはかなり簡単です。ドキュメントも非常に優れており、知っておくべきことを説明しています。
ヴェン

参考までに、私はもう1つ使用する._などして大がかりになりましたが、大きな入力の場合は遅くなります。jjLkf!s.e%ib10hk._T.PUT
FryAmTheEggman

3

MATL、30バイト

4Y2Z^!"@Sd@!U10G:q^/kPG:\~h?@!

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

とても遅いです。以下のためにinput 3それをオンラインコンパイラには数秒かかります。数字を1つずつ表示するには、コードの最後にを含めDます

説明

4Y2       % predefined literal: string '0123456789'
Z^        % implicit input. Cartesian power: 2D char array. Each number is a row
!         % transpose
"         % for each column
  @       %   push current column
  Sd      %   sort and compute consecutive differences (*)
  @!U     %   push current column. Convert to number
  10G:q^  %   array [1 10 100 ... 10^(n-1)], where n is the input
  /k      %   divide element-wise. Round down
  P       %   reverse array
  G:      %   array [1 2 ... n]
  \~      %   modulo operation, element-wise. Negate: gives 1 if divisible (**)
  h       %   concatenate (*) and (**). Truthy if all elements are nonzero
  ?       %   if so
    @!    %     current number as a row array of char (string)
          %   implicitly end if
          % implicitly end if
          % implicitly display stack contents

コードに何か問題があります。5を超えると出力が生成されなくなり、5の場合、最後の数値(確認する必要がある唯一の数値)は正しくありません。986は3で割り切れない
-DavisDude


プロンプトを誤解したと思います。見て3、私はあなたがカップルの兆候(例えば026)を持って見ることができます。また、説明をいただければ
幸いです-DavisDude

これはまだ機能しません。3は
021、024

@DavisDude編集、今私はもっと慎重に挑戦を読むこと
ルイスMendo

1

ルビー、87バイト

基本的な再帰ソリューション。

f=->n,x="",j=1{j>n ?puts(x):([*?0..?9]-x.chars).map{|i|f[n,x+i,j+1]if((x+i).to_i)%j<1}}

印刷する代わりに順列のリストを返すことができる場合、85バイト:

f=->n,x="",j=1{j>n ?x:([*?0..?9]-x.chars).map{|i|f[n,x+i,j+1]if((x+i).to_i)%j<1}-[p]}

1

Python、132バイト

lambda n:[x for x in map(("{:0%s}"%n).format,(range(10**n)))if all(int(x[:i])%i<1and len(set(x))==len(x)for i in range(1,len(x)+1))]

itertoolsSp3000を使用してはいけないことに気づかせてくれたおかげで、を削除して26バイトを削除しました。

filterチップではなく、Sp3000のおかげで、リストの内包表記ではなく2バイトを使用してドロップしました。

オンラインで試す:Python 2Python 3

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