美文网首页
Python 100练习题 Day3

Python 100练习题 Day3

作者: P酱不想说话 | 来源:发表于2021-03-16 11:23 被阅读0次

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.


image.png image.png

summary:

1.其实参考答案中提供的利用count函数的方法更加的实用,也更加的简单
Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
count()方法语法:
"""
str.count(sub, start= 0,end=len(string))
"""
参数
sub -- 搜索的子字符串
start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
2.Python中sort ()与 sorted() 区别
sort 与 sorted 区别:

sort 是应用在 list 上的方法,属于列表的成员方法,sorted 可以对所有可迭代的对象进行排序操作。
list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
sort使用方法为ls.sort(),而sorted使用方法为sorted(ls)。

Question 11

Question
Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence.

Example:

0100,0011,1010,1001
Then the output should be:

1010
Notes: Assume the data is input by console.

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


image.png

summary:

这部分的题目主要设计的问题难点我认为在于进制的转换


image.png

上图很好的说明了进制之间转换的函数,感觉并不是很常用,返回的值是字符串,会带有前缀,其实在题目的最后应该再用下字符串分割把前缀删除

Question 12

Question: Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.The numbers obtained should be printed in a comma-separated sequence on a single line.

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


image.png

Question 13

Question: Write a program that accepts a sentence and calculate the number of letters and digits.

Suppose the following input is supplied to the program:

hello world! 123 Then, the output should be:

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


image.png

summary:主要是isalpha()和isnumeric()的使用判断。

相关文章

网友评论

      本文标题:Python 100练习题 Day3

      本文链接:https://www.haomeiwen.com/subject/jvorcltx.html