Python 100練習(xí)題 Day3

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.其實(shí)參考答案中提供的利用count函數(shù)的方法更加的實(shí)用,也更加的簡(jiǎn)單
Python count() 方法用于統(tǒng)計(jì)字符串里某個(gè)字符出現(xiàn)的次數(shù)??蛇x參數(shù)為在字符串搜索的開(kāi)始與結(jié)束位置。
count()方法語(yǔ)法:
"""
str.count(sub, start= 0,end=len(string))
"""
參數(shù)
sub -- 搜索的子字符串
start -- 字符串開(kāi)始搜索的位置。默認(rèn)為第一個(gè)字符,第一個(gè)字符索引值為0。
end -- 字符串中結(jié)束搜索的位置。字符中第一個(gè)字符的索引為 0。默認(rèn)為字符串的最后一個(gè)位置。
2.Python中sort ()與 sorted() 區(qū)別
sort 與 sorted 區(qū)別:

sort 是應(yīng)用在 list 上的方法,屬于列表的成員方法,sorted 可以對(duì)所有可迭代的對(duì)象進(jìn)行排序操作。
list 的 sort 方法返回的是對(duì)已經(jīng)存在的列表進(jìn)行操作,而內(nèi)建函數(shù) sorted 方法返回的是一個(gè)新的 list,而不是在原來(lái)的基礎(chǔ)上進(jìn)行的操作。
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:

這部分的題目主要設(shè)計(jì)的問(wèn)題難點(diǎn)我認(rèn)為在于進(jìn)制的轉(zhuǎn)換


image.png

上圖很好的說(shuō)明了進(jìn)制之間轉(zhuǎn)換的函數(shù),感覺(jué)并不是很常用,返回的值是字符串,會(huì)帶有前綴,其實(shí)在題目的最后應(yīng)該再用下字符串分割把前綴刪除

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()的使用判斷。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容