`
he_lux
  • 浏览: 102994 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

python challenge 10

阅读更多
第十题。页面显示len(a[30]) = ?。 点击页面上的图片,显示a = [1, 11, 21, 1211, 111221, 看来是求规律,不过这个规律太难找了,我直接上网找答案了………………

原来1211表示1个2,1个1,111221表示1个1,1个2,2个1。 规律知道了,现在求第30个数的长度。
import re

if __name__ == '__main__':
    
    counter = 0
    
    def fun(s):
        
        result = ''
        
        char = s[0]
        count = 0
                    
        for x in s:
            if x == char:
                count += 1
            else:
                result += str(count) + char
                count = 1
                char = x
                
        result += str(count) + char
        
        print(len(result))
        
        global counter
        counter += 1
        
        if counter < 30:
            fun(result)
                
    fun('1')
    
    #solution 2 Start
    def describe(s):
        sets = re.findall("(1+|2+|3+)", s)
        return ''.join(str(len(x)) + x[0] for x in sets)
    
    s = '1'
    
    for dummy in range(30):
        s = describe(s)
        
    print(len(s))
    #solution 2 End


0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics