内容简介:大家好,好久不见,我最近在Github上发现了一个好东西,是关于夯实Python基础的100道题,原作者是在Python2的时候创建的,闲来无事,非常适合像我一样的小白来练习对于每一道题,解法都不唯一,我在这里仅仅是抛砖引玉,希望可以集合大家的智慧,如果哪道题有其他解法,希望可以在评论中留下大家宝贵的意见!每次我会更新10道题,一共会更新10篇,这也算是对我之前的文章一个总结啦,如果没有看到我之前有关Python的小白学习分享的同学们,可以戳下面连接查看哈:如果大家想要和我联系,可以访问我的个人主页:
一套全面的练习,大家智慧的结晶
大家好,好久不见,我最近在Github上发现了一个好东西,是关于夯实 Python 基础的100道题,原作者是在Python2的时候创建的,闲来无事,非常适合像我一样的小白来练习
对于每一道题,解法都不唯一,我在这里仅仅是抛砖引玉,希望可以集合大家的智慧,如果哪道题有其他解法,希望可以在评论中留下大家宝贵的意见!每次我会更新10道题,一共会更新10篇,这也算是对我之前的文章一个总结啦,如果没有看到我之前有关Python的小白学习分享的同学们,可以戳下面连接查看哈:
如果大家想要和我联系,可以访问我的个人主页:
好啦,闲话少说,让我们开始今天的刷题之旅吧!
Question 1:
Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,between 2000 and 3200 (both included).The numbers obtained should be printed in a comma-separated sequence on a single line.
解法一
for i in range(2000,3201):
if i%7 == 0 and i%5!=0:
print(i,end=',')
print("\b")
解法二
numbers = [str(x) for x in range(2000,3201) if (x%7==0) and (x%5!=0)]
print (','.join(numbers))
Question 2:
* Write a program which can compute the factorial of a given numbers.The results should be printed in a comma-separated sequence on a single line.Suppose the following input is supplied to the program: 8
Then, the output should be:40320 *
解法一
def fact(x):
if x == 0:
return 1
return x * fact(x - 1)
x=int(input())
print(fact(x))
解法二
import math as ma x=int(input()) print(ma.factorial(x))
解法三
from functools import reduce from operator import mul x=int(input()) print(reduce(mul,range(1,x+1)))
Question 3:
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
解法一
n=int(input())
d=dict()
for i in range(1,n+1):
d[i]=i*i
print(d)
解法二
n=int(input())
d={x:x*x for x in range(1,n+1)}
print(d)
Question 4:
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.Suppose the following input is supplied to the program:
34,67,55,33,12,98
Then, the output should be:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')
解法一
values=input()
l=values.split(",")
t=tuple(l)
print(f"List of values : {l}")
print(f"Tuple of values : {t}")
Question 5:
Define a class which has at least two methods:
- getString: to get a string from console input
- printString: to print the string in upper case.
Also please include simple test function to test the class methods.
解法一
class InputOutString:
def __init__(self):
self.s = ""
def getString(self):
self.s = input()
def printString(self):
print(self.s.upper())
# Test
a = InputOutString()
a.getString()
a.printString()
Question 6:
Q = Square root of [(2 C D)/H]
Following are the fixed values of C and H:
C is 50. H is 30.
D is the variable whose values should be input to your program in a comma-separated sequence.For example
Let us assume the following comma separated input sequence is given to the program: *
100,150,180
The output of the program should be:
18,22,24
解法一
import math
c=50
h=30
value = []
items= [x for x in input("Input numbers comma-separated:").split(',')]
for d in items:
value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))
print (','.join(value))
Question 7:
Note: i=0,1.., X-1; j=0,1,¡Y-1. Suppose the following inputs are given to the program: 3,5
Then, the output of the program should be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
解法一
x,y = map(int,input().split(','))
lst = []
for i in range(x):
tmp = []
for j in range(y):
tmp.append(i*j)
lst.append(tmp)
print(lst)
解法二
x,y = map(int,input().split(','))
lst = [[i*j for j in range(y)] for i in range(x)]
print(lst)
Question 8:
Suppose the following input is supplied to the program:
without,hello,bag,world
Then, the output should be:
bag,hello,without,world
解法一
original_string = input("Input Text:")
l = original_string.split(",")
final_string = sorted(l,key=str)
print(','.join(final_string))
解法二
lst = input().split(',')
lst.sort()
print(",".join(lst))
Question 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
解法一
lines = []
while True:
s = input()
if s:
lines.append(s.upper())
else:
break;
for sentence in lines:
print(sentence)
Question 10:
Suppose the following input is supplied to the program:
hello world and practice makes perfect and hello world again
Then, the output should be:
again and hello makes perfect practice world
解法一
word = input().split()
for i in word:
if word.count(i) > 1: #count function returns total repeatation of an element that is send as argument
word.remove(i) # removes exactly one element per call
word.sort()
print(" ".join(word))
解法二
s = input("Input Text:")
words = [word for word in s.split(" ")]
print (" ".join(sorted(list(set(words)))))
源代码下载
我把前十道题的代码上传到github上啦,如果大家想看一下每道题的输出结果,可以点击以下链接下载:
我的运行环境Python 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:
- raw_input()在Python3中是input()
- print需要加括号
- fstring可以换成.format(),或者%s,%d
谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- golang基础练习(一)
- Python基础练习100题 ( 21~ 30)
- Python基础练习100题 ( 31~ 40)
- [Java] 蓝桥杯BASIC-14 基础练习 时间转换
- [Java] 蓝桥杯BASIC-17 基础练习 矩阵乘法
- [Java] 蓝桥杯BASIC-15 基础练习 字符串对比
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Form Design
Luke Wroblewski / Rosenfeld Media / 2008-5-2 / GBP 25.00
Forms make or break the most crucial online interactions: checkout, registration, and any task requiring information entry. In Web Form Design, Luke Wroblewski draws on original research, his consider......一起来看看 《Web Form Design》 这本书的介绍吧!
CSS 压缩/解压工具
在线压缩/解压 CSS 代码
XML、JSON 在线转换
在线XML、JSON转换工具