程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-05(2)

Python 练习100题---No.(1-20)---Level(1-3)

发布于2019-12-14 15:53     阅读(960)     评论(0)     点赞(19)     收藏(5)


github展示python100题
链接如下:
https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt
以下为博主翻译后题目及解答,答案代码分为两个,第一条为博主个人解答(Python3),第二条为题目所提供答案(Python2)
………………………………………………………………………………
本部分为题目1-20,等级难度1-3,后续题目文章更新中~~
………………………………………………………………………………
1、问题:

写一个程序,找出所有这些数字,可以被7整除,但不是5的倍数,

2000至3200间(均包括在内)。

获得的数字应以逗号分隔的顺序打印在一行上。

ls =  []
for i in range(2000,3200):
    if (i%7 == 0)and (i%5 != 0):
        ls.append(str(i))
print (",".join(ls))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
values=raw_input()
l=values.split(",")
t=tuple(l)
print l
print t
  • 1
  • 2
  • 3
  • 4
  • 5

2、问题:

写一个能计算给定 数的阶乘的程序。

结果应以逗号分隔的顺序打印在一行上。

假设向程序提供了以下输入:

8

那么,输出应该是:

40320

a=int(input("input the number"))
JC =int(1)
while a!=0:
    JC=JC*a
    a=a-1
print (JC)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
def fact(x):
    if x == 0:
        return 1
    return x * fact(x - 1)

x=int(raw_input())
print fact(x)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3、问题:

对于给定的整数n,编写一个程序来生成一个字典,其中包含(i,i*i)这样一个介于1和n之间的整数(两者都包括在内)。然后程序应该打印字典。

假设向程序提供了以下输入:

8

那么,输出应该是:

{1:1,2:4,3:9,4:16,5:25,6:36,7:49,8:64}

a= {}
i=int(input("input the number"))
while i != 0:
    a[i]=i*i
    i=i-1
print (a)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
n=int(raw_input())
d=dict()
for i in range(1,n+1):
    d[i]=i*i

print d
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4、问题:
编写一个程序,从控制台接收一系列逗号分隔的数字,并生成一个列表和一个包含每个数字的元组。

假设向程序提供了以下输入:

34、67、55、33、12、98

那么,输出应该是:

[‘34’,‘67’,‘55’,‘33’,‘12’,‘98’]

(‘34’、‘67’、‘55’、‘33’、‘12’、‘98’)

x=input("please input")
a=x.split(",")
b=() 
for item in a: 
    b=b+(item,)
print (a)
print (b)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
values=raw_input()
l=values.split(",")
t=tuple(l)
print l
print t
  • 1
  • 2
  • 3
  • 4
  • 5

5、问题:
定义至少有两个方法的类:

get string:从控制台输入获取字符串

print string:以大写形式打印字符串。

也请包含简单的测试函数来测试类方法。

class test():
    def iput(self):
        self.i=input("please input:")
    def up(self):    
       self.j=self.i.upper()
    def pt(self):
       print(self.j)
x=test()
x.iput()
x.up()
x.pt()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
class InputOutString(object):
    def __init__(self):
        self.s = ""

    def getString(self):
        self.s = raw_input()

    def printString(self):
        print self.s.upper()

strObj = InputOutString()
strObj.getString()
strObj.printString()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

6、问题:
编写一个程序,根据给定的公式计算并打印值:

Q= [(2 * C * D)/H]的平方根

C和H的固定值如下:

C是50。H是30。

D是一个变量,其值应以逗号分隔的顺序输入到程序中。

例子

假设程序有以下逗号分隔的输入序列:

100,150,180

程序的输出应为:

18,22,24

C = 50
H = 30
TOLQ = []
TOL = [x for x in input('input').split(',') ]
for D in TOL:
      TOLQ.append(str(int(round((((2 * C * float(D))/H)**1/2)))))
print (','.join(TOLQ))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
#!/usr/bin/env python
import math
c=50
h=30
value = []
items=[x for x in raw_input().split(',')]
for d in items:
    value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))

print ','.join(value)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

7、问题:
编写一个以2位X,Y为输入,生成二维数组的程序。数组的第i行和第j列中的元素值应为i*j。

注:i=0,1…,X-1;j=0,1,…Y-1。

例子

假设程序有以下输入:

3,5

那么,程序的输出应该是:

[[0,0,0,0,0],[0,1,2,3,4],[0,2,4,6,8]]

x = input ("x:")
y = input ("y:")
num_list = [[0]*(int(y)) for n in range(int(x))]
i=0
j=0
for i in range(0,int(x)) :
    for j in range(0,int(y)) :
       num_list[i][j]=i*j
       j +=1
    i +=1
