2021-02-19

100道python练习题(续)

6 lv2

这一道题我在写代码的时候做了一些修改,没有对一些变量进行赋值。

# 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
#写一个这样的程序,在满足Q=[(2*50*D)/30]^0.5的情况下,D为任意值,当输入100,150,180,它的输出结果是18,22,24
a=input()
b=a.split(',')
P=[]
import math
for i in range(0,len(b)-1):
    Q=math.sqrt((2*50*int(b[i]))/30)
    print(int(Q),end='')
    print(',',end='')
Q=math.sqrt((2*50*int(b[-1]))/30)
print(int(Q))
for i in b:
    P.append(str(int(math.sqrt((2 * 50 * int(i)) / 30))))
print(P)
print(','.join(P))

7 lv2

# 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]]
# 写一个二维数组,第i行和第j列的值为i*j,如:
# 当输入3,5时
# 输出:
# [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
x=input()
i=int(x.split(',')[0])
j=int(x.split(',')[1])
two_dimensional_array=[]
save=[]
for k in range(0,i):
    for l in range(0,j):
       save.append(k*l)
    two_dimensional_array.append(save)
    save=[]
print(two_dimensional_array)


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)

8 lv2

# 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
#写一个程序,接受逗号隔开的单词作为输入,并且将它们按单词表排序用以逗号隔开的方式打印
#例如:输入:
# without,hello,bag,world
# 输出:
# bag, hello, without, world
items=[x for x in input().split(',')]
items.sort()
print(','.join(items))
#我们可以通过冒泡排序来进行排序,代码如下:(其实是没有必要的,因为python是面向对象的语言)
def bubble(x):
    n=len(x)
    for i in range(n-1,0):
        for j in range(0,i):
            if x[j]>x[j+1]:
                temp=x[j]
                x[j]=x[j+1]
                x[j+1]=temp
    return x
print(bubble(items))

9 lv2

# 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
# 写一个接受句子的程序并且将它们转换为大写输出,输入格式如下:
# Hello world
# Practice makes perfect
# 输出如下:
# HELLO WORLD
# PRACTICE MAKES PERFECT
lines=[]
while True:
    s=input()
    if s:
        lines.append(s.upper())
    else:
        break
for line in lines:
    print(line)

10 lv2

# 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
#写一个接受一串单词的程序,并且移除复制的成分(相同的成分),最后将这些单词按照单词表顺序输出
#输入:
# hello world and practice makes perfect and hello world again
#输出:
# again and hello makes perfect practice world
words=[x for x in input().split(' ')]
words.sort()
wlen=len(words)
for i in range(1,wlen):
    if words[i-1]== words[i]:
        words[i-1]=0
for word in words:
    if word==0:
        words.remove(0)

print(' '.join(words))
#我们可以用集合轻松的筛选出相同的成分并将其剔除

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

大家如果觉得有点帮助的话可以点个赞哦

你可能感兴趣的:(python)