KEEP K.I.S.S.

tk's blog

pythonchallenge 10: what are you looking at?

tk posted @ Aug 31, 2012 03:47:30 PM in python with tags python pythonchallenge , 1324 阅读

python挑战的第10题

http://www.pythonchallenge.com/pc/return/bull.html(需要前面某题的钥匙,登录密码对 huge : file)

问题是 a = [1, 11, 21, 1211, 111221, 求 len(a[30]) = ?

数列的规律在于 后面一个元素是对前一个元素的“读法”,就是数数字,比如 "1211" 里是 1个"1"、1个"2"、2个"1",所以“读法”是"111221",这也就是下一个元素了。

其中一个字符串的“读法”关键是对于连续的同一个数字子序列是合并的读法,如果采用正则来匹配的话,就应该是这样的一个模式

pattern = re.compile(r'(\d)\1*')

(\d)用于匹配一个数字,然后成为一个组,\1就是引用这个组,* 默认是贪婪匹配,这样就可以匹配一个连续相同的数字串了。

用这个模式匹配一个字串后,就可以“读”出来,一个长度加上数字本身

# match 是匹配的 match object
s = match.group(0)
assert s
return "{0:d}{1:s}".format(len(s), s[0])

而对于一个完整的由多个不同数字子串构成的序列元素来说,完整“读法”的计算可以使用正则的 sub 方法

# repl function for regexp.sub
def repl(match):
    s = match.group(0)
    assert s
    return "{0:d}{1:s}".format(len(s), s[0])

# read a digit string, otherwise, the next string
def read_digit_str(str):
    # must be a digit string
    assert str.isdigit()

    return pattern.sub(repl, str)

这里的 sub 替换过程使用的是函数,函数将相同数字构成的串替换为对应的“读法”字符串

然后写一个序列生成器

# generators
def sequence():
    s = "1"
    while True:
        yield s
        s = read_digit_str(s)

这样就可以不断的生成序列元素了,如果要计算 a[30] 的长度,可以用内置的 enumerate 函数带索引遍历,完整代码:

# pythonchallenge 10
# http://www.pythonchallenge.com/pc/return/bull.html
#
# sequence: a = [1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211...
#
# puzzle: len(a[30]) = ?

import re
pattern = re.compile(r'(\d)\1*')

# repl function for regexp.sub
def repl(match):
    s = match.group(0)
    assert s
    return "{0:d}{1:s}".format(len(s), s[0])

# read a digit string, otherwise, the next string
def read_digit_str(str):
    # must be a digit string
    assert str.isdigit()

    return pattern.sub(repl, str)

# generators
def sequence():
    s = "1"
    while True:
        yield s
        s = read_digit_str(s)

for index, item in enumerate(sequence()):
    if index == 30:
        print(len(item))
        break

len(a[30]) = 5808

หวยฮานอย 说:
Jan 08, 2023 09:27:36 AM

I’m excited to uncover this page. I need to to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site. หวยฮานอย

Panenpoker 说:
Jan 19, 2023 02:35:36 AM

I need to to thank you for this very good read!! I definitely loved every little bit of it. I have you bookmarked to check out new things you post… Panenpoker

manly-manners.com 说:
Jan 19, 2023 11:38:18 PM

Well we really like to visit this site, many useful information we can get here. manly-manners.com

Buy Magic Mushrooms 说:
Jan 30, 2023 05:42:18 AM

Cool you write, the information is very good and interesting, I'll give you a link to my site. Albino Penis Envy Mushrooms

hkseo 说:
Nov 12, 2023 04:35:31 PM

I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well! Naz Tricks

hkseo 说:
Nov 20, 2023 12:07:07 AM

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. ottawa fat reduction


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter