0
点赞
收藏
分享

微信扫一扫

100+ Python challenging programming exercises慢慢更新

读思意行 2022-04-14 阅读 29
python
这是一个github项目,fork 6.2k,star 19.3k。
原项目是英文的,我用有道翻译,过分离谱的地方稍微修改一下。慢慢学习慢慢做,努力提升自己的编程水平。
原地址:Python-programming-exercises/100+ Python challenging programming exercises for Python 3.md at master · zhiwehu/Python-programming-exercises · GitHub100+ Python challenging programming exercises. Contribute to zhiwehu/Python-programming-exercises development by creating an account on GitHub.https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises%20for%20Python%203.md题目分三级,level 1、level 2、level 3
1级新手,2级中等水平,3级高手。

'''Question 1
Level 1
Question:
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.'''
'''写一个程序,找出所有能被7整除但不是5倍数的数, 从2000到3200(都包括在内)。获得的数字应该以逗号分隔的顺序打印在一行上。'''

代码:

lista = []
for i in range(2000,3201):
    if i % 7 == 0 and i % 5 != 0:
        lista.append(str(i))   # append()是列表的方法,字符串没有。
# print(lista)
print(','.join(lista)) 
'''Question 2
Level 1

Question:
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'''
'''写一个程序,可以计算给定数字的阶乘。  
结果应该以逗号分隔的顺序打印在一行上。  
假设向程序提供以下输入:  
8  
然后,输出应该是:  
40320  '''
def factorial(n):
    sum = 1
    for i in range(1,n+1):
        sum *= i
    return sum

n = int(input('请输入想要计算阶乘的数:'))
fac = factorial(n)
print(fac)
'''Question 3
Level 1

Question:
With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.
Suppose the following input is supplied to the program:
8
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}'''
'''对于给定的整数n,编写一个程序生成一个包含(i, i*i)的字典,使其为1到n之间的整数(两者都包含)。 然后程序应该打印字典。  
假设向程序提供以下输入:  
8  
然后,输出应该是:  
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}  '''
def square(n):
    squ = dict()
    for i in range(1,n+1):
        squ[i] = i*i
    return squ


n = int(input('请输入n的值:'))
squdic = square(n)
print(squdic)
'''Question 4
Level 1

Question:
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')'''
'''编写一个程序,从控制台接收一个逗号分隔的数字序列,并生成包含每个数字的列表和元组。  
假设向程序提供以下输入:  
34,67,55,33,12,98  
然后,输出应该是:  
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')'''
nums = input('请输入数字序列,用逗号分隔,按q结束:')
numl = list(nums.split(','))
numt = tuple(nums.split(','))
# numl = nums.split(',')
# numt = tuple(nums)   # 这两句是答案的,但是错的,会将字符串的每一个字符都打散变成元组
print(numl)
print(numt)
'''Question 5
Level 1

Question:
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.'''
'''定义一个至少有两个方法的类:  
getString:从控制台输入获取一个字符串  
printString:打印大写字符串。  
也请包含简单的测试函数来测试类的方法。  '''
class IOString(object):
    def __init__(self):
        self.stra = ''

    def getString(self):
        self.stra =  input('请输入一个字符串:')

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

str1 = IOString()

str1.getString()
str1.printString()

待续……

举报

相关推荐

0 条评论