#1.正则表达式计算 origin = "1 - 2 * ( ( 60 - 30 + ( -40.0 / 5 ) * ( 9 - 2 * 5 / 3 + 7 / 3 * 99 / 4 * 2998 + 10 * 568 / 14 )) - ( - 4 * 3 ) / ( 16 - 3 * 2))"

import re
import functools def checkInput(formula):
"""检测输入合法与否,是否包含字母等非法字符"""
return not re.search("[^0-9+\-*/.()\s]",formula) def formatInput(formula):
"""标准化输入表达式,去除多余空格等"""
formula = formula.replace(' ','')
formula = formula.replace('++', '+')
formula = formula.replace('+-', '-')
formula = formula.replace('-+', '-')
formula = formula.replace('--', '+')
return formula def mul_divOperation(s):
"""乘法除法运算"""
# 1-2*-14969036.7968254
s = formatInput(s)
sub_str = re.search('(\d+\.?\d*[*/]-?\d+\.?\d*)', s)
while sub_str:
sub_str = sub_str.group()
if sub_str.count('*'):
l_num, r_num = sub_str.split('*')
s = s.replace(sub_str, str(float(l_num)*float(r_num)))
else:
l_num, r_num = sub_str.split('/')
s = s.replace(sub_str, str(float(l_num) / float(r_num)))
#print(s)
s = formatInput(s)
sub_str = re.search('(\d+\.?\d*[*/]\d+\.?\d*)', s) return s def add_minusOperation(s):
"""加法减法运算
思路:在最前面加上+号,然后正则匹配累加
"""
s = formatInput(s)
s = '+' + s
#print(s)
tmp = re.findall('[+\-]\d+\.?\d*', s)
s = str(functools.reduce(lambda x, y:float(x)+float(y), tmp))
#print(tmp)
return s def compute(formula):
"""无括号表达式解析"""
#ret = formula[1:-1]
ret = formatInput(formula)
ret = mul_divOperation(ret)
ret = add_minusOperation(ret)
return ret def calc(formula):
"""计算程序入口"""
has_parenthesise = formula.count('(')
if checkInput(formula):
formula = formatInput(formula)
while has_parenthesise:
sub_parenthesise = re.search('\([^()]*\)', formula) #匹配最内层括号
if sub_parenthesise:
#print(formula+"...before")
formula = formula.replace(sub_parenthesise.group(), compute(sub_parenthesise.group()[1:-1]))
#print(formula+'...after')
else:
#print('没有括号了...')
has_parenthesise = False ret = compute(formula)
print('结果为:')
return ret else:
print("输入有误!")
-------------------------------------------------------------------------------------------
def add(args): #加减
args = args.replace(' ', '')
args = args.replace('++', '+')
args = args.replace('+-', '-')
args = args.replace('-+', '-')
args = args.replace('--', '+')
tmp = re.findall("([+\-]?\d+\.?\d*)", args)
ret = 0
for i in tmp:
ret = ret + float(i)
return ret def mul(args):
#乘除
while True:
ret = re.split("(\d+\.?\d*[\*/][\+-]?\d+\.?\d*)",args,1)
if len(ret) == 3:
a = ret[0]
b = ret[1]
c = ret[2]
if "*" in b:
num1,num2 = b.split("*")
new_b = float(num1) * float(num2)
args = a + str(new_b) + c
elif "/" in b:
num1, num2 = b.split("/")
new_b = float(num1) / float(num2)
args = a + str(new_b) + c
else:
return add(args)
def calc(args):
while True:
args = args.replace(" ", "")
ret = re.split("\(([^()]+)\)",args,1)
if len(ret) == 3:
a,b,c = ret
r = mul(b) #调用乘除得到计算结果
args = a + str(r) + c
else:
return mul(args) print(calc(origin))
print(eval(origin)) #2.将“我我我、、、我我、、我要、我要要、、、要要要、、要要、、学学学、、、、学学编、、、学编编编、、编编编程、、程程”还原成:我要学编程
f = "我我我、、、我我、、我要、我要要、、、要要要、、要要、、学学学、、、、学学编、、、学编编编、、编编编程、、程程" c = ''
import re
a = re.sub(r'、','',f)
for i in a:
if i not in c:
c += i
print(c) #3.查找IP地址
temp = "192.168.1.200 10.10.10.10 3.3.50.3 127.0.0.1 244.255.255.249 273.167.189.222" # sel = re.compile(r'((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))')
# 参考:https://www.cnblogs.com/brogong/p/7929298.html
f = re.compile(r"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")
ip = f.findall(temp)
print(ip) #4.读写用户的输入,根据用户输入,创建一个相应的目录
improt os,sys
os.mkdir(sys.argv[1]) #5.进度条,显示百分比
import sys,time,datetime
#
for i in range(1,101):
if i == 100:
b = '完成'
else:
b = '进度中'
for a in ['\\','|','/','=']:
sys.stdout.write("\r")
sys.stdout.write(r'%s%s%s %s%% ' % (b,int(i/100*100)*'=',a,int(i/100*100)))
sys.stdout.flush()
time.sleep(0.3) #6.密码加密版,用户登录
import hashlib
def md5(pawd):
hash = hashlib.md5(bytes('fana,*=',encoding='utf-8'))
hash.update(bytes(pawd,encoding='utf-8'))
return hash.hexdigest() def zhuce(user,pawd):
with open('fana.db','a',encoding='UTF-8') as f:
tmp = user + '|' + md5(pawd) + '\n'
f.write(tmp) def denglu(user,pawd):
with open('fan.db','r',encoding='UTF-8') as f:
for line in f:
u,p = line.strip().split('|')
if u == user and p == md5(pawd):
return True
return False i = input('登陆按1,注册按2:')
if i == '2':
user = input('用户名:')
pawd = input('密码:')
zhuce(user,pawd)
print('注册成功')
if i == '1':
user = input('用户名:')
pawd = input('密码:')
re = denglu(user,pawd)
if re:
print('登陆成功')
else:
print('登陆失败')

