当我尝试向这个 J 程序添加第二个钩子/叉子时,我得到了意想不到的结果。谁能解释为什么?
((1&{~+/)*./\(=1&{))1 1 1 3 2 4 1
我总是收到索引错误。
要点是输出两个数字,一个与列表中的第一个数字相同,第二个与该数字重复的次数相同。
所以这很有效:
*./\(=1&{)1 1 1 3 2 4 1
1 1 1 0 0 0 0
我将第一个数字与列表的其余部分进行比较。然后我插入一个 和 压缩 - 只要我有一个完整的 1 字符串,一旦它破坏了 和 失败并且零出现,这就会给我一个 1。
我想我可以添加另一组括号,再次从列表中获取前导元素,并以某种方式记录这些数字,最终的想法是有另一个阶段,我将向量的逆应用于原始列表,并且然后使用 $: 返回同一动词的递归应用。有点像快速排序的例子,我以为我理解了,但我想我没有。
但我什至无法靠近。我将把这个问题作为一个单独的问题提出,以便人们的回答得到适当的认可。
((1&{~+/)*./\(=1&{))1 1 1 3 2 4 1
I always get Index Error.
The point is to output two numbers, one that is the same as the first number in the list, the second which is the same as the number of times that number is repeated.
So this much works:
*./\(=1&{)1 1 1 3 2 4 1
1 1 1 0 0 0 0
I compare the first number against the rest of the list. Then I do an insertion of an and compression - and this gives me a 1 so long as I have an unbroken string of 1's, once it breaks the and fails and the zeros come forth.
I thought that I could then add another set of parens, get the lead element from the list again, and somehow record those numbers, the eventual idea would be to have another stage where I apply the inverse of the vector to the original list, and then use $: to get back for a recursive application of the same verb. Sort of like the quicksort example, which I thought I sort of understood, but I guess I don't.
But I can't even get close. I will ask this as a separate question so that people get proper credit for answering.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在寻找这样的东西吗?
Were you looking for something like this?
J 的索引为 0,因此您想尝试以下操作:
J is 0 indexed, so you want to try this instead:
从你的问题中我不太确定这是否是你想要的最初结果?
这可能是获取第一个元素重复次数的更简单方法。
我的印象是您希望最终得到类似于以下内容的结果? (给出列表中连续重复的计数)
I'm not quite sure from your question whether that is the initial result that you were trying for?
This might be a simpler way of getting the number of times the first element is repeated.
I get the impression that you are wanting to end up with something similar to the following? (which gives the count of the consecutive repeats in the list)
第 1 部分
有内置动词
{.
来获取数组中的第一项,因此第一部分已完成。第 2 部分
对于第二部分,我们可以将其与
e.
< 结合起来/a> 如果给定一个向量,则返回一个表,显示元素出现的位置:使用第一行(或列!):
求和此结果:
组合
将第 1 部分和第 2 部分组合成 2 元素列表是通过二元实现的
,
(附加):我总是作弊并且使用显式到默认转换器 (
13 :
) 来处理如下情况:但此时您可能会注意到,使用
[:
有点粗暴(上限)和NMR 的答案更清晰。阅读本文的好处
如果您返回一个装箱数组(通过使用
;
而不是,
),您可以将此函数扩展到数字以外的列表:Part 1
There's the built in verb
{.
to get the first item from an array, so the first part's done.Part 2
For the second part, we can combine this with
e.
which, if given a vector, returns a table showing where the elements occur:Use the first row (or column!):
Sum this result:
Combine
Combining parts 1 and 2 into a 2-element list is achieved with dyadic
,
(append):I always cheat and use the explicit-to-tacit converter (
13 :
)for things like this:But at this point you may notice that this is a bit heavy-handed with the
[:
(cap) and NMR's answer is cleaner.Bonus for reading this far
If you return a boxed array (by using
;
instead of,
), you can extend this function to lists of things other than numbers: