编写python程序、找出100_100+ Python挑战性编程练习(1)

目前,这个项目已经获得了7.1k Stars,4.1k Forks。

初级水平是指刚刚通过Python入门课程的人。他可以用1或2个Python类或函数来解决一些问题。通常,答案可以直接在教科书中找到。

中级水平是指刚刚学习了Python,但是已经有了较强的编程背景的人。他应该能够解决可能涉及3个或3个Python类或函数的问题。答案不能直接在教科书中找到。

先进水平是他应该使用Python来解决更复杂的问题,使用更丰富的库、函数、数据结构和算法。他应该使用几个Python标准包和高级技术来解决这个问题。

=========1、Question:问题 2、Hints:提示 3、Solution:解决方案===========

1、编写一个程序,找出所有能被7整除但不是5的倍数的数,2000年到3200年(都包括在内)。得到的数字应该以逗号分隔的顺序打印在一行上。

考虑使用range(#begin, #end)方法

1 values =[]2

3 for i in range(1000, 3001):4 s =str(i)5 if (int(s[0]) % 2 == 0) and (int(s[1]) % 2 == 0) and (int(s[2])%2==0) and (int(s[3])%2==0):6 values.append(s)7

8 print(','.join(values))9

10

11

12

13 #2000,2002,2004,.........,2884,2886,2888

View Code

2、 编写一个可以计算给定数字的阶乘的程序。结果应该以逗号分隔的顺序打印在一行上。假设向程序提供以下输入:8则输出为:40320

如果向问题提供输入数据,则应该将其视为控制台输入。

1 deffact(x):2 if x ==0:3 return 1

4 return x*fact(x-1)5

6 x =int(input())7 print(fact(x))

View Code

3、 对于给定的整数n,编写一个程序来生成一个字典,其中包含(i, i*i)一个在1和n之间的整数(都包含在内)。然后程序应该打印字典。假设向程序提供以下输入:8则输出为: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}

考虑使用dict类型()

1 n =int(input())2 d =dict()3 for i in range(1, n+1):4 d[i] = i*i5 print(d)

View Code

4、编写一个程序,接受来自控制台的逗号分隔的数字序列,并生成一个包含每个数字的列表和元组。假设向程序提供以下输入: 34,67,55,33,12,98 则输出为:['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98')

方法可以将列表转换为元组

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

View Code

5、 定义一个至少有两个方法的类:getString:从控制台输入获取字符串打印字符串:打印字符串的大写字母。还请包含简单的测试函数来测试类方法。

使用_init__方法构造一些参数 (直接一个uppper不可以吗??)

1 classInputOutString(object):2 def __init__(self):3 self.s = ""

4

5 defgetString(self):6 self.s =input()7

8 defprintString(self):9 print(self.s.upper())10

11

12 strObj =InputOutString()13 strObj.getString()14 strObj.printString()

View Code

6、 编写一个程序,计算和打印的价值根据给定的公式:Q =√[(2 * C * D)/H]以下是C和H的固定值:C是50。H是30。D是一个变量,它的值应该以逗号分隔的序列输入到程序中。

例子-假设程序的输入序列是逗号分隔的: 100,150,180 则输出为:18,22,24

如果接收到的输出是十进制的,则应四舍五入到其最接近的值(例如,如果接收到的输出是26.0,则应打印为26)

1 #!/usr/bin/env python

2 importmath3 c=50

4 h=30

5 value =[]6 items=[x for x in input().split(',')]7 for d initems:8 value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))9

10 print(','.join(value))

View Code

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

1 input_str =input()2 dimensions=[int(x) for x in input_str.split(',')]3 rowNum=dimensions[0]4 colNum=dimensions[1]5 multilist = [[0 for col in range(colNum)] for row inrange(rowNum)]6

7 for row inrange(rowNum):8 for col inrange(colNum):9 multilist[row][col]= row*col10

11 print(multilist)

View Code

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

假设向程序提供以下输入:without,hello,bag,world 输出为: bag,hello,without,world

1 items=[x for x in input().split(',')]2 items.sort()3 print(','.join(items))

View Code

9、编写一个接受行序列作为输入的程序,并在将句子中的所有字符都大写后打印这些行。

Suppose the following input is supplied to the program: Hello world Practice makes perfect Then, the output should be: HELLO WORLD PRACTICE MAKES PERFECT

1 lines =[]2 whileTrue:3 s =input()4 ifs:5 lines.append(s.upper())6 else:7 break;8

9 for sentence inlines:10 print(sentence)

View Code

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

假设向程序提供以下输入: hello world and practice makes perfect and hello world again 输出为: again and hello makes perfect practice world

我们使用set容器自动删除重复的数据,然后使用sort()对数据进行排序。

1 s =input()2 words = [word for word in s.split(" ")]3 print(' '.join(sorted(list(set(words)))))

View Code

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

例如:0100,0011,1010,1001 输出为:1010

1 value =[]2 items=[x for x in input().split(',')]3 for p initems:4 intp = int(p, 2)5 if not intp%5:6 value.append(p)7

8 print(','.join(value))

View Code

12、编写一个程序,它将找到所有这些数字在1000和3000之间(都包括在内),使数字的每一位都是偶数。得到的数字应该以逗号分隔的顺序打印在一行上。

