挑战Python100题(1)

2023-12-23 06:51:58

100+ Python challenging programming exercises 1

Question 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.

编写一个程序,找出2000到3200之间的所有这些数字,这些数字可以被7整除,但不是5的倍数(都包括在内)。数字以逗号分隔的顺序打印在一行上。

Hints: Consider use range(#begin, #end) method

Solution:

l=[]

for i in range(2000, 3201):
    if (i%7==0) and (i%5!=0):
        l.append(str(i))

print(','.join(l))

Out:?

2002,2009,2016,2023,2037,2044,2051,2058,2072,2079,2086,2093,2107,2114,2121,2128,2142,2149,2156,2163,2177,2184,2191,2198,2212,2219,2226,2233,2247,2254,2261,2268,2282,2289,2296,2303,2317,2324,2331,2338,2352,2359,2366,2373,2387,2394,2401,2408,2422,2429,2436,2443,2457,2464,2471,2478,2492,2499,2506,2513,2527,2534,2541,2548,2562,2569,2576,2583,2597,2604,2611,2618,2632,2639,2646,2653,2667,2674,2681,2688,2702,2709,2716,2723,2737,2744,2751,2758,2772,2779,2786,2793,2807,2814,2821,2828,2842,2849,2856,2863,2877,2884,2891,2898,2912,2919,2926,2933,2947,2954,2961,2968,2982,2989,2996,3003,3017,3024,3031,3038,3052,3059,3066,3073,3087,3094,3101,3108,3122,3129,3136,3143,3157,3164,3171,3178,3192,3199


Question 2

Question: Write a program which can compute the factorial of a given numbers. Suppose the following input is supplied to the program: 8 Then, the output should be: 40320

写一个程序,可以计算给定数字的阶乘。假设向程序提供了以下输入:8,则输出应为:40320

Hints: In case of input data being supplied to the question, it should be assumed to be a console input.

Solution:

def fact(x):
    if x == 0:
        return 1
    return x * fact(x - 1)
 
x=int(input())
print(fact(x))

In:
8
Out:
40320


Question 3

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}

Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Consider use dict()

对于给定的整数n,编写一个程序生成并打印列出一个包含(i,i*i)的字典,数字i介于1和n之间的整数(两者都包括)。假设向程序提供了以下输入8,则输出:{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}。
提示:如果向问题提供输入数据,则应假定它是控制台输入。考虑使用dict()

Solution:

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

In:
8

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


Question 4

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

Hints: In case of input data being supplied to the question, it should be assumed to be a console input. tuple() method can convert list to tuple

编写一个程序,从控制台接受逗号分隔的数字序列,并生成包含每个数字的列表和元组。假设向程序提供了以下输入: 34,67,55,33,12,98 ,输出则为:?['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98')
提示:如果向问题提供输入数据,则应假定它是控制台输入。tuple()方法可以将列表转换为元组。

Solution:

values=input()

l=values.split(",")

t=tuple(l)

print(l)

print(t)

In:
34,67,55,33,12,98

Out:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')


Question 5

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.

Hints: Use init method to construct some parameters

定义一个至少有两个方法的类:getString:从控制台输入获取字符串,printString:以大写形式打印字符串。还请包括简单的测试函数来测试类方法。
提示:使用init方法构造一些参数

Solution:

class InputOutString(object):
    def __init__(self):
        self.s = ""

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

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

strObj = InputOutString()
strObj.getString()
strObj.printString()

In:
Hello, world.

Out:
HELLO, WORLD.


Question 6

Question: Write a program that calculates and prints the value according to the given formula: 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. 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

Hints: If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26) In case of input data being supplied to the question, it should be assumed to be a console input.

编写一个程序,根据给定的公式计算并打印该值:Q=[(2*C*D)/H]的平方根以下是C和H的固定值:C是50。H是30。D是一个变量,其值应以逗号分隔的顺序输入到程序中。示例让我们假设以下逗号分隔的输入序列被提供给程序:100,150,180;程序的输出应该是:18,22,24

提示:如果收到的输出为十进制形式,则应四舍五入至最接近的值(例如,如果收到的输出为26.0,则应打印为26)。如果向问题提供了输入数据,则应假定其为控制台输入。

Solution:

import math

c=50
h=30
value = []

items=[x for x in input().split(',')]

for d in items:
    value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))

print(','.join(value))

In:
100,150,180

Out:
18,22,24


Question 7

Question: Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j. Note: i=0,1.., X-1; j=0,1,?-Y-1. Example 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]]

Hints: Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form.

编写一个程序,以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],[0,1,2,3,4],[0,2,4,6,8]]

提示:注意:如果向问题提供了输入数据,则应假定它是以逗号分隔形式的控制台输入。

Solution:

input_str = 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)

In:
3,5

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


Question 8

Question: Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically. Suppose the following input is supplied to the program: without,hello,bag,world Then, the output should be: bag,hello,without,world

Hints: In case of input data being supplied to the question, it should be assumed to be a console input.

编写一个程序,接受逗号分隔的单词序列作为输入,并在按字母顺序排序后按逗号分隔的顺序打印单词。假设向程序提供了以下输入:without,hello,bag,world,那么输出应该是:bag,hello,without,world
提示:如果向问题提供输入数据,则应假定它是控制台输入。

Solution:

items=[x for x in input().split(',')]

items.sort()

print(','.join(items))

In:
without,hello,bag,world

Out:
bag,hello,without,world


Question 9

Question:?Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized. Suppose the following input is supplied to the program: Hello world Practice makes perfect. Then, the output should be: HELLO WORLD PRACTICE MAKES PERFECT

Hints: In case of input data being supplied to the question, it should be assumed to be a console input.

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

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

Solution:???????

lines = []
while True:
    s = input()
    if s:
        lines.append(s.upper())
    else:
        break;
 
for sentence in lines:
    print(sentence)

In:
Hello
world
Practice
makes
perfect
?

Out:
HELLO
WORLD
PRACTICE
MAKES
PERFECT


Question 10

Question: Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically. 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

Hints: In case of input data being supplied to the question, it should be assumed to be a console input. We use set container to remove duplicated data automatically and then use sorted() to sort the data.

编写一个程序,接受一系列空格分隔的单词作为输入,并在删除所有重复单词并按字母数字排序后打印这些单词。假设向程序提供了以下输入:hello world and practice makes perfect and hello world again,那么输出应该是:again and hello makes perfect practice world

提示:如果向问题提供输入数据,则应假定它是控制台输入。我们使用set container自动删除重复的数据,然后使用sorted()对数据进行排序。

Solution:

s = input()

words = [word for word in s.split(" ")]

print(" ".join(sorted(list(set(words)))))

In:
hello world and practice makes perfect and hello world again

Out:
again and hello makes perfect practice world


来源:GitHub - 965714601/Python-programming-exercises: 100+ Python challenging programming exercises

文章来源:https://blog.csdn.net/boysoft2002/article/details/135162361
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。