19 练习题:re模块、递归函数

# 1、匹配一篇英文文章的标题 类似 The Voice Of China
import re
exp = 'The Voice Of China'
ret = re.match('([A-Z1-9][a-z1-9]* )*[A-Z1-9][a-z1-9]*', exp)
print(ret)  # <_sre.SRE_Match object; span=(0, 18), match='The Voice Of China'>


# 2、匹配一个网址
import re
ret = re.match('((https|http|ftp|rtsp|mms)?://)[^\s]+', 'http://www.baidu.com')
print(ret)  # <_sre.SRE_Match object; span=(0, 20), match='http://www.baidu.com'>


# 3、匹配年月日日期 类似 2018-12-06 2018/12/06 2018.12.06
# (20|19)\d{2}(?P<sub>[^\d])(1[0-2]|0?[1-9])(?P=sub)([12][0-9]|3[01]|0?[1-9])


# 4、匹配15位或者18位身份证号
# ^([1-9]\d{14})(\d{2}[\dx])?$


# 5、从lianjia.html(html文件在群文件中)中匹配出标题,户型和面积,结果如下:
# [('金台路交通部部委楼南北大三居带客厅 单位自持物业', '3室1厅', '91.22平米'), ('西山枫林 高楼层南向两居 户型方正 采光好', '2室1厅', '94.14平米')]
import re
with open('lianjia.html', encoding='utf-8',mode='r') as file_handler:
    ret = file_handler.read()
res = re.findall('<a.*?data-sl="">(?P<introduction>.*?)</a>.*?'
               ' <span class="divide">/</span>(?P<divide>.*?)<span class="divide">/</span>(?P<area>.*?)<span class="divide">/</span>', ret, re.S)
print(res)



# 6、1-2*((60-30+(-40/5)(9-25/3+7/399/42998+10568/14))-(-43)/(16-32))
# 从上面算式中匹配出最内层小括号以及小括号内的表达式
import re
exp = '1-2*((60-30+(-40/5)(9-25/3+7/399/42998+10568/14))-(-4*3)/(16-32))'
ret = re.findall('\([^()]*?\)',exp)
print(ret)  # ['(-40/5)', '(9-25/3+7/399/42998+10568/14)', '(-4*3)', '(16-32)']


# 7、从类似9-25/3+7/399/42998+10*568/14的表达式中匹配出乘法或除法
import re
exp = '9-25/3+7/399/42998+10*568/14'
ret = re.findall('\d+[/*]\d+', exp)
print(ret)
# 有一个问题:正则表达式好像不能匹配重复的字符



# 8、完成递归相关的题目、通读博客,完成三级菜单
# 8.1 计算阶乘
def factorial(i):
    if i == 1:
        return 1
    return factorial(i - 1) * i


# print(factorial(4))

# 8.2 os模块:查看一个文件夹下的所有文件,这个文件夹下面还有文件夹,不能用walk
import os


def read_dir(dirname):
    # if os.listdir(dirname) == []:
    #     return
    for i in os.listdir(dirname):
        if os.path.isfile(os.path.join(dirname, i)):
            print(i)
        if os.path.isdir(os.path.join(dirname, i)):
            read_dir(os.path.join(dirname, i))


# read_dir('day20')

# 8.3 计算一个文件夹下所有文件的大小,这个文件夹下面还有文件夹,不能用walk
import os


def read_dirsize(dirname, size=0):
    if os.listdir(dirname) == []:
        return size
    for i in os.listdir(dirname):
        size += os.path.getsize(os.path.join(dirname, i))
        # print(f'{os.path.join(dirname, i)}>>size>>{os.path.getsize(os.path.join(dirname, i))}')
        if os.path.isdir(os.path.join(dirname, i)):
            size += read_dirsize(os.path.join(dirname, i), size)
    return size


print(read_dirsize(r'day09'))

# 8.4计算斐波那契数列
import time

# 定义了一个log_interval装饰器来研究下列几种方法的时间复杂度
def log_interval(func): 
    def inner(*args, **kwargs):
        stamp1 = time.time()
        ret = func(*args, **kwargs)
        stamp2 = time.time()
        interval = stamp2 - stamp1
        print(f'interval:{interval}')
        return ret

    return inner


# 递归方法1
@log_interval
def Fibonacci(n):
    def inner(args):
        if args == 1 or args == 0:
            return 1
        return inner(args - 1) + inner(args - 2)

    r = inner(n)
    return r


# print(Fibonacci(30)  # interval:0.4075314998626709

# 递归方法2
@log_interval
def Fibonacci(n):
    def inner(args, a=1, b=1):
        if args == 0:
            return a
        a, b = b, a + b
        return inner(args - 1, a, b)

    return inner(n)


# print(Fibonacci(50))  # interval:0.0010001659393310547


# 非递归方法
@log_interval
def Fibonacci(n):
    a = 1
    b = 1
    for i in range(n):
        a, b = b, a + b
    return a


# print(Fibonacci(50000))  # interval:0.044003963470458984
# 生成器方法
@log_interval
def Fibonacci(n):
    i = 0

    def inner(args):
        a = 1
        yield a
        b = 1
        for i in range(args):
            a, b = b, a + b
            yield a

    for i in inner(n):
        pass
    return i


# print(Fibonacci(50000))  # interval:0.04400515556335449
# 8.5 三级菜单
menu = {
    '北京': {
        '海淀': {
            '五道口': {
                'soho': {},
                '网易': {},
                'google': {}
            },
            '中关村': {
                '爱奇艺': {},
                '汽车之家': {},
                'youku': {},
            },
            '上地': {
                '百度': {},
            },
        },
        '昌平': {
            '沙河': {
                '老男孩': {},
                '北航': {},
            },
            '天通苑': {},
            '回龙观': {},
        },
        '朝阳': {},
        '东城': {},
    },
    '上海': {
        '闵行': {
            "人民广场": {
                '炸鸡店': {}
            }
        },
        '闸北': {
            '火车战': {
                '携程': {}
            }
        },
        '浦东': {},
    },
    '山东': {},
}


def read_menu(menu):
    if menu == {}:
        return False
    flag = True
    while flag:
        for i in menu:
            print(i)
        select = input('input>>').strip()
        if select.upper() == 'B':
            return True
        if select.upper() == 'Q':
            flag = False
        elif select in menu:
            flag = read_menu(menu[select])
        else:
            print('ILLEGAL INPUT')
    return False


read_menu(menu)
posted @ 2020-07-17 22:20  Raigor  阅读(140)  评论(0编辑  收藏  举报