输出结果为:2000,2002,2004,2006,2008,2020,2022,2024,......,2866,2868,2880,2882,2884,2886,2888

1 values =[]2

3 for i in range(1000, 3001):4 s =str(i)5 if (int(s[0]) % 2 == 0) and (int(s[1]) % 2 == 0) and (int(s[2])%2==0) and (int(s[3])%2==0):6 values.append(s)7

8 print(','.join(values))

View Code

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

例如 hello world! 123 输出为: LETTERS 10 DIGITS 3

1 s =input()2 d={"DIGITS":0, "LETTERS":0}3 for c ins:4 ifc.isdigit():5 d["DIGITS"]+=1

6 elifc.isalpha():7 d["LETTERS"]+=1

8 else:9 pass

10 print("LETTERS", d["LETTERS"])11 print("DIGITS", d["DIGITS"])

View Code

14、编写一个程序,接受一个句子,并计算字母和数字的数量。编写一个程序,接受一个句子,并计算大写字母和小写字母的数量。

例如:I love You till the end of the World   UPPER CASE 3 LOWER CASE 25

1 s =input()2 d={"UPPER CASE":0, "LOWER CASE":0}3 for c ins:4 ifc.isupper():5 d["UPPER CASE"]+=1

6 elifc.islower():7 d["LOWER CASE"]+=1

8 else:9 pass

10 print("UPPER CASE", d["UPPER CASE"])11 print("LOWER CASE", d["LOWER CASE"])

View Code

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

例如:2  2468

1 a =input()2 n1 = int( "%s" %a )3 n2 = int( "%s%s" %(a,a) )4 n3 = int( "%s%s%s" %(a,a,a) )5 n4 = int( "%s%s%s%s" %(a,a,a,a) )6 print (n1+n2+n3+n4)

View Code

16、使用列表理解来对列表中的每个奇数求平方。该列表是由逗号分隔的数字序列输入的。

例如:1,2,3,4,5,6,7,8,9 输出为:1,3,5,7,9

1 #values = "1,2,3,4,5,6,7,8,9"

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

View Code

17、编写一个程序,根据控制台输入的交易日志计算银行帐户的净金额。事务日志格式如下:D 100W 200D表示存款,W表示取款。

假设向程序提供以下输入:D 300 D 300 W 200 D 100 输出为:500 (越写越觉得无聊啊,继续坚持)

1 netAmount =02 whileTrue:3 s =input()4 if nots:5 break

6 values = s.split(" ")7 operation =values[0]8 amount = int(values[1])9 if operation=="D":10 netAmount+=amount11 elif operation=="W":12 netAmount-=amount13 else:14 pass

15 print(netAmount)

View Code

18、网站需要用户输入用户名和密码才能注册。编写程序检查用户输入密码的有效性。

以下是检查密码的准则:

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

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

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

3.至少有一个字符来自[$#@]

4.最小交易密码长度:6

5.最大交易密码长度:12

您的程序应该接受一个逗号分隔的密码序列,并将根据上述标准检查它们。匹配条件的密码将被打印出来,每个密码之间用逗号分隔。

例子

如果下列密码作为程序的输入:

ABd1234@1 F1 # 2 w3e * 2 we3345

则程序输出为:

ABd1234@1

1 importre2 value =[]3 items=[x for x in input().split(',')]4 for p initems:5 if len(p)<6 or len(p)>12:6 continue

7 else:8 pass

9 if not re.search("[a-z]",p):10 continue

11 elif not re.search("[0-9]",p):12 continue

13 elif not re.search("[A-Z]",p):14 continue

15 elif not re.search("[$#@]",p):16 continue

17 elif re.search("\s",p):18 continue

19 else:20 pass

21 value.append(p)22 print(",".join(value))

View Code

19、您需要编写一个程序来对(姓名、年龄、身高)元组按升序排序,其中姓名是字符串,年龄和身高是数字。元组由控制台输入。排序标准为:

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

----我们使用itemgetter来启用多个排序键。

1 importoperator2 l =[]3 whileTrue:4 s =input()5 if nots:6 break

7 l.append(tuple(s.split(",")))8

9 print(sorted(l, key=operator.itemgetter(0,1,2)))

View Code

20、使用生成器定义一个类,它可以迭代给定范围0到n之间的数字,这些数字可以被7整除。

例如100内的: 0 7 14 21 28 35424956637077849198

1 defputNumbers(n):2 i =03 while i<4 j="i5" i="i+1</p">

6 if j%7==0:7 yieldj8

9 for i in putNumbers(100):10 print(i)

View Code

==============暂停分隔符==================下班回去继续写=================下面拓展链接==================

Python入门、提高学习网站链接:https://github.com/jackfrued/Python-100-Days?utm_source=wechat_session&utm_medium=social&utm_oi=931473721877651456

刷Leetcode网站力扣:https://leetcode-cn.com/problemset/all/

Python进阶:https://docs.pythontab.com/interpy/

不抱怨,不埋怨,没有所谓的寒冬, 只有不努力的人。加油,为了更牛的技术更高的薪资,多学习多积累。2019.11.30

4>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值