Python​ 重解零基础100题(2)

简介: Python​ 重解零基础100题(2)

第11题


难度:2级


问题:编写一个程序,接收一系列以逗号分隔的4位二进制数作为输入,然后检查它们是否可被5整除,可被5整除的数字将以逗号分隔的顺序打印。


例:0100,0011,1010,1001

那么输出应该是:1010

提示:如果输入数据被提供给问题,则应该假定它是控制台输入。

value = []
print('请输入逗号分隔的4位二进制数:')
items=[x for x in input().split(',')]
for p in items:
    intp = int(p, 2)
    # print(intp)
    if not intp%5:
        value.append(p)
print (','.join(value))



我的答案:

>>> print(','.join([_ for _ in input('输入一组二制数:').split(',') if int(_,2)%5==0]))
0100,0011,1010,1001
1010
>>> 
>>> print(','.join([_ for _ in input('输入一组二制数:').split(',') if int(_,2)%5==0]))
0101,0111,1010,1111
0101,1010,1111
>>> 




第12题


难度:2级


问题:编写一个程序,找到1000到3000之间并且所有位数均为偶数的所有数字,比如2000,2002等;获得的数字都以逗号分隔的顺序,打印在一行上。


提示:如果输入数据被提供给问题,则应该假定它是控制台输入。


values = []
for i in range(1000, 3001):
    s = str(i)
    if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0):
        values.append(s)
print (",".join(values))


我的答案:

>>> [i for i in range(1000,3001) if all([int(_)%2==0 for _ in str(i)])]
[2000, 2002, 2004, 2006, 2008, 2020, 2022, 2024, 2026, 2028, 2040, 2042,
 2044, 2046, 2048, 2060, 2062, 2064, 2066, 2068, 2080, 2082, 2084, 2086,
 2088, 2200, 2202, 2204, 2206, 2208, 2220, 2222, 2224, 2226, 2228, 2240,
 2242, 2244, 2246, 2248, 2260, 2262, 2264, 2266, 2268, 2280, 2282, 2284,
 2286, 2288, 2400, 2402, 2404, 2406, 2408, 2420, 2422, 2424, 2426, 2428,
 2440, 2442, 2444, 2446, 2448, 2460, 2462, 2464, 2466, 2468, 2480, 2482,
 2484, 2486, 2488, 2600, 2602, 2604, 2606, 2608, 2620, 2622, 2624, 2626,
 2628, 2640, 2642, 2644, 2646, 2648, 2660, 2662, 2664, 2666, 2668, 2680,
 2682, 2684, 2686, 2688, 2800, 2802, 2804, 2806, 2808, 2820, 2822, 2824,
 2826, 2828, 2840, 2842, 2844, 2846, 2848, 2860, 2862, 2864, 2866, 2868,
 2880, 2882, 2884, 2886, 2888]
>>> 



第13题


难度:2级


问题:编写一个接受句子并计算字母和数字的程序。


假设程序输入:Hello world! 123

则输出应该是:字母10 数字3


提示:如果输入数据被提供给问题,则应该假定它是控制台输入。


print('请输入:')
s = input()
d={"DIGITS":0, "LETTERS":0}
for c in s:
    if c.isdigit():
        d["DIGITS"]+=1
    elif c.isalpha():
        d["LETTERS"]+=1
    else:
        pass
print ("字母", d["LETTERS"])
print ("数字", d["DIGITS"])


我的答案:

>>> s='Hello world! 123'
>>> sum(1 for _ in s if _.isalpha())
10
>>> sum(1 for _ in s if _.isnumeric())
3
>>> 
# 输入输出格式,略



第14题


难度:2级


问题:编写一个接收句子的程序,并计算大写字母和小写字母的数量。

假设为程序提供了以下输入:Hello world!

则输出应该是:UPPER CASE 1;LOWER CASE 9


提示:如果输入数据被提供给问题,则应该假定它是控制台输入。


print('请输入:')
s = input()
d={"UPPER CASE":0, "LOWER CASE":0}
for c in s:
    if c.isupper():
        d["UPPER CASE"]+=1
    elif c.islower():
        d["LOWER CASE"]+=1
    else:
        pass
print ("UPPER CASE", d["UPPER CASE"])
print ("LOWER CASE", d["LOWER CASE"])


我的答案:

>>> s='Hello world!'
>>> sum(1 for _ in s if _.isupper())
1
>>> sum(1 for _ in s if _.islower())
9
>>> 
# 输入输出格式,略



第15题


难度:2级


问题:编写一个程序,计算a + aa + aaa + aaaa的值,给定的数字作为a的值。

假设为程序提供了以下输入:9;输出应该是:11106


提示:如果输入数据被提供给问题,则应该假定它是控制台输入。


print('请输入一个数字:')
a = input()
n1 = int( "%s" % a )
n2 = int( "%s%s" % (a,a) )
n3 = int( "%s%s%s" % (a,a,a) )
n4 = int( "%s%s%s%s" % (a,a,a,a) )
print (n1+n2+n3+n4)


我的答案:

1. >>> eval('a+aa+aaa+aaaa'.replace('a',input('请输入一位数字:')))
2. 请输入一位数字:9
3. 11106
4. >>>




第16题


难度:2级


问题:使用列表推导输出列表中的每个奇数,该列表由一系列逗号分隔的数字输入。

假设程序输入:1,2,3,4,5,6,7,8,9

输出应该是:1,3,5,7,9


print("输入:")
values = input()
numbers = [x for x in values.split(",") if int(x)%2!=0]
print (",".join(numbers))

略!



第17题


难度:2级


