有没有方法找到列表模式后面的下一个可能元素?

huangapple go评论80阅读模式
英文:

Is there any method to find the next possible element following the list's pattern?

问题

我想要一个方法,以列表的模式获取列表中的下一个可能元素作为继续。

比如,有一个列表 ls,
ls = [1,2,2,3,2,2,1,2,2,3]

而我想要获取列表的下一个可能元素.. 在这种情况下,是"2"。

英文:

I want a method to get the next possible element as a continuation of the list following the pattern on the list.

Say, there is a list ls,
ls = [1,2,2,3,2,2,1,2,2,3]

And I want to get the next possible element of the list.. In this case, "2".

答案1

得分: 1

你可以在遍历列表时跟踪最短循环的长度。

cycle = 1
for i, x in enumerate(ls):
    if x != ls[i % cycle]:
        cycle = i + 1
print(ls[len(ls) % cycle]) # 2
英文:

You can keep track of the length of the shortest cycle while iterating over the list.

cycle = 1
for i, x in enumerate(ls):
    if x != ls[i % cycle]:
        cycle = i + 1
print(ls[len(ls) % cycle]) # 2

答案2

得分: 1

以下是代码部分的翻译:

建议"AI是必需的"当然是荒谬的但是你的问题陈述不清楚你提供的示例序列后面可以跟一个2但也可能有其他解决方案这里是[OEIS网站](https://oeis.org/search?q=1%2C2%2C2%2C3%2C2%2C2%2C1%2C2%2C2%2C3&language=english)上的一些示例

然而假设你正在寻找平凡的扩展算术进度无限重复一定数量的简单算术步骤),这是一个解决方案

def find_pattern(xs):
if len(xs) < 2:
return [0]
n = 1
while n < len(xs):
pattern = list(map(lambda x: x[1] - x[0], zip(xs[:n], xs[1:n + 1])))
for i, (x, y) in enumerate(zip(xs, xs[1:])):
if y - x != pattern[i % n]:
n = i + 1
break
else:
return pattern

sample = [1, 2, 2, 3, 2, 2, 1, 2, 2, 3]
pattern = find_pattern(sample)
print(pattern)


使用这个示例模式,你可以扩展序列:

def extend_pattern(xs, pattern, n):
x = xs[0]
for i in range(n):
yield x
if i < len(xs) and xs[i] != x:
raise ValueError(f'序列{xs}在索引{i}不匹配模式{pattern}。')
x = x + pattern[i % len(pattern])

print(list(extend_pattern(sample, pattern, 15)))


当结合使用时,输出如下:

[1, 0, 1, -1, 0, -1]
[1, 2, 2, 3, 2, 2, 1, 2, 2, 3, 2, 2, 1, 2, 2]


但是,作为这个方法出现严重错误的示例:

sample = [2, 3, 5, 7, 11, 13, 17, 19]
pattern = find_pattern(sample)
print(pattern)
print(list(extend_pattern(sample, pattern, 15)))


结果:

[1, 2, 2, 4, 2, 4, 2]
[2, 3, 5, 7, 11, 13, 17, 19, 20, 22, 24, 28, 30, 34, 36]


对于人来说,很明显`[2, 3, 5, 7, 11, 13, 17, 19]`是质数列表,但这并不是该函数找到的内容。
英文:

The suggestion that "AI is required" is of course silly. But your problem statement is unclear. The series you provide as an example can be followed by a 2, but other solutions are possible as well, here's a few examples on the OEIS website.

However, assuming that you're looking for the trivial extended arithmetic progression (repeating a fixed number of simple arithmetic steps indefinitely), this is a solution:

def find_pattern(xs):
    if len(xs) &lt; 2:
        return [0]
    n = 1
    while n &lt; len(xs):
        pattern = list(map(lambda x: x[1] - x[0], zip(xs[:n], xs[1:n + 1])))
        for i, (x, y) in enumerate(zip(xs, xs[1:])):
            if y - x != pattern[i % n]:
                n = i + 1
                break
        else:
            return pattern

sample = [1, 2, 2, 3, 2, 2, 1, 2, 2, 3]
pattern = find_pattern(sample)
print(pattern)

And with that sample pattern, you could extend the series:

def extend_pattern(xs, pattern, n):
    x = xs[0]
    for i in range(n):
        yield x
        if i &lt; len(xs) and xs[i] != x:
            raise ValueError(f&#39;The series {xs} does not match the pattern {pattern} at index {i}.&#39;)
        x = x + pattern[i % len(pattern)]


print(list(extend_pattern(sample, pattern, 15)))

When combined, the output:

[1, 0, 1, -1, 0, -1]
[1, 2, 2, 3, 2, 2, 1, 2, 2, 3, 2, 2, 1, 2, 2]

However, as an example that this gets horribly wrong:

sample = [2, 3, 5, 7, 11, 13, 17, 19]
pattern = find_pattern(sample)
print(pattern)
print(list(extend_pattern(sample, pattern, 15)))

Result:

[1, 2, 2, 4, 2, 4, 2]
[2, 3, 5, 7, 11, 13, 17, 19, 20, 22, 24, 28, 30, 34, 36]

To a person, it's fairly obvious that [2, 3, 5, 7, 11, 13, 17, 19] is a list of primes, but that's not what the function finds of course.

huangapple
  • 本文由 发表于 2023年2月19日 13:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定