第十一题地址:http://www.pythonchallenge.com/pc/return/bull.html
我们根据查看HTML代码,图中的牛可以点击跳转到一个.../sequence.txt的地址。我们得到一个a的list。最下面的问题是len(a[30])是多少,所以我们最后需要解到这个list的第三十个数字的长度。
查阅之后发现这样的list叫look and see sequence,【1,11,21,1211,.....】就是看到1,读作一个1,得到11;11读作两个1,得到21;balabala...
a = '1'
for i in xrange(30):
pos = 0
tmp = ''
str_len = len(a)
while pos < str_len:
count = 1
# count how many are the same number
while pos+1 < str_len and a[pos] == a[pos+1]:
count += 1
pos += 1
# add that into the string
tmp += '%d%s' % (count, a[pos])
pos += 1
a = tmp
print(len(a))
# result, the 30th
5808上述代码,表示先得到一个1,然后循环29次得到第三十个数字。使用xrange是为了节省空间。
代码的意思是,先look,读当前的数字,重复的数字累计,然后加上到一个string中,一直到当前数字读完。
最终的结果是5808
本文介绍了一个有趣的数列生成挑战,通过Python代码实现Look-and-Say数列的生成,并给出了第30项的长度为5808的答案。

1468

被折叠的 条评论
为什么被折叠?