print (num_list)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
input_str = raw_input()
dimensions=[int(x) for x in input_str.split(',')]
rowNum=dimensions[0]
colNum=dimensions[1]
multilist = [[0 for col in range(colNum)] for row in range(rowNum)]

for row in range(rowNum):
    for col in range(colNum):
        multilist[row][col]= row*col

print multilist
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

8、问题:
编写一个程序,接受以逗号分隔的单词序列作为输入,并在按字母顺序排序后以逗号分隔的序列打印单词。

假设向程序提供了以下输入:

without,hello,bag,world

那么,输出应该是:

bag,hello,without,world

x = input ("x:")
a = x.split(',')
i=0
for i in range(0,len(a)-1):
    for j in range(i+1,len(a)):
        string1 = a[i]
        string2 = a[i+1]
        if string1[0] > string2[0]:
            b=a[i]
            a[i]=a[j]
            a[j]=b
        i+=1
print (a)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
items=[x for x in raw_input().split(',')]
items.sort()
print ','.join(items)
  • 1
  • 2
  • 3

9、问题:
编写一个程序,接受行序列作为输入,并在将句子中的所有字符大写后打印行。
假设向程序提供了以下输入:
Hello world
Practice makes perfect
那么,输出应该是:
HELLO WORLD
PRACTICE MAKES PERFECT

ls = []
end = 'end'
print ("输入,并以end结束")
for i in iter(input,end):
    ls.append(i.upper())
print (ls)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
lines = []
while True:
    s = raw_input()
    if s:
        lines.append(s.upper())
    else:
        break;

for sentence in lines:
    print sentence
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

10、问题:
编写一个程序,接受一系列空格分隔的单词作为输入,并在删除所有重复的单词并按字母数字顺序排序后打印这些单词。

假设向程序提供了以下输入:

hello world and practice makes perfect and hello world again

那么,输出应该是:

again and hello makes perfect practice world

list0=input('please input').split()
list3=list(set(list0))
list3.sort()
print(list3)

  • 1
  • 2
  • 3
  • 4
  • 5
s = raw_input()
words = [word for word in s.split(" ")]
print " ".join(sorted(list(set(words))))
  • 1
  • 2
  • 3

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

例子:

0100,0011,1010,1001

那么输出应该是:

1010

注意:假设数据是由控制台输入的。

ls = input("please input").split(",")
ls2 = []
ls3 = []
for i in ls:
    j=int(i[0])*(2**3)+int(i[1])*(2**2)+int(i[2])*(2**1)+ int(i[3])*(2**0) 
    if (j%5 ==0):
        ls2.append(j)
        ls3.append(i)

