pyhton习题20190201
#20190131
'''
检查ipv4的有效性,有效则返回true,否则返回false,(提示使用split函数进行分割)
'''
import os
def print_ping_ip(ip):
s = os.system('ping '+ip)
if s == 0:
return true
else:
return false
'''
检测密码强度
c1 : 长度>=8
c2: 包含数字和字母
c3: 其他可见的特殊字符
强:满足c1,c2,c3
中: 只满足任一2个条件
弱:只满足任一1个或0个条件
'''
import string
def print_mima_jianyan(stra):
flag = 0
num = 0
digit = 0
if len(stra) >= 8:
flag = 1
for i in stra:
if i in string.ascii_letters:
num = 1
elif i in string.digits:
digit = 1
elif i in string.punctuation:
flag += 1
#num_sum = num+digit
result = num+digit+flag
if (result <= 1) or (flag == 0 and num+digit == 2):
print("弱:只满足任一1个或0个条件")
if (flag >= 2 and num+digit <= 1) or (flag == 1 and num+digit == 2):
print("中: 只满足任一2个条件")
if flag >= 2 and digit+num >=2:
print("强:满足c1,c2,c3")
'''
求两个集合的交集和并集
'''
def print_jiaoji_bingji(lista,listb):
num_lista = []
num_listb = []
for i in lista:
if i in listb:
num_lista.append(i)
else:
num_listb.append(i)
for j in listb:
if j not in num_lista:
num_listb.append(j)
print(lista)
print(listb)
print("交集:",num_lista)
print(num_listb)
print("并集:",num_lista+num_listb)
'''
判断一个字符串是否为回文字符串
'''
def print_huiwen(stra):
if stra[::] == stra[::-1]:
print(stra," 是回文")
else:
print("不是回文!!")
#20190201
'''
不区分大小写对包含多个字符串对象的列表进行排序,显示排序后的结果还需要显示大小写不变的原字符串
'''
def print_str_sort(strlist):
print("原字符:",strlist)
strlist.sort()
print("排序后的字符:",strlist)
'''
一个数如果恰好等于它的因子之和,这个数就称为完数,例如6的因子为1,2,3,而6=1+2+3,因此6是完数,编程找出1000之内的所有完数,并按6 its factors are 1,2,3这样的格式输出
'''
def print_wangshu():
num_dict = {}
num = 0
for i in range(1001):
for j in range(1,i):
if i%j == 0 and i not in num_dict.keys():
num_dict[i] =[j]
elif i%j == 0 and i in num_dict.keys():
num_dict[i].append(j)
for k,v in num_dict.items():
if sum(v) == k:
print(k," its factors are ",v)
'''
写一个函数,识别输入字符串是否是符合 python 语法的变量名
(不能数字开头、只能使用数字和字母以及‘_’)
'''
import string
def print_variable():
num = input("请输入一个变量名:")
if num[0] in string.digits:
return "python变量名不能以数字开头"
for i in num:
if (i not in string.digits) and (i not in string.ascii_letters) and (i not in '_'):
return "只能使用数字和字母以及‘_’"
return "符合python变量命名"
'''
一个句子中的所有数字和标点符号删除
'''
def print_str_del_digit_punctuation(stra):
num = ''
for i in stra:
if (i not in string.digits) and (i not in string.punctuation):
num += i
print("原来的字符:",stra)
print("筛选后的字符:",num)
'''
自定义实现strip()---只能移除头尾指定的字符串
'''
def print_strip(stra,sep):
num = ''
if stra[0] == sep:
num = stra[1:]
elif stra[-1] == sep:
del stra[-1]
print(stra)
上一篇: 冬季预防疾病全攻略 早睡晚起勤喝水
下一篇: PHP $_SERVER详解