列表是可分割的吗?


20

启发(与解释被盗)

背景

假设您有两个列表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 = 2a_3 / b_3 = 2,所有这一切都是整数,因此A是潜在的,整除B

作为应该输出的示例False,我们可以有:

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

这是False因为我们不能重新排序,B因为25和5在中A,但是in 的唯一除数B是5,因此将被忽略。

你的任务

显然,您的任务是确定两个列表(作为输入)是否可能被整除。您可以采用任何可接受的方式进行输入,就像输出一样。

列表中可能有重复项,并且整数的唯一大小限制是您的语言。两个列表中的所有整数都将大于0,并且两个列表的大小均相等。

与所有,输出值必须是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
@毛茸茸的问题: 两个列表的大小相等
Caird coinheringaahing

2
为什么最后一个测试用例未定义?
丹尼斯,

1
@丹尼斯其中一个列表中有一个0
Caird coinheringaahing

1
对。(不确定为什么0可以被所有整数整除。)两个输出是否必须是真实的和虚假的,或者仅仅是一致的?
丹尼斯,

@Dennis 1)在第二个列表中为0的情况下,以避免0除法错误2)保持一致
caird coinheringaahing

Answers:


10

果冻,5个字节

Œ!%ḄẠ

退货 对于True 0,对于False 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

发生在相反的顺序argents和回报1True0False

在线尝试!

说明

    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¦P应该工作6个字节。
Zgarb

那些被认为是“两个不同的价值”吗?
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字节

使用Dennis's Jelly答案中的想法减少了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)]!={}& 

感谢@ngenisis -5个字节


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字节

-3个字节,得益于Jarko Dubbeldam

由于Jarko,又有了-8个字节

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

奇怪的是,R没有生成所有排列的内置函数。返回一个布尔值。

此外,在Jarko的第二项改进中,any将列表强制logical为警告的向量。

在线尝试!(小提琴)


1
全部(x <1)比任何(!x)长,并且您应该可以将sum替换为any
JAD

@JarkoDubbeldam的好电话。谢谢。
朱塞佩

哦,您可以省略unlist,是的,隐式强制。
JAD

@JarkoDubbeldam很棒。
朱塞佩

2

Mathematica,42个字节

MemberQ[Permutations@#2,a_/;And@@(a∣#)]&



1

J,27个字节

0=[:*/(A.~i.@!@#)@]+/@:|"1[

在线尝试!

将第一个列表作为左参数,将第二个列表作为右参数。


1
21字节(|"1~e.~0*[)i.@!@#A.]
英里

1

CJam,20个 17字节

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

测试版本

将数组B作为第一个参数并将数组A作为第二个参数的函数。请注意,在测试版本中,我将顺序从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 bytes

I was thinking too short.

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

anonymous function takes two arrays, returns NULL for falsy and 1 for truthy.
Throws an error if second array contains 0.

Try it online.


Prints the wrong result for $f([6,5],[3,5]).
nwellnhof

@nwellnhof fixed. thanks for noticing.
Titus

1

C (gcc), 191 bytes

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

Try it online!

Usage: f(int size, int size, int *a, int *b)

returns 1 if divisable, 0 otherwise. See example usage on TIO.

(Gotta do permutations the hard way in C, so this is hardly competitive)


1

Perl 6, 38 bytes

Actually @nwellnhof's answer seems to be too much readable, so I set out to follow the fine Perl tradition of write-only code :—).

1 byte saved thanks to @nwellnhof.

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

Try it online!

What does it do: It's an anonymous function that takes two list arguments. When we say @^a, we mean the first one, when @^b, it's the second one.

(@^a,) is a list containing the list @^a. @^b.permutations is the list of all the permutations of @^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 %%.

The max operator gives the largest element of the list (in this case, it's the list that has the most True's in it). We then reduce it using the logical AND operator to see if all elements of that "most true" list are true, and that's the result. It's nearly exact copy of what @nwellnhof wrote, just using obscure operators to shave off bytes.


It says permutations, it is clearly far too readable ;)
caird coinheringaahing

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

Replace [&&] with min to save another byte.
nwellnhof

You can remove the spaces around the XZ%%
Jo King

I wish something like {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

Ruby, 56 bytes

->x,y{x.permutation.any?{|p|p.zip(y).all?{|a,b|a%b==0}}}

Try it online!

Fairly straightforward, exploits the fact that permutation exists.


0

Scala, 60 Bytes

Golfed:

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

Ungolfed:

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

Japt, 12 11 bytes

Outputs true or false.

Vá de@gY vX

Test it


Explanation

Implicit input of arrays U & V (A & B, respectively)

Generate an array of all permutations of V.

d

Check if any of the elements (sub-arrays) return true.

e@

Check if every element in the current sub-array returns true when passed through the following function, with X being the current element and Y the current index.

gY

Get the element in U at index Y.

vX

Check if it's divisible by X.

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.