Python教程

pythonchallenge Level 10

本文主要是介绍pythonchallenge Level 10,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

第10关地址:http://www.pythonchallenge.com/pc/return/bull.html

账号:huge 密码:file

页面提示 len(a[30])

点击大牛的位置,跳转到了http://www.pythonchallenge.com/pc/return/sequence.txt

a = [1, 11, 21, 1211, 111221, 

也就是找规律,求len(a[30])是多少

规律是第二个数字是读第一个数字,如

1:1个1=11 

11:2个1=21

21:1个2,2个1=1221

1211:1个1,1个2,2个1=111221

111221:3个1,2个2,1个1=312211

...以此类推得到a[30]并且计算长度

def deal_numNew(str_num):
    num = 1
    new_list = []
    if len(str_num) == 1:
        num = 1
        new_list.append(str(num))
        new_list.append(str_num[0])
    else:
        for i in range(len(str_num)):
            if i+1==len(str_num):
                new_list.append(str(num))
                new_list.append(str_num[i])
            elif str_num[i+1] == str_num[i]:
                num += 1
            else:
                new_list.append(str(num))
                new_list.append(str_num[i])
                num = 1
    return ''.join(new_list)


a = [1, 11, 21, 1211, 111221, 312211]
str_num = "312211"
for i in range(30):
    new_list = deal_numNew(str_num)
    a.append(new_list)
    str_num = a[-1]
print(a[30])
print(len(a[30]))

得到 len(a[30]) = 5808

获得下一关地址:http://www.pythonchallenge.com/pc/return/5808.html

这篇关于pythonchallenge Level 10的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!