num = input("输个数字进来呗,帮你翻转过来哦:")
print(num[::-1])
第二题用了过滤器,写复杂了
# 写复杂了,不如用推导式
a =
b = list(filter(lambda x: x%2, a))
print(*(b if len(b)< len(a)/2 else set(a).difference(set(b))))
第三题用字符串的方法
# 写成这样也可以
def validate_pin(string):
return True if string.isdigit() and len(string) in else False 本帖最后由 bozhen 于 2017-8-14 13:04 编辑
第一题:
num_str = input('请输入一串数字:')
num_list = list(num_str)
num_list.sort(reverse = True) # 从大到小排序
print(*num_list)
第二题:
def getOdd_Even(list_num):
list_odd = []
list_even = []
for each in list_num:
if not each % 2 :
list_even.append(each)
else:
list_odd.append(each)
if len(list_even) == 1: # 我理解的题意是不同的数字只有一个,如果非一个,则比较len(list_even) 与len(list_odd)的大小,输入小的那个序列
return print(*list_even)
if len(list_odd) == 1:
return print(*list_odd)
list_num =
getOdd_Even(list_num)
第三题:
def checkPasswd(string):
lenth = len(string)
if (lenth == 4 or lenth == 6)and string.isdigit():
return True
else:
return False
passwd = ['1234','12345','a234']
for each in passwd:
if checkPasswd(each): # 如果返回值为True,则打印
print(each)
tmp =
L1 =
L2 =
result = L1 if len(L1)<len(L2) else L2
print(*result) 的 学习一下 01
def decending(num):
st = str(num)
lis = list(st)
lis.sort()
lis.reverse()
st = ''
for i in lis:
st = st + ''.join(i)
return st
n = int(input('请输入自然数:\n'))
print(decending(n))
02
def different(lis):
lis1 = []
for i in lis:
if i % 2 == 0:
lis1.append(0)
else:
lis1.append(1)
if lis1.count(0) < lis1.count(1):
return lis
else:
return lis
lst1 =
lst2 =
print(different(lst1))
print(different(lst2)) 03
def validate_pin(st):
if st.isdigit() == True and (len(st) == 4 or len(st) == 6):
return 'True'
else:
return 'False'
st1 = input('please input your pin:\n')
print(validate_pin(st1)) 还可以
## 数字反序
x = input('Input:')
if x.isdigit():
x = x[::-1]
print('Output:%s' %x)
else:
print('输入有误!')
## 选出与其他数字奇偶性不同的奇(偶)数
x = input('请输入数组:')
x = list(map(int,x.split()))
a = []
b = []
for i in range(len(x)):
if x % 2 == 0:
a.append(x)
else:
b.append(x)
if len(a) == 1:
print(a)
elif len(b) == 1:
print(b)
else:
print('输入有误!')
## 因为银行的密码通常是4位或者6位纯数字,那么请你写个函数,来判断输入的字符串是否符合4位或者6位纯数字要求。
while True:
x = input('请输入密码:')
if x.isdigit():
if len(x) == 4 or len(x) == 6 :
print('True')
break
else:
print('密码必须是四位或八位!')
else:
print('密码须为纯数字!')
'''第一个,数字反序'''
num = list(input('Pls enter a number:'))
num.sort()
num.reverse()
print(''.join(num))
'''第二个,选出与其他数字奇偶性不同的奇(偶)数。'''
num=list(input('请输入几个整数,我可以选出其中奇偶不同的一个:').split())
num =
ji=[]
ou=[]
for each in num:
if each%2==0:
ou.append(each)
else:
ji.append(each)
if len(ji)>len(ou):
print(ou)
else:
print(ji)
'''因为银行的密码通常是4位或者6位纯数字,那么请你写个函数,
来判断输入的字符串是否符合4位或者6位纯数字要求。'''
def validate_pin(code):
a = 0
for each in code:
if each.isnumeric():
a+=1
else:
break
if a==4 or a==6:
return True
else:
return False
print(validate_pin('1234'))
print(validate_pin("12345"))
print(validate_pin("a234"))
感谢 1
nums = list(input("Input:"))
nums.reverse()
a = ''
for i in nums:
a += i
print("Output: %s"%a)
2
nums = input("输入一组数字(有且只有一个奇数或偶数,以,分隔每个数字): ").split(',')
j = []
o = []
for i in nums:
i = int(i)
if i % 2 == 0:
o.append(i)
if i % 2 != 0:
j.append(i)
if len(j) == 1:
print("Should return:%d"%j)
if len(o) == 1:
print("Should return:%d"%o)
3
def validate_pin(n):
if n.isdigit() and (len(n) == 4 or len(n) == 6):
return True
else:
return False lambda x:int(str(x)[::-1])def func(l):
a=
return aif len(a)==1 else lambda x:True if x.isdigit and(len(str(x))in(4,6))else False int func(int n) {
int res = 0;
while (n) {
res += n % 10;
n /= 10;
}
return res;
} 第一题:
# 数字反序
num_list = []
num = input('Input:')
for each in num:
num_list.append(each)
num_list.reverse()
print('Output:',end='')
for each in num_list:
print(each,end='')
第二题:
# 判别奇偶
list_num = list(input('请输入一列数据:').split(','))
num_odd = []
num_even = []
for each in list_num:
if int(each)%2 == 0:
num_even.append(each)
else:
num_odd.append(each)
print('should return:',end='')
if len(num_odd) == 1:
print('%s'%num_odd)
else:
print('%s'%num_even)
第三题:
# 判断密码格式正确与否
def validate_pin(n):
pin_length = len(n)
if not n.isdigit():
print('validate_pin("%s")'%n,'==False')
else:
if pin_length == 4 or pin_length == 6:
print('validate_pin("%s")'%n,'== True')
else:
print('validate_pin("%s")'%n,'==False')
validate_pin('a11111')
validate_pin('1209888')
validate_pin('2341')
validate_pin('123456')
结果:
validate_pin("a11111") ==False
validate_pin("1209888") ==False
validate_pin("2341") == True
validate_pin("123456") == True 第一题:
print(input('请输入字符串:')[::-1]) s = list(raw_input())
s.reverse()
print "".join(s)
页:
1
[2]