Python-06~10题

2020-08-11  本文已影响0人  AoEliauk

---06---

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.

>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

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=50,H=30。D是以逗号分隔使用Input函数输入的可选值。

假定我们输入(100,150,180)这3个值,输出结果应该是:18,22,24。

提示:如果结果是带小数点的浮点数,应该取整。

Solution:

program result

程序简化

直接打印


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

Solution: 


解法一:

program(1)

解法二:

program(2) result

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

Solution:

program result

sort()函数:用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数

语法:list.sort( key=None, reverse=False)

key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。

reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)

join() 方法:用于将序列中的元素以指定的字符连接生成一个新的字符串


---09---

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

Solution:

program result

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

Solution:

解法一:

program(1)

解法二:

program(2) result
上一篇下一篇

猜你喜欢

热点阅读