成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Python基礎(chǔ)練習(xí)100題 ( 91~ 100)

Jrain / 2676人閱讀

摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷最后的題解法一解法二解法一解法二解法一雞兔同籠解法一解法一解法二解法一解法二默認(rèn)就是

刷題繼續(xù)

昨天和大家分享了81-90題,今天繼續(xù)來刷最后的91-100題

Question 91:
Please write a program which accepts a string from console and print it in reverse order.

**Example:
If the following string is given as input to the program:*

rise to vote sir
Then, the output of the program should be:
ris etov ot esir
解法一
s = input()
s = "".join(reversed(s))
print(s)
解法二
s=input()
s = s[::-1]
print(s)
Question 92:
Please write a program which accepts a string from console and print the characters that have even indexes.

*Example:
If the following string is given as input to the program:*

H1e2l3l4o5w6o7r8l9d
Then, the output of the program should be:
Helloworld
解法一
s=input()
print(s[::2])
解法二
s = "H1e2l3l4o5w6o7r8l9d"
s = [ v for (i,v) in enumerate(s) if i%2 ==0 ]
print("".join(s))
Question 93:
Please write a program which prints all permutations of [1,2,3]

解法一
import itertools
result = list(itertools.permutations([1,2,3]))
print(result)
Question 94:
*Write a program to solve a classic ancient Chinese puzzle: 
We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have?* (雞兔同籠)

解法一
def solve(numheads,numlegs):
    ns="No solutions!"
    for i in range(numheads+1):
        j=numheads-i
        if 2*i+4*j==numlegs:
            return i,j
    return ns,ns

numheads=35
numlegs=94
solutions=solve(numheads,numlegs)
print(solutions)
Question 95:

Given the participants" score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.

If the following string is given as input to the program:

5
2 3 6 6 5

Then, the output of the program should be:

5
解法一
n = int(input())
arr = map(int, input().split())
arr = list(set(arr))
arr.sort()
print(arr[-2])
解法二
n = int(input())
arr = map(int, input().split())
arr = list(set(arr))
print(sorted(arr)[-2])
Question 96:

*You are given a string S and width W.
Your task is to wrap the string into a paragraph of width.*

If the following string is given as input to the program:

ABCDEFGHIJKLIMNOQRSTUVWXYZ
4

Then, the output of the program should be:

ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ
解法一
import textwrap

def wrap(string, max_width):
    string = textwrap.wrap(string,max_width)
    string = "
".join(string)
    return string

if __name__ == "__main__":
    string, max_width = input(), int(input())
    result = wrap(string, max_width)
    print(result)
解法二
import itertools as it

def grouper(lst, n, fillvalue=None):
    iters = [iter(lst)] * n
    return it.zip_longest(*iters, fillvalue=fillvalue)  #  默認(rèn)就是None
string, max_width = input(), int(input())
result = grouper(string, max_width)
print(list(result))
Question 97:

You are given an integer, N. Your task is to print an alphabet rangoli of size N. (Rangoli is a form of Indian folk art based on creation of patterns.)

Different sizes of alphabet rangoli are shown below:

#size 3

----c----
--c-b-c--
c-b-a-b-c
--c-b-c--
----c----

#size 5

--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------
解法一
import string
def print_rangoli(size):
    n = size
    alph = string.ascii_lowercase
    width = 4 * n - 3

    ans = []
    for i in range(n):
        left = "-".join(alph[n - i - 1:n])
        mid = left[-1:0:-1] + left
        final = mid.center(width, "-")
        ans.append(final)

    if len(ans) > 1:
        for i in ans[n - 2::-1]:
            ans.append(i)
    ans = "
".join(ans)
    print(ans)

if __name__ == "__main__":
    n = int(input())
    print_rangoli(n)
Question 98:
You are given a date. Your task is to find what the day is on that date.

Input

A single line of input containing the space separated month, day and year, respectively, in MM DD YYYY format.

08 05 2015

Output

Output the correct day in capital letters.

WEDNESDAY
解法一
import calendar

month, day, year = map(int, input().split())

dayId = calendar.weekday(year, month, day)
print(calendar.day_name[dayId].upper())
    
解法二
import datetime
month, day, year = map(int, input().split())
dayId = datetime.date(year, month, day)
print(dayId.strftime("%A"))
Question 99:
Given 2 sets of integers, M and N, print their symmetric difference in ascending order. The term symmetric difference indicates those values that exist in either M or N but do not exist in both.

Input

The first line of input contains an integer, M.The second line contains M space-separated integers.The third line contains an integer, N.The fourth line contains N space-separated integers.

4
2 4 5 9
4
2 4 11 12

Output

Output the symmetric difference integers in ascending order, one per line.

5
9
11
12

解法一
if __name__ == "__main__":
    n = int(input())
    set1 = set(map(int,input().split()))

    m = int(input())
    set2 = set(map(int, input().split()))

    ans = list(set1 ^ set2)
    print(sorted(ans))
Question 100:

You are given words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification.

If the following string is given as input to the program:

4
bcdef
abcdefg
bcde
bcdef

Then, the output of the program should be:

3
2 1 1
解法一
n = int(input())

word_list = []
word_dict = {}

for i in range(n):
    word = input()
    if word not in word_dict:
        word_list.append(word)
    word_dict[word] = word_dict.get(word, 0) + 1

print(len(word_list))
for word in word_list:
    print(word_dict[word], end=" ")
源代碼下載

這十道題的代碼在我的github上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載:

Python 91-100題

我的運(yùn)行環(huán)境Python 3.6+,如果你用的是Python 2.7版本,絕大多數(shù)不同就體現(xiàn)在以下3點(diǎn):

raw_input()在Python3中是input()

print需要加括號

fstring可以換成.format(),或者%s,%d

到今天為止,這套題就已經(jīng)全部結(jié)束了,相信大家如果看了每一道題,還是對技能提升有些許幫助的!

如果你有更好的Python學(xué)習(xí)資料,想要分享或者交流,歡迎給我留言哈!

進(jìn)入我的個人主頁

獨(dú)樂樂不如眾樂樂,大家一起進(jìn)步,謝謝!

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/44028.html

相關(guān)文章

  • Python基礎(chǔ)練習(xí)100 ( 81~ 90)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題我的運(yùn) 刷題繼續(xù) 昨天和大家分享了71-80題,今天繼續(xù)來刷81~90題 Question 81: By using list comprehension, p...

    劉德剛 評論0 收藏0
  • Python基礎(chǔ)練習(xí)100 ( 71~ 80)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下 刷題繼續(xù) 昨天和大家分享了61-70題,今天繼續(xù)來刷71~80題 Question 71: Please write a program to out...

    Jeff 評論0 收藏0
  • Python基礎(chǔ)練習(xí)100 ( 61~ 70)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題 刷題繼續(xù) 昨天和大家分享了51-60題,今天繼續(xù)來刷61~70題 Question 61: The Fibonacci Sequence is computed based o...

    jeyhan 評論0 收藏0
  • Python基礎(chǔ)練習(xí)100 ( 51~ 60)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載 刷題繼續(xù) 昨天和大家分享了41-50題,今天繼續(xù)來刷51~60題 Question 51: Write a function to compute 5/0 and use ...

    岳光 評論0 收藏0
  • Python基礎(chǔ)練習(xí)100 ( 31~ 40)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來刷題解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題我的運(yùn)行環(huán)境如果你 刷題繼續(xù) 昨天和大家分享了21-30題,今天繼續(xù)來刷31~40題 Question 31: Define a function which can pr...

    miracledan 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<