python--第六天练习题的更多相关文章

  1. python入门练习题1

    常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...

  2. Python/ MySQL练习题(一)

    Python/ MySQL练习题(一) 查询“生物”课程比“物理”课程成绩高的所有学生的学号 SELECT * FROM ( SELECT * FROM course LEFT JOIN score ...

  3. python/MySQL练习题(二)

    python/MySQL练习题(二) 查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T ...

  4. Python第六天 类型转换

    Python第六天   类型转换 目录 Pycharm使用技巧(转载) Python第一天  安装  shell  文件 Python第二天  变量  运算符与表达式  input()与raw_inp ...

  5. python字典练习题

    python字典练习题 写代码:有如下字典按照要求实现每一个功能dict = {"k1":"v1","k2":"v2", ...

  6. 孤荷凌寒自学python第六天 列表的嵌套与列表的主要方法

    孤荷凌寒自学python第六天 列表的嵌套与列表的主要方法 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) (同步的语音笔记朗读:https://www.ximalaya.com/keji/1 ...

  7. Python经典练习题1:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

    Python经典练习题 网上能够搜得到的答案为: for i in range(1,85): if 168 % i == 0: j = 168 / i; if i > j and (i + j) ...

  8. Python模块练习题

    练习题: 1.logging模块有几个日志级别? #INFO,WARNING,DEBUG,CRITICAL,ERROR 2.请配置logging模块,使其在屏幕和文件里同时打印以下格式的日志 2017 ...

  9. Python程序练习题(一)

    Python:程序练习题(一) 1.2 整数序列求和.用户输入一个正整数N,计算从1到N(包含1和N)相加之后的结果. 代码如下: n=input("请输入整数N:") sum=0 ...

  10. 系统学习python第六天学习笔记

    1.补充 1.列表方法补充 reverse,反转. v1 = [1,2,3111,32,13] print(v1) v1.reverse() print(v1) sort,排序 v1 = [11,22 ...

随机推荐

  1. axios的二次封装

    'use strict' import axios from 'axios' import qs from 'qs' var host = "https://www.easy-mock.co ...

  2. 关于财务YT知识点

    1 YT 将今年剩余的未花完的money做YT,生成一个YT号,用在下一年使用的机制. 2 生成YT的方式 2.1 PR生成YT 2.2 PO生成YT 2.3 TR生成YT 2.4 预算直接生成YT ...

  3. (十一)springmvc和spring的整合

    1:Maven引入相关的jar包. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  4. Thymeleaf 模板使用 Error resolving template "/home", template might not exist or might not be accessible by any of the

    和属性文件中thymeleaf模板的配置相关 1.配置信息 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix= ...

  5. 利用Supervisor 管理自己部署的应用程序

    首先,在centos7下安装supervisor yum install python-setuptools easy_install supervisor 然后新建配置文件 #新建superviso ...

  6. 三、eureka服务端获取服务列表

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 eureka服务端维护了一个服务信息的列表,服务端节点之间相互复制服务信息.而作为eur ...

  7. 通过数组的某一个属性值进行排序(如id)

    let arr = [ {id: 1, name: 'aaa'}, {id: 4, name: 'ddd'}, {id: 2, name: 'bbb'}, {id: 3, name: 'ccc'} ] ...

  8. 【转载】salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解

    salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解   建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schem ...

  9. #LOF算法

    a.每个数据点,计算它与其他点的距离 b.找到它的K近邻,计算LOF得分 clf=LocalOutlierFactor(n_neighbors=20,algorithm='auto',contamin ...

  10. springboot项目命linux环境下命令启动

    测试环境:dev nohup java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 \-Dcom.s ...