1.判断一个ip是否合格
(1)ip是点分十进制,十进制有四段,不能有空格
(2)ip每段十进制数字范围[0-255]
ip = raw_input('ip:')
if len(ip.split('.')) !=4:
print 'no'
exit(0)
li = [ i.isspace() for i in ip]
if li.count(True)>0: ##li列表存放的是False(不是空格)或者True(为空格)
print 'No'
else:
sip = [int(i) for i in ip.split('.')]
for j in sip:
if not 0<=j<=255:
print 'NO'
break
else:
print 'YES'
2.函数式编程考察
用 filter()进行函数式编程,写一段代码来给出一个年份的列表并返回一个只有闰年的列表。列表解析式实现方式呢?
def fun(*args):
for i in args:
if (i % 4 ==0 and i % 100 !=0) or i % 400 ==0:
return True
else:
return False
t = (1994,1995,1996,2000,2004,2016,2017)
print filter(fun,t)
或
t=(1994,1995,1996,2000,2004,2016,2017)
leap = [ i for i in t if (i % 4 == 0 and i % 100 != 0) or i % 400 == 0 ]
print leap
或
leap = [ i for i in (1994,1995,1996,2000,2004,2016,2017)if (i % 4 == 0 and i % 100 != 0) or i % 400 == 0 ]
print leap
3.题目描述
将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符
- 输入描述:将一个英文语句以单词为单位逆序排放。
- 输出描述:得到逆序的句子
示例1
- 输入
I am a boy
- 输出
boy a am I
s= raw_input('str:')
print " ".join(s.split()[::-1])
4. 数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,
输出按照key值升序进行输出。
- 输入描述:先输入键值对的个数,然后输入成对的index和value值,以空格隔开
- 输出描述:输出合并后的键值对(多行)
print "输入"
count = input() ###确认输入keys-values对数
d={} ##新建空字典存储
for i in range(count):
index,value=map(int,raw_input().split()) ##map高阶函数,int 表示后边输入的字符串转化int型,
if index in d:
d[index]+=value
else:
d[index] =value
print '输出'
for k,v in d.items():
print k,v
5.用户登陆系统version 3
#!/usr/bin/env python
#coding:utf-8
userinfo = {
'root':{
'name':'root',
'passwd':'root',
'age':'12',
'gender':'M',
'email':'root@qq.com'
},
}
info = """
********用户管理系统**********
1.用户登陆
2.注册用户
3.查询用户信息
4.注销登陆
5.删除用户
6.退出系统
"""
print info
def login():
try:
country = 0
print '欢迎登陆'.center(50, '*')
loginuse = raw_input('登陆名:')
if loginuse in userinfo:
while country < 3:
loginps = raw_input('登陆密码:')
country += 1
if loginps == userinfo[loginuse]['passwd']:
print '登陆成功!'
break
else:
print '登陆密码输入错误,请重新输入!'
else:
print '输入密码错误超过三次'
else:
print '无此用户!'
except:
print '非法操作'
def create():
print "\n\t\t\t\t欢迎注册\n".center(100, "-")
while 1:
createuser = raw_input("*注册用户名:")
if createuser in userinfo:
print "%s用户名已经存在,请重新输入!" % createuser
else:
while 1:
createpasswd = raw_input("*请设置密码:")
if 1 < len(createpasswd) < 8:
print "密码设置成功!"
break
else:
print "密码必须大于8位小于16位!"
continue
break
createage = raw_input("年龄:")
if not createage:
createage = 0
creategender = raw_input("性别:")
if not creategender:
creategender = None
createemail = raw_input("邮箱:")
if not createemail:
createemail = None
userinfo[createuser] = {
'name': createuser,
'passwd': createpasswd,
'age': createage,
'gender': creategender,
'email': createemail
}
def look():
for k, v in userinfo.items():
print k, v
def logout():
print "用户正在注销..."
return login()
while True:
chioce = raw_input("请选择:")
if chioce == "1":
login()
elif chioce =='2':
create()
elif chioce =='3':
look()
elif chioce =='4':
logout()
elif chioce =='5':
deluser=raw_input('请选择你要删除的用户名:')
if deluser in userinfo:
delpd=raw_input('请输入要删除用户的密码:')
if delpd == userinfo[deluser]['passwd']:
userinfo.pop(deluser)
print '%s删除成功' %deluser
else:
print '删除-密码错误...'
else:
print '无法删除%s,无此用户' %deluser
elif chioce =='6':
print 'Bye-bye'
exit(0)
6.求fib数
from collections import Iterable
class Fib(object):
def __init__(self):
self.a=0
self.b=1
def __iter__(self):
return self
def next(self):
self.a,self.b = self.b,self.a+self.b
return self.b
f=Fib()
for i,j in enumerate(f):
if i >9:
break
print j