@Author:Runsen
@Date:2020/5/26
作者介紹:Runsen目前大三下學期,專業化學工程與工藝,大學沉迷日語,Python, Java和一系列數據分析軟件。導致翹課嚴重,專業排名中下。.在大學60%的時間,都在CSDN。決定今天比昨天要更加努力。
第五題
歸併排序和快速排序在最佳情況下都具有時間複雜性。然而,其中一個算法的最壞情況複雜度爲。說明哪些算法的時間複雜度爲,並簡要解釋爲什麼
答案是快速排序
快速排序具有最好的平均性能(average behavior),但最壞性能(worst case behavior)和插入排序
相同,也是。比如一個序列5,4,3,2,1,要排爲1,2,3,4,5。按照快速排序方法,每次只會有一個數據進入正確順序,不能把數據分成大小相當的兩份,很明顯,]時間複雜度就成了O(n^2)。
第二題
在對冒泡排序、選擇排序和插入排序的外部循環進行三次迭代之後,下面使用哪種排序
第一個很明顯選擇排序,選擇排序在未排序序列中找到最小(大)元素,存放到排序序列的起始位置。
第三個很明顯冒泡排序,
那麼第二個很明顯插入排序
第三題,其實就是一個二分查找,只不過變成了查找對應的字符串,變湯不變藥,問題就是當count等於2時候,那麼對應的mid,higth,low。
第一,len(collection) = 10,一開始mid等於4,然後不對,直接low等於5,count等於1,接着mid等於7,不對,直接low等於8,count等於2,
因此答案是mid等於7,higth等於9,low等於8。
最後一題
(20分)你被委託寫一個簡單的遊戲卡遊戲稱爲考試撲克。這場比賽是用一套標準的撲克牌進行的。在您的遊戲中,卡片將被表示爲(等級,套裝)元組的列表。注意數字11-13代表傑克、王后、國王,而我代表王牌。西裝(黑桃鑽石、紅桃和梅花)用一個字母表示。我們將把這些卡片存儲在一個叫做deck的全局抖動結構中。您可以假設在您的程序中定義了以下內容
SCDH就是撲克牌的四種類別,一共有52張撲克牌
玩家最初獲得5張牌。然後,他們還有三個(可選)回合,目標是實現以下其中一個
其中通俗的來說,就是現在你有五張牌,打牌的時候,怎麼可以出五張牌,順子可以打出去,三帶二可以打出去,還有就是鋤大地的同一種花色可以打出去。最後,通過五張牌的總和作爲得分。你可以換牌。
這個代碼寫了我快一個小時,真的很多細節注意
'''
@Author: Runsen
@微信公衆號: 潤森筆記
@博客: https://blog.csdn.net/weixin_44510615
@Date: 2020/5/21
'''
import random
# 定義一副未洗的牌[0, 1....51]
porks = [i for i in range(52)]
# 定義花色
# 方片D(Diamonds) 梅花C(Clubs) 紅桃H(heart) 黑桃S(spade)
suits = ['D', 'C', 'H', 'S']
# 定義編號
ranks = ['1', '2', '3', '4', '5', '6', '7', '8',
'9', '10', '11', '12', '13']
s = 0
def get_porks(mylist):
# 隨機生成一牌,但是要求不能在之前的出現
pork = random.choice(ranks) + random.choice(suits)
if pork in mylist:
# 出現重新生成一牌
return get_porks(mylist)
else:
return pork
def get_five_porks():
mylist = []
for i in range(52):
# 隨機得到0-51整數隨機數
randomIndex = random.randint(0, 51)
# 交換
porks[i], porks[randomIndex] = porks[randomIndex], porks[i]
for i in range(5):
suit = suits[porks[i] // 13]
rank = ranks[porks[i] % 13]
mylist.append(str(rank + suit))
return mylist
def change_porks(mylist):
# 需不需要換牌
global s
print(mylist,s)
Change = input("Change some cards:y or n: ")
if Change == "y":
keep = input("Indicate which cards you wish to keep :")
for index,i in enumerate(keep):
if i == '0':
# 換牌
pork = get_porks(mylist)
print(pork)
mylist.insert(index, pork)
# 在刪除之後的下一個
mylist.pop(index+1)
print("You now have:"+ str(mylist))
change_porks(mylist)
else:
# 計算分數
for i in mylist:
print(i)
s += int(i[:-1])
print("Your total score is now:" +str(s))
return s
def teststraightchecker():
global s
mylist = get_five_porks()
print("Here is your first hand:" + str(mylist))
change_porks(mylist)
choice = input("Would you like to play again (y or n) :")
if choice == 'y':
teststraightchecker()
else:
print("Thank you for playing")
if __name__ == '__main__':
teststraightchecker()
Here is your first hand:['11D', '13H', '3H', '6C', '12C']
Change some cards:y or n: y
Indicate which cards you wish to keep :11001
You now have:['11D', '13H', '3S', '7D', '12C']
Change some cards:y or n: y
Indicate which cards you wish to keep :11001
You now have:['11D', '13H', '11H', '8S', '12C']
Change some cards:y or n: y
Indicate which cards you wish to keep :11101
You now have:['11D', '13H', '11H', '6H', '12C']
Change some cards:y or n: y
Indicate which cards you wish to keep :11100
You now have:['11D', '13H', '11H', '13C', '6C']
Change some cards:y or n: y
Indicate which cards you wish to keep :11110
You now have:['11D', '13H', '11H', '13C', '13S']
Change some cards:y or n: n
Your total score is now:61
Would you like to play again (y or n) :n
Thank you for playing