果冻,28个字节
ṣ”+ṣ”xV$€)p/ZPSƭ€j⁾x^Ʋ€j“ +
在线尝试!
完整程序。将两个多项式作为两个字符串的列表。
说明(扩展形式)
ṣ”+ṣ”xV$€µ€p/ZPSƭ€j⁾x^Ʋ€j“ + ” Arguments: x
µ Monadic chain.
€ Map the monadic link over the argument.
Note that this will "pop" the previous chain, so
it will really act as a link rather than a
sub-chain.
ṣ”+ ṣ, right = '+'.
Split the left argument on each occurrence of
the right.
Note that strings in Jelly are lists of
single-character Python strings.
€ Map the monadic link over the argument.
$ Make a non-niladic monadic chain of at least
two links.
ṣ”x ṣ, right = 'x'.
Split the left argument on each occurrence of
the right.
V Evaluate the argument as a niladic link.
/ Reduce the dyadic link over the argument.
p Cartesian product of left and right arguments.
€ Map the monadic link over the argument.
Ʋ Make a non-niladic monadic chain of at least
four links.
Z Transpose the argument.
€ Map the monadic link over the argument.
ƭ At the first call, call the first link. At the
second call, call the second link. Rinse and
repeat.
P Product: ;1×/$
S Sum: ;0+/$
j⁾x^ j, right = "x^".
Put the right argument between the left one's
elements and concatenate the result.
j“ + ” j, right = " + ".
Put the right argument between the left one's
elements and concatenate the result.
混叠
)
与相同µ€
。暗示
尾随”
,可以省略。
算法
假设我们有以下输入:
["6x^2 + 7x^1 + -2x^0", "1x^2 + -2x^3"]
第一个过程是解析,应用于两个多项式的每一个。让我们处理第一个"6x^2 + 7x^1 + -2x^0"
:
第一步是用分割字符串'+'
,以分隔术语。结果是:
["6x^2 ", " 7x^1 ", " -2x^0"]
下一步是将每个字符串除以'x'
,以将系数与指数分开。结果是这样的:
[["6", "^2 "], [" 7", "^1 "], [" -2", "^0"]]
当前,这些字符串中似乎有很多垃圾,但实际上这些垃圾并不重要。这些字符串都将被评估为尼拉迪奇果冻链接。琐碎地,空格并不重要,因为它们不在数字的数字之间。因此,我们可以对以下内容进行评估,但仍获得相同的结果:
[["6", "^2"], ["7", "^1"], ["-2", "^0"]]
本^
来看多一点令人不安,但它们实际上并不无论做任何事情!好吧,这^
是按位XOR原子,但是niladic链的作用类似于monadic链接,只是第一个链接实际上是参数,而不是参数(如果是niladic)。如果不是,则链接的参数为0
。该指数有^
S作为他们的第一个字符,而^
不是译注,这样的说法被认为是0
。字符串的其余部分(即数字)是的正确参数^
。因此,例如,^2
是0 XOR 2 = 2。明显,0 XOR n = n。所有指数都是整数,所以我们很好。因此,对此进行评估而不是以上内容不会更改结果:
[["6", "2"], ["7", "1"], ["-2", "0"]]
开始了:
[[6, 2], [7, 1], [-2, 0]]
此步骤还将转换"-0"
为0
。
由于我们要解析两个输入,因此解析后的结果将是这样:
[[[6, 2], [7, 1], [-2, 0]], [[1, 2], [-2, 3]]]
解析到此完成。下一个过程是乘法。
我们首先采用以下两个列表的笛卡尔乘积:
[[[6, 2], [1, 2]], [[6, 2], [-2, 3]], [[7, 1], [1, 2]], [[7, 1], [-2, 3]], [[-2, 0], [1, 2]], [[-2, 0], [-2, 3]]]
制作了许多对,每个对按顺序从左侧列表中的一个元素到右侧的一个。这也恰好是输出的预期顺序。这个挑战确实要求我们应用乘法分布,因为在此之后我们不要求进一步处理结果。
每对中的对表示我们要相乘的项,第一个元素是系数,第二个元素是指数。要乘以项,我们乘以系数,然后将指数相加(一个XCb Xd= a b xCXd= a b (xCXd)= (a b )xc + d)。我们该怎么做?让我们处理第二对[[6, 2], [-2, 3]]
。
我们首先换位:
[[6, -2], [2, 3]]
然后,我们取第一对的乘积和第二对的和:
[-12, 5]
代码的相关部分PSƭ€
实际上并没有为每对术语重置其计数器,但是由于它们是成对的,因此不需要。
处理所有成对的术语,我们有:
[[6, 4], [-12, 5], [7, 3], [-14, 4], [-2, 2], [4, 3]]
在这里,乘法完成了,因为我们不必合并类似的术语。最终过程是Prettyfying。
我们首先加入"x^"
:
[[6, 'x', '^', 4], [-12, 'x', '^', 5], [7, 'x', '^', 3], [-14, 'x', '^', 4], [-2, 'x', '^', 2], [4, 'x', '^', 3]]
然后我们加入列表" + "
:
[6, 'x', '^', 4, ' ', '+', ' ', -12, 'x', '^', 5, ' ', '+', ' ', 7, 'x', '^', 3, ' ', '+', ' ', -14, 'x', '^', 4, ' ', '+', ' ', -2, 'x', '^', 2, ' ', '+', ' ', 4, 'x', '^', 3]
请注意,列表中仍然有数字,因此它并不是真正的字符串。但是,Jelly有一个名为“字符串化”的过程,该过程在程序执行结束时立即运行以打印出结果。对于深度为1的列表,它实际上只是将每个元素转换为其字符串表示形式并将字符串连接在一起,因此我们得到了所需的输出:
6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3