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)
pyhton习题20190201的更多相关文章
- Pyhton核心编程-Chap2习题-DIY
在学Python,在看<Python核心编程>的pdf,做了Chap2的题目,答案为DIY # Filename: 2-11.py # Author: ChrisZZ mylist = [ ...
- pyhton 核心编程 正则表达式习题
方案一 import re #1. 识别下列字符串:“bat,” “bit,” “but,” “hat,” “hit,” 或 “hut” import re def test1(self): bt = ...
- 001_02-python基础习题答案
python 基础习题 执行 Python 脚本的两种方式 如:脚本/python/test.py 第一种方式:python /python/test.py 第二中方式:在test.py中声明:/us ...
- Python基础语法习题一
Part 1 习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型 2.执行 Python 脚本的两种方式是什么 3.Pyhton 单行注释和多行注释分别用什么 ...
- Sharepoint学习笔记—习题系列--70-576习题解析 --索引目录
Sharepoint学习笔记—习题系列--70-576习题解析 为便于查阅,这里整理并列出了70-576习题解析系列的所有问题,有些内容可能会在以后更新. 需要事先申明的是: 1. ...
- 《python核心编》程课后习题——第三章
核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...
- 习题 5: 更多的变量和打印 | 笨办法学 Python
一. 简述 “格式化字符串(format string)” - 每一次你使用 ' ’ 或 " " 把一些文本引用起来,你就建立了一个字符串. 字符串是程序将信息展示给人的方式. ...
- 【WebGoat习题解析】Parameter Tampering->Bypass HTML Field Restrictions
The form below uses HTML form field restrictions. In order to pass this lesson, submit the form with ...
- python核心编程(第二版)习题
重新再看一遍python核心编程,把后面的习题都做一下.
随机推荐
- laravel with嵌套的渴求式加载
今天在通过需求表A查询场地类型表B,然后通过表B的场地类型id去查询表C场地类型名的时候遇到了一个小的问题. 需求表A的字段:id.user_id .name等等: 中间表B的字段:id.appeal ...
- July 31st 2017 Week 31st Monday
Elegance is the only beauty that never fades. 优雅是唯一不会褪色的美. Even the most beautiful apperace would be ...
- PetaPoco轻量级ORM框架 - Database API 手册
PetaPoco Database API #region IDisposable public void Dispose() #endregion #region Constructors publ ...
- 随手练——LintCode 433 - 小岛数量
LintCode 433: https://www.lintcode.com/problem/number-of-islands/description LintCode 434: https://w ...
- No.7 - 使用 animate.css 实现一个优雅的登录框
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- leetcode64. Minimum Path Sum
这个题是从左上角到右下角的路径和最小,实际就是一道dp题. 第一种写法是只初始化(0,0)位置,第二种写法则是把第一行.第一列都初始化了.个人更喜欢第二种写法,简单一点. dp的右下角的值就为最终的值 ...
- rand7生成rand10,rand1生成rand6,rand2生成rand5(包含了rand2生成rand3)
这种题要分两步,第一步是“插空儿”,第二步是“筛” 1.rand7生成rand10 只要是10的倍数就好 int rand10() { int num; do{ num = (rand7() - ) ...
- PAT——1060. 爱丁顿数
英国天文学家爱丁顿很喜欢骑车.据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数”E,即满足有E天骑车超过E英里的最大整数E.据说爱丁顿自己的E等于87. 现给定某人N天的骑车距离,请你算出对应的爱 ...
- 【luogu P1608 路径统计】 题解
题目链接:https://www.luogu.org/problemnew/show/P1608 补上一发最短路计数! 感谢王强qwqqqq @Lance1ot #include <queue& ...
- Genymotion集成到Eclipse
在Eclipse中使用Genymotion Google的ADT中自带的模拟器速度太慢,可以使用Genymotion代替.关于Genymotion的安装方法,可以直接访问官网,需要注册账号,因为创建模 ...