首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >给我看看轨迹

给我看看轨迹
EN

Code Golf用户
提问于 2021-08-15 19:35:00
回答 7查看 2.2K关注 0票数 23

给定一个整数列表,找出无限期地向右移动所指示的步骤所产生的“轨迹”(如果是负数),必要时从第一个元素开始包装。

此处的“轨迹”定义为一份清单,其中载有只访问一次的元素,按访问顺序排列,以及包含反复访问的元素的清单,也是按以下顺序排列的:

代码语言:javascript
复制
[first, second, ...], [first_of_loop, second_of_loop, ...]

请注意:

  • 多个元素可能具有相同的值,但在考虑是否已被访问时,这些元素是不同的。
  • 不需要处理空列表(给定一个空列表,您的代码可能出错)。

示例

给定的

代码语言:javascript
复制
[6, 0, -6, 2, -9 , 5, 3]

我们

  • 从第一个元素6开始
  • 向右走63
  • 向右走3-6
  • 左转62
  • 向右走25
  • 向右走5回到2

因此轨迹是

代码语言:javascript
复制
[6, 3, -6], [2, 5]

...where第二个列表显示最后一个循环(我们首先遇到2,然后是5,然后永远循环)。

测试用例

代码语言:javascript
复制
                                  in  out
                                 [0]  [], [0]
                                 [3]  [], [3]
                             [-1, 2]  [-1], [2]
               [5, 2, 4, 6, 7, 3, 1]  [], [5, 3, 2, 6, 4, 1]
             [6, 0, -6, 2, -9 ,5 ,3]  [6, 3, -6], [2, 5]
              [4, 5, 2, 6, -2, 7, 8]  [4], [-2, 2]
           [6, 10, 10, 0, -9, -4, 6]  [6, 6], [-4, 10, -9, 10]
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]  [], [9, 9, 9, 9]
EN

回答 7

Code Golf用户

发布于 2021-08-15 22:02:55

JavaScript (ES6),92字节

代码语言:javascript
复制
a=>[(g=p=>a=1/(i=g[p%=w=a.length])?[]:[q=a[p],...g(p+q+w*q*q,g[p]=k++)])(k=0).splice(0,i),a]

在网上试试!

评论

代码语言:javascript
复制
a => [                 // a[] = input array
  ( g = p =>           // g is a recursive function taking a position p
                       // the underlying object of g is also used to keep
                       // track of the positions that are visited
    a =                // update a[]
      1 / (            //
        i = g[         // reduce the position modulo the length w of the
          p %= w =     // array and load g[p] into i
            a.length   //
        ]              //
      ) ?              // if i is defined:
        []             //   we've found the loop: stop the recursion
      :                // else:
        [              //   update the output array:
          q = a[p],    //     load a[p] into q
          ...g(        //     do a recursive call:
            p + q +    //       add q to p
            w * q * q, //       also add w*q² to make sure it's >= 0
            g[p] = k++ //       save the index k into g[p] and increment k
          )            //     end of recursive call
        ]              //   end of array update
  )(k = 0)             // initial call to g with p = k = 0
  .splice(0, i),       // extract the non-looping part
  a                    // append the looping part
]                      //
票数 4
EN

Code Golf用户

发布于 2021-08-15 20:54:57

Pip -xp,32字节

代码语言:javascript
复制
^:iT(Yy+a@y%:#a)NiiPBya@i^@:i@?y

将列表(格式化为:[1;2;-3])作为命令行参数,并输出两个列表的列表。在这里试试!或,验证所有测试用例在TIO。

解释

代码语言:javascript
复制
^:iT(Yy+a@y%:#a)NiiPBya@i^@:i@?y
                                  a is cmdline input, eval'd (-x flag);
                                  i is 0, y is "" (implicit)
^:i                               Split i and assign back to i: i is now [0]
   T                              Loop until
      y                            current index
       +a@y                        plus number at current index
           %:                      mod
             #a                    length of list
     Y                             (yank into y, making this the new index)
    (          )Ni                 is already in the list of indices traversed:
                  iPBy              Push the new index onto the list of indices
                                  After the loop, we have the list of unique
                                  indices traversed in i and the first repeated
                                  index in y
                      a@i         Values from a at each index in i
                         ^@:      split at
                            i@?y  the index of y in i
票数 3
EN

Code Golf用户

发布于 2021-08-15 21:40:33

木炭,42字节

代码语言:javascript
复制
≔⁰ζW¬№υζ«⊞υζ≔﹪⁺ζ§θζLθζ»≔⌕υζζUMυ§θιI⟦…υζ✂υζ

在网上试试!链接是详细的代码版本。解释:

代码语言:javascript
复制
≔⁰ζ

从索引0开始。

代码语言:javascript
复制
W¬№υζ«

重复直到找到一个重复的索引。

代码语言:javascript
复制
⊞υζ

保存当前索引。

代码语言:javascript
复制
≔﹪⁺ζ§θζLθζ

计算新的索引。

代码语言:javascript
复制
»≔⌕υζζ

找到重复的位置。

代码语言:javascript
复制
UMυ§θι

将索引替换为值。

代码语言:javascript
复制
I⟦…υζ✂υζ

将重复之前和之后的值作为单独的数组输出。

票数 3
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/233424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档