リストは分割可能ですか?


20

(説明を盗まれた)これに触発された

バックグラウンド

2つのリストA = [a_1, a_2, ..., a_n]B = [b_1, b_2, ..., b_n]整数のリストがあるとします。私たちは、言うA潜在的に割り切れるB存在する場合の順列Bことが可能a_iで割り切れるb_iすべてのためにi。問題は次のとおりです。すべてで割り切れるBように並べ替える(つまり、並べ替える)ことa_iは可能ですか?たとえば、あなたが持っている場合b_ii

A = [6, 12, 8]
B = [3, 4, 6]

そして、その答えは次のようになりますTrueよう、Bように並べ替えることができますB = [3, 6, 4]し、我々はそれを持っているだろうa_1 / b_1 = 2a_2 / b_2 = 2と、a_3 / b_3 = 2整数であり、そのすべてが、そう、Aによって潜在的に割り切れますB

出力すべき例としてFalse

A = [10, 12, 6, 5, 21, 25]
B = [2, 7, 5, 3, 12, 3]

これは、25と5がinであるため、False再注文できないためです。ただし、唯一の除数は5なので、1つは省略されます。BAB

あなたのタスク

あなたの仕事は、明らかに、(入力として与えられた)2つのリストが潜在的に割り切れるかどうかを判断することです。出力と同様に、受け入れられた方法で入力を受け取ることができます。

リスト内の重複は可能性があり、整数のサイズ制限は言語のみです。両方のリストのすべての整数は0より大きくなり、両方のリストは同じサイズになります。

すべてのと同様に、出力値はtrueとfalseを表す2つの異なる値でなければなりません。

これはので、最短のコードが勝ちます!

テストケース

Input, input => output

[6, 12, 8], [3, 4, 6] => True
[10, 5, 7], [1, 5, 100] => False
[14, 10053, 6, 9] [1,1,1,1] => True
[12] [7] => False
[0, 6, 19, 1, 3] [2, 3, 4, 5, 6] => undefined

3
質問から@Shaggy:両方のリストは同じサイズになります
ケアード共編

2
最後のテストケースが未定義なのはなぜですか?
デニス

1
リストの@Dennis 1は、その中に0がある
coinheringaahing caird

1
右。(理由はわかりませんが、0はすべての整数で割り切れます。)2つの出力は、真偽である必要がありますか?
デニス

@Dennis 1)0が2番目のリストにある場合、0の分割エラーを回避するために2)ちょうど一貫性がある
ケアード共生

回答:


10

ゼリー、5バイト

Œ!%ḄẠ

Trueの場合は0Falseの場合は1を返します

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

使い方

Œ!%ḄẠ  Main link. Arguments: A, B (arrays)

Œ!     Generate all permutations of A.
  %    Take each permutation modulo B (element-wise).
   Ḅ   Convert all resulting arrays from binary to integer.
       This yields 0 iff the permutation is divisible by B.
    Ạ  All; yield 0 if the result contains a 0, 1 otherwise.

9

ハスク7 6 5バイト

@Zgarbのおかげで2バイト節約

▼▲‡¦P

引数を逆順で受け取り、1for Trueおよび0for を返しますFalse

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

説明

    P     -- Permutations of the first argument
  ‡       -- Deep zip (vectorises function) with second argument
   ¦      --   Does x divide y
 ▲        -- Get the maximum of that list, returns [1,1...1] if present
▼         -- Get the minimum of that list, will return 0 unless the list is all 1s

VΠMz¦P6バイトで動作するはずです。
-Zgarb

それらは「2つの異なる値」と見なされますか?
-geokavel

ああ、Mzできます
-Zgarb

▼▲代わりにあなたが必要だと思います▲▼。いずれにせよ、素晴らしいアイデアです!
-Zgarb

5

05AB1E、7バイト

入力:リストBとAを取ります(逆順)
出力:trueの場合は1、それ以外の場合は0

œvIyÖPM

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

説明:

œvIyÖPM    Complete program
œ          Pushes all permutations of B as a list
 v         For each permutation
  I        Pushes last input on top of the stack
   yÖ      Computes a % b == 0 for each element of A and B
     P     Pushes the total product of the list
      M    Pushes the largest number on top of the stack

5

MATL8 7 6バイト

デニスのゼリーの答えからのアイデアを使用して1バイトオフ

Y@\!aA

入力はB、その後、A。出力は0、割り切れるか1どうかです。

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

説明

Y@     % Implicit input: row vector B. Matrix of all permutations, each on a row
\      % Implicit input: row vector A. Modulo, element-wise with broadcast. Gives
       % a matrix in which each row contains the moduli of each permutation of B
       % with respect to A
!a     % True for rows that contain at least a nonzero value
A      % True if all values are true. Implicit display

3

Mathematica、52バイト

