当我尝试向这个 J 程序添加第二个钩子/叉子时,我得到了意想不到的结果。谁能解释为什么?

发布于 2024-12-05 08:42:01 字数 480 浏览 0 评论 0原文

((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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

够钟 2024-12-12 08:42:01

您在寻找这样的东西吗?

f=.{.,+/@:({. E. ])

NB.f 1 1 1 3 2 4 1
NB.1 4

Were you looking for something like this?

f=.{.,+/@:({. E. ])

NB.f 1 1 1 3 2 4 1
NB.1 4
向日葵 2024-12-12 08:42:01

J 的索引为 0,因此您想尝试以下操作:

((0&{~+/)*./\(=0&{))1 1 1 3 2 4 1

J is 0 indexed, so you want to try this instead:

((0&{~+/)*./\(=0&{))1 1 1 3 2 4 1
仅此而已 2024-12-12 08:42:01
   (0&{ , +/@(*./\)@(= 0&{)) 1 1 1 3 2 4 1
1 3

从你的问题中我不太确定这是否是你想要的最初结果?

这可能是获取第一个元素重复次数的更简单方法。

   =/\ 1 1 1 3 2 4 1
1 1 1 0 0 0 0

我的印象是您希望最终得到类似于以下内容的结果? (给出列表中连续重复的计数)

  ([: #/.~ 0 , [: +/\ 2 ~:/\ ]) 1 1 1 3 2 2 4 1
3 1 2 1 1
   (0&{ , +/@(*./\)@(= 0&{)) 1 1 1 3 2 4 1
1 3

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.

   =/\ 1 1 1 3 2 4 1
1 1 1 0 0 0 0

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)

  ([: #/.~ 0 , [: +/\ 2 ~:/\ ]) 1 1 1 3 2 2 4 1
3 1 2 1 1
秋日私语 2024-12-12 08:42:01

第 1 部分

有内置动词 {. 来获取数组中的第一项,因此第一部分已完成。

第 2 部分

对于第二部分,我们可以将其与 e.< 结合起来/a> 如果给定一个向量,则返回一个表,显示元素出现的位置:

   e. 1 3 1 1
1 0 1 1
0 1 0 0
1 0 1 1
1 0 1 1

使用第一行(或列!):

   {.e. 1 3 1 1
1 0 1 1

求和此结果:

   +/{.e. 1 3 1 1
3

组合

将第 1 部分和第 2 部分组合成 2 元素列表是通过二元实现的,(附加):

(result of {.) , (result of sum of first row (or column!) of e.)

我总是作弊并且使用显式到默认转换器 (13 :) 来处理如下情况:

   13 : '({.y) , +/{. e.y'
{. , [: +/ [: {. e.

但此时您可能会注意到,使用 [: 有点粗暴(上限)和NMR 的答案更清晰。

阅读本文的好处

如果您返回一个装箱数组(通过使用 ; 而不是 ,),您可以将此函数扩展到数字以外的列表:

   f =: 13 : '({.y) ; +/{. e.y'
   f  'abracadabra'
┌─┬─┐
│a│5│
└─┴─┘

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:

   e. 1 3 1 1
1 0 1 1
0 1 0 0
1 0 1 1
1 0 1 1

Use the first row (or column!):

   {.e. 1 3 1 1
1 0 1 1

Sum this result:

   +/{.e. 1 3 1 1
3

Combine

Combining parts 1 and 2 into a 2-element list is achieved with dyadic , (append):

(result of {.) , (result of sum of first row (or column!) of e.)

I always cheat and use the explicit-to-tacit converter (13 :)for things like this:

   13 : '({.y) , +/{. e.y'
{. , [: +/ [: {. e.

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:

   f =: 13 : '({.y) ; +/{. e.y'
   f  'abracadabra'
┌─┬─┐
│a│5│
└─┴─┘
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文