Python challenge 11 - look and see sequence

第十一题地址: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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值