问题:编写一个程序,根据控制台输入的事务日志计算银行帐户的净金额。事务日志格式如下所示:

D 100 W 200

D表示存款,而W表示提款。

假设向程序依次输入:D 300;D 300;W 200;D 100;

则输出应该为:500

提示:如果输入数据被提供给问题,则应该假定它是控制台输入。

netAmount = 0
while True:
    print("请输入:")
    s = input()
    if not s:
        break
    values = s.split(" ")
    operation = values[0]
    amount = int(values[1])
    if operation=="D":
        netAmount+=amount
    elif operation=="W":
        netAmount-=amount
    else:
        pass
print (netAmount)

略!




第18题


难度:3级


问题:网站要求用户输入用户名和密码进行注册。编写程序以检查用户输入的密码有效性。


以下是检查密码的标准:


1 [a-z]之间至少有1个字母

2 [0-9]之间至少有1个数字

3 [A-Z]之间至少有一个字母

4 [$#@]中至少有1个字符

5 最短交易密码长度:6

6 交易密码的最大长度:12


您的程序接收一系列逗号分隔的密码,并将根据上述标准进行检查,将打印符合条件的密码,每个密码用逗号分隔。


例:如果以下密码作为程序的输入:ABd1234@1,a F1#,2w3E*,2We3345

则程序的输出应该是:ABd1234@1

import re
value = []
print("请输入:")
items=[x for x in input().split(',')]
for p in items:
    if len(p)<6 or len(p)>12:
        continue
    else:
        pass
    if not re.search("[a-z]",p):
        continue
    elif not re.search("[0-9]",p):
        continue
    elif not re.search("[A-Z]",p):
        continue
    elif not re.search("[$#@]",p):
        continue
    elif re.search("\s",p):
        continue
    else:
        pass
    value.append(p)
print (",".join(value))



我的答案:

>>> import re
>>> pat=['[a-z]','[0-9]','[A-Z]','[$#@]']
>>> password=['ABd1234@1','a F1#','2w3E*','2We3345']
>>> [s for s in password if 6<=len(s)<=12 and all([re.findall(p,s) for p in pat])]
['ABd1234@1']
>>> 
# 输入输出格式 略




第19题


难度:3级

问题:您需要编写一个程序,按升序对(名称,年龄,高度)元组进行排序,其中name是字符串,age和height是数字, 元组由控制台输入。


排序标准是:


1:根据名称排序;

2:然后根据年龄排序;

3:然后按分数排序。


优先级是name > age > 得分。


如果给出以下元组作为程序的输入:Tom,19,80;John,20,90;Jony,17,91;Jony,17,93;Json,21,85

然后,程序的输出应该是:[('John','20','90'),('Jony','17','91'),('Jony','17','93'),('Json','21 ','85'),('Tom','19','80')]

提示:使用itemgetter来启用多个排序键。


from operator import itemgetter, attrgetter
l = []
print("请输入:")
while True:
    s = input()
    if not s:
        break
    l.append(tuple(s.split(",")))
print (sorted(l, key=itemgetter(0,1,2)))


我的答案:

>>> s='Tom,19,80;John,20,90;Jony,17,91;Jony,17,93;Json,21,85'
>>> t=[tuple(t.split(',')) for t in s.split(';')]
>>> t
[('Tom', '19', '80'), ('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85')]
>>> t=[(i[0],int(i[1]),int(i[2])) for i in t]
>>> t
[('Tom', 19, 80), ('John', 20, 90), ('Jony', 17, 91), ('Jony', 17, 93), ('Json', 21, 85)]
>>> sorted(t,key=lambda x:(x[0],x[1],x[2]))
[('John', 20, 90), ('Jony', 17, 91), ('Jony', 17, 93), ('Json', 21, 85), ('Tom', 19, 80)]
>>> 
>>> # 合并成一行:
>>> sorted([(i[0],int(i[1]),int(i[2])) for i in [tuple(t.split(',')) for t in s.split(';')]],key=lambda x:(x[0],x[1],x[2]))
[('John', 20, 90), ('Jony', 17, 91), ('Jony', 17, 93), ('Json', 21, 85), ('Tom', 19, 80)]
>>> 




第20题


难度:3级

问题:使用生成器定义一个类,该生成器可以在给定范围0和n之间迭代可被7整除的数字。

提示:考虑使用yield。


def putNumbers(n):
    i = 0
    while i<n:
        j=i
        i=i+1
        if j%7==0:
            yield j
for i in putNumbers(100):
    print (i)


略!

目录
相关文章
|
存储 索引 Python
Python​ 重解零基础100题(10-2)
Python​ 重解零基础100题(10-2)
69 0
|
存储 Python
Python​ 重解零基础100题(10)
Python​ 重解零基础100题(10)
68 0
|
索引 Python
Python​ 重解零基础100题(9)
Python​ 重解零基础100题(9)
73 0
|
索引 Python
Python​ 重解零基础100题(8)
Python​ 重解零基础100题(8)
84 0
|
Python
Python​ 重解零基础100题(7)
Python​ 重解零基础100题(7)
117 0
|
Python
Python​ 重解零基础100题(6)
Python​ 重解零基础100题(6)
69 0
|
Python
Python​ 重解零基础100题(5)
Python​ 重解零基础100题(5)
65 0
|
Python
Python​ 重解零基础100题(4)
Python​ 重解零基础100题(4)
82 0
|
机器人 Python
Python​ 重解零基础100题(3)
Python​ 重解零基础100题(3)
115 0
|
Python 容器
Python​ 重解零基础100题(1)
Python​ 重解零基础100题(1)
136 0