Cases[Permutations@#2,p_/;And@@IntegerQ/@(#/p)]!={}& 

-5バイトありがとう@ngenisis


2
Cases一般的に短いです:Cases[Permutations@#2,p_/;And@@IntegerQ/@(#/p)]!={}&
ngenisis

3

JavaScript(ES6)、67 63バイト

ブール値を返します。

f=([x,...a],b)=>!x||b.some((y,i)=>x%y?0:f(a,c=[...b],c[i]=1/0))

テストケース



3

R + combinat69 66 58バイト

Jarko Dubbeldamのおかげで-3バイト

Jarkoのおかげでさらに-8バイト

function(a,b)any(combinat::permn(b,function(x)all(!a%%x)))

奇妙なことに、Rにはすべての順列を生成するための組み込み機能がありません。ブール値を返します。

さらに、Jarkoの2番目の改善によりany、リストをlogical警告付きのベクターに強制します。

オンラインでお試しください!(r-フィドル)


1
すべての(X <1)が長い任意の(!x)のよりも、あなたはいずれかとの和を交換することができるはず
JAD

@JarkoDubbeldam良い電話。ありがとうございました。
ジュゼッペ

ああ、そしてunlistを省くことができます、暗黙の強制のためにいや。
-JAD

@JarkoDubbeldam素晴らしい。
ジュゼッペ





1

CJam、20 17バイト

:A;e!{A\.%:+!}#W>

テストバージョン

配列Bを最初の引数として、配列Aを2番目の引数として取る関数。テストバージョンでは、順序をA、Bの順に切り替えることに注意してください。


1

JavaScript(ES6)、100バイト

f=(a,b)=>!a[0]||a.some((c,i)=>b.some((d,j)=>c%d<1&f(e=[...a],d=[...b],e.splice(i,1),d.splice(j,1))))

やや非効率的。余分にそれ&をスピードアップします。


1

PHP、112 180 178バイト

私は短すぎると考えていました。

function($a,$b){for($p=array_keys($b);++$i<count($b);){foreach($b as$k=>$x)$f|=$a[$k]%$x;if($f=!$f)return 1;$p[$i]?[$b[$j],$b[$i],$i]=[$b[$i],$b[$j=$i%2*--$p[$i]],0]:$p[$i]=$i;}}

無名関数は2つの配列を取りNULL、偽りと真実を返します1
2番目の配列にが含まれて0いる場合、エラーがスローされます。

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


の誤った結果を出力し$f([6,5],[3,5])ます。
-nwellnhof

@nwellnhofが修正されました。気づいてくれてありがとう。
タイタス

1

C(gcc)、191バイト

#define F(v)for(i=0;i<v;++i){
#define X if(f(s,n,a,b))return 1
j;f(s,n,a,b,i)int*a,*b;{if(--n){F(n)X;j=i*(n%2);b[j]^=b[n];b[n]^=b[j];b[j]^=b[n];}X;}else{F(s)if(a[i]%b[i])return 0;}return 1;}}

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

使用法: f(int size, int size, int *a, int *b)

1割り切れる場合に戻ります。0。それ以外の場合。TIOの使用例を参照してください。

(Cで順列置換を行う必要があるため、これはほとんど競合しません)


1

Perl 6、38バイト

実際、@ nwellnhofの答えは読みやすいように思えるので、私は書き込み専用コードのすばらしいPerlの伝統に従うことに着手しました:—)。

@nwellnhofのおかげで1バイト節約されました。

{min max (@^a,) XZ%% @^b.permutations}

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

それは何をしますか:2つのリスト引数を取る匿名関数です。私たちが言うとき@^a、私たちは最初のものを意味し@^b、それは2番目のものです。

(@^a,)はlistを含むリスト@^aです。@^b.permutationsのすべての順列のリストです@^b. The "XZ%%" operator makes all possible pairs of that one list on the left and all the permutations on the right, and uses the operator "Z%%" on them, which is the standard "zip" operation using the divisibility operator %%.

maxオペレータは、リストの最大の要素を(この場合には、それはほとんど持っているリストです与えTrue、その中のを)。次に、論理AND演算子を使用してそれを減らし、その「最も真」のリストのすべての要素が真であるかどうかを確認し、それが結果です。@nwellnhofが書いたもののほぼ正確なコピーで、あいまいな演算子を使用してバイトを削り取っています。


それは言うpermutations、それは明らかにあまりにも読みやすいです;)
caird coinheringaahing

Well, Perl 6 has a really powerful introspection model. Perhaps I could study it in order to obscure that call? :D
Ramillies

交換する[&&]min to save another byte.
nwellnhof

周囲のスペースを削除できます XZ%%
Jo King

私は次のようなものを望みます {all (@^a,)Z%%@^b.permutations.any} were possible
Jo King

1

Brachylog, 6 bytes

pᵐz%ᵛ0

Try it online!

The predicate succeeds if the two lists are potentially divisible and fails if they are not.

pᵐ        For some pair of permutations of the two input lists,
  z       for each pair of corresponding elements
   %ᵛ0    the first mod the second is always zero.




0

Scala、60バイト

ゴルフ:

a=>b=>b.permutations exists(a zip _ forall(p=>p._1%p._2==0))

ゴルフをしていない:

a=>b=>         // Function literal taking 2 lists of integers, a and b.
b.permutations // All permutations of b.
exists(        // Whether the given function is true for any element.
a zip _        // Zips a and the current permutation of b into a list of pairs.
forall(        // Whether the given function is true for all elements.
p=>            // Function literal taking a pair of integers.
p._1%p._2==0)) // If the remainder of integer division between the members of the pair is 0.

0

ジャプト12 11バイト

出力trueまたはfalse

Vá de@gY vX

試して


説明

配列の暗黙的な入力UV(それぞれAB

のすべての順列の配列を生成しVます。

d

要素(サブ配列)のいずれかがtrueを返すかどうかを確認します。

e@

現在のサブ配列内のすべての要素Xが、現在の要素およびY現在のインデックスである次の関数を介して渡されたときにtrueを返すかどうかを確認します。

gY

Uindex の要素を取得しますY

vX

で割り切れるかどうかを確認しXます。

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