计算一排数字(见上下文的详细信息) [英] calculate a row of numbers(see context for details)

查看:118
本文介绍了计算一排数字(见上下文的详细信息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两排数字,第1行是从0开始的连续数字,现在要求你填写第2行,以确保行2中的数字是correspoding数第1行中出现的第2行的时间

there are two rows of numbers, row 1 is consecutive numbers starting from 0, now ask you to fill out row 2 to make sure the number in row 2 is the times of correspoding number in row 1 appearing in row 2.

例如:

0 1 1 2 3 4 5 6 7 8 9

_ _ _ _ _ _ _ _ _ _

具体而言,我们使用 ROW1 为行1和 ROW2 为行2,我们填写< $ C C> ROW2 $,以确保它satisies: ROW2 [I] =计数(ROW2,ROW1 [I])计数(ROW2,ROW1 [I])表示 ROW1 [I] 的频率计数ROW2

To be more specific, we use row1 for row 1 and row2 for row 2, we fill out row2 to make sure it satisies: row2[i] = count(row2, row1[i]). count(row2, row1[i]) means frequency count of row1[i] among row2.

推荐答案

在对1000运行该解决方案必须运行循环中的平均3.608倍

Out of 1000 runs this solution had to run the loop an average of 3.608 times

import random

def f(x):
    l = []
    for i in range(10):
        l.append(x.count(i))
    return l

fast = list(range(10))

while f(fast) != fast:
    fast = []
    slow = []
    for i in range(10):
        r = random.randint(0,9)
        fast.append(r)
        slow.append(r)
    while True:
        fast = f(f(fast))
        slow = f(slow)
        if fast == slow:
            break

print(fast)

函数f(x)取猜测,x和返回计数。我们基本上是寻找一个解决方案,使得F(X)= X。

f(x) takes a guess, x, and returns the counts. We are essentially looking for a solution such that f(x) = x.

我们首先从0-9中选择10个随机整数,做一个清单。我们的目标是要反复设置该列表等于其自身,直到我们找到一条溶液或碰上一个周期。要检查周期,我们用乌龟和头发的算法,这在2的速度移动。速度快的两倍一样快的速度慢。如果这些都是平等的,我们遇到了一个周期,从一个新的随机场景开始。

We first choose 10 random integers from 0-9 and make a list. Our goal is to repeatedly set this list equal to itself until we either find a solution or run into a cycle. To check for cycles, we use the Tortoise and the Hair algorithm, which move at 2 speeds. A fast speed which is twice as quick as the slow speed. If these are equal, we have run into a cycle and start from a new random scenario.

我跑过了几次,发现对于n> 6的通解(其中,在这种情况下,N = 10)。它的形式为[正4,2,1,0 ...,0,1,0,0,0]

I ran through this a few times, and found the general solution for n>6 (where in this case n = 10). It is of the form [n-4,2,1,0...,0,1,0,0,0]

这篇关于计算一排数字(见上下文的详细信息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