> Input
> Input
>> 1²
>> (3]
>> 1%L
>> L=2
>> Each 5 4
>> Each 6 7
>> L⋅R
>> Each 9 4 8
> {0}
>> {10}
>> 12∖11
>> Output 13
在线尝试!
返回一组所有可能的解决方案,以及空集(即 ∅)(如果不存在解决方案)。
怎么运行的
毫不奇怪,它的工作原理几乎与大多数其他答案相同:它生成一个数字列表,并使用参数检查每个数字的反模数。
如果您熟悉Whispers程序结构的工作原理,请随时跳到水平线。如果不是:从本质上讲,Whispers从最后一行开始在逐行参考系统上工作。每行被分类为两个选项之一。它是一条空白线,还是一条操作员线。
Nilad行以开头>
,例如> Input
或,> {0}
并返回该行表示的确切值,即> {0}
返回集合{ 0 }。> Input
返回STDIN的下一行,如果可能,则进行评估。
运算符行以开头>>
,例如>> 1²
或>> (3]
,表示在一个或多个值上运行运算符。在这里,所使用的数字不引用那些显式数字,而是引用该行上的值。例如,²
是square命令(n → n2),因此>> 1²
不返回值1个2,而是返回第1行的平方,在这种情况下,这是第一个输入。
通常,操作员行仅使用数字作为参考,但您可能已经注意到了>> L=2
和>> L⋅R
。这两个值L
和R
与Each
语句一起使用。Each
语句通过使用两个或三个参数(又作为数字引用)来工作。第一个参数(例如5
)是对使用函数的运算符行的引用,其余参数是数组。然后,我们在数组上迭代该函数,其中的L
和R
在函数中表示要迭代的数组中的当前元素。举个例子:
让 甲= [ 1 ,2 ,3 ,4 ], B=[4,3,2,1] and f(x,y)=x+y. Assuming we are running the following code:
> [1, 2, 3, 4]
> [4, 3, 2, 1]
>> L+R
>> Each 3 1 2
We then get a demonstration of how Each
statements work. First, when working with two arrays, we zip them to form C=[(1,4),(2,3),(3,2),(4,1)] then map f(x,y) over each pair, forming our final array D=[f(1,4),f(2,3),f(3,2),f(4,1)]=[5,5,5,5]
Try it online!
How this code works
Working counter-intuitively to how Whispers works, we start from the first two lines:
> Input
> Input
This collects our two inputs, lets say x and y, and stores them in lines 1 and 2 respectively. We then store x2 on line 3 and create a range A:=[1...x2] on line 4. Next, we jump to the section
>> 1%L
>> L=2
>> Each 5 4
>> Each 6 7
The first thing executed here is line 7, >> Each 5 4
, which iterates line 5 over line 4. This yields the array B:=[i%x|i∈A], where a%b is defined as the modulus of a and b.
We then execute line 8, >> Each 6 7
, which iterates line 6 over B, yielding an array C:=[(i%x)=y|i∈A].
For the inputs x=5,y=2, we have A=[1,2,3,...,23,24,25], B=[0,1,2,1,0,5,5,...,5,5] and C=[0,0,1,0,0,...,0,0]
We then jump down to
>> L⋅R
>> Each 9 4 8
which is our example of a dyadic Each
statement. Here, our function is line 9 i.e >> L⋅R
and our two arrays are A and C. We multiply each element in A with it's corresponding element in C, which yields an array, E, where each element works from the following relationship:
Ei={0AiCi=0Ci=1
We then end up with an array consisting of 0s and the inverse moduli of x and y. In order to remove the 0s, we convert this array to a set (>> {10}
), then take the set difference between this set and {0}, yielding, then outputting, our final result.