print (ls2)
print (ls3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
value = []
items=[x for x in raw_input().split(',')]
for p in items:
    intp = int(p, 2)
    if not intp%5:
        value.append(p)

print ','.join(value)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

12、问题:

编写一个程序,找出1000到3000之间的所有数字(包括这两个数字),使每个数字都是偶数。

获得的数字应以逗号分隔的顺序打印在一行上。

ls = []
for i in range(1000,3001):
    if (i%2==0):
        ls.append(i)
print (ls)
  • 1
  • 2
  • 3
  • 4
  • 5
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

假设向程序提供了以下输入:

hello world! 123

那么,输出应该是:

LETTERS 10
DIGITS 3

ls = input("please input:")
digit = 0
alpha = 0
for i in ls:
    if i.isdigit():
        digit += 1
    elif i.isalpha():
        alpha += 1

print ("LETTERS "+str(alpha))
print ("DIGITS "+str(digit))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
s = raw_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 "LETTERS", d["LETTERS"]
print "DIGITS", d["DIGITS"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

14、问题:
编写一个接受句子的程序,计算大写字母和小写字母的数目。

假设向程序提供了以下输入:

Hello world!
那么,输出应该是:

UPPER CASE 1
LOWER CASE 9

ls = input("please input:")
up = 0
low = 0
for i in ls:    
    if i.isupper():  
        up += 1
    elif i.islower():
        low += 1
    else: 
        pass
print ("UPPER CASE " + str(up))
print ("LOWER CASE " + str(low))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
s = raw_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"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

假设向程序提供了以下输入:

9

那么,输出应该是:

11106

a=int(input("input a:"))
b=a*10 +a
c=a*100+b
d=a*1000+c
e=a+b+c+d
print (e)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
a = raw_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
  • 2
  • 3
  • 4
  • 5
  • 6

16、问题:
使用列表理解将列表中的每个奇数平方。列表由一系列逗号分隔的数字输入。

假设向程序提供了以下输入:

1,2,3,4,5,6,7,8,9

那么,输出应该是:

1,3,5,7,9

ls = (input("please input:")).split(',')
ls2 = []
for i in ls:
    if (int(i)%2 != 0):
        ls2.append(i)
print (ls2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
values = raw_input()
numbers = [x for x in values.split(",") if int(x)%2!=0]
print ",".join(numbers)
  • 1
  • 2
  • 3

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

D 100
W 200

D表示存款,W表示取款。

假设向程序提供了以下输入:

D 300
D 300
W 200
D 100

那么,输出应该是:

500

ls = []
digit = []
alpha = []
end = 'end'
print ("输入,并以end结束")
for i in iter(input,end):
    ls.append(i.split())
for h in ls:
    for j in h: 
        if j.isdigit():
            digit.append(j)
        if j.isalpha():
            alpha.append(j)
print (digit)
print (alpha)
x =0
SUM1 = 0
for x in range(0,len(digit)):
    if alpha[x]=='D' :
        SUM1 = SUM1 + int(digit[x])
    elif alpha[x]=='W' :
        SUM1 = SUM1 - int(digit[x])
    else:
        pass
print (SUM1)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
netAmount = 0
while True:
    s = raw_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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

18、问题:
网站要求用户输入用户名和密码才能注册。编写程序检查用户输入密码的有效性。
以下是检查密码的条件:
1。[a-z]之间至少有一个字母
2。在[0-9]之间至少有一个数字
1。[A-Z]之间至少有一个字母
3。[$#@]中至少有一个字符
4。交易密码最短长度:6
5。交易密码的最大长度:12
您的程序应该接受一系列逗号分隔的密码,并将根据上述标准进行检查。
将打印符合条件的密码,每个密码用逗号分隔。
例子
如果以下密码作为程序的输入:
ABd1234@1,a F1#,2w3E*,2We3345
那么,程序的输出应该是:
ABd1234@1
注:本题博主认为自己的方法是符合题意的,也就是程序输出应该不是题中所给的,叙述和结果不一致,仅作参考。

ls = input("input your password:")
tol = []
zm=sz=zm1=fh1=d=c = 0
for i in ls:
    if i.isalpha():
        if ("a"<=str(i) and str(i)<="z"):
            if (zm == 0):
                zm = 1      
            tol.append(i)    
        elif ("A"<=str(i) and str(i)<="Z"):
            if (zm1 == 0):
                zm1 = 1
            tol.append(i)
            
    elif i.isdigit():       
        if (0<int(i) & int(i)<10 ):
            if (sz == 0):
                sz = 1
            tol.append(i)
    elif str(i) in ('#','$','@'):
        if (fh1 == 0):    
            fh1 = 1
        tol.append(i)
    else :
        print ("存在规定外的字符"+str(i)) 
        
    if (len(ls)>=6 & len(ls)<=12):
        if (d == 0):
            d = 1      
    else :
        print ("超出范围")
    
if (zm == 1 and zm1==1 and sz ==1 and fh1==1 and d==1):
    print ("密码正确:"+str(tol))
else:
    print ("请输入规范密码")
    print ("当前符合规范的密码:"+str(tol))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
import re
value = []
items=[x for x in raw_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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

19、问题:
您需要编写一个程序来按升序对(name,age,height)元组进行排序,其中name是string,age和height是数字。元组由控制台输入。排序条件为:

1:按名称排序;

2:然后按年龄排序;

3:然后按分数排序。

优先考虑的是名字>年龄>分数。

如果将下列元组作为程序的输入:

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’)]

import operator
ls = []
while True:
    x = input("please input:")
    if not x:
        break
    ls.append(tuple(x.split(',')))
print (sorted(ls,key=operator.itemgetter(0,2,1)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
l = []
while True:
    s = raw_input()
    if not s:
        break
    l.append(tuple(s.split(",")))

print sorted(l, key=itemgetter(0,1,2))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

def putNumbers(n):
    i = 0
    while i<n:
        j=i
        i=i+1
        if j%7==0:
            yield j
x= input("please input 0-?:")
for i in putNumbers(int(x)):
    print (i)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
def putNumbers(n):
    i = 0
    while i<n:
        j=i
        i=i+1
        if j%7==0:
            yield j

for i in reverse(100):
    print i
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

推荐文章:
MySql练习题–50题–第一弹
MySql练习题–45题–第二弹

发布了22 篇原创文章 · 获赞 23 · 访问量 2596


所属网站分类: 技术文章 > 博客

作者:放羊人

链接:https://www.pythonheidong.com/blog/article/176310/4051a30e91d8d3fed566/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

19 0
收藏该文
已收藏

评论内容:(最多支持255个字符)