Python 數(shù)據(jù)結(jié)構(gòu)與算法

WHAT

  • 介紹Python數(shù)據(jù)結(jié)構(gòu)與算法第三方庫 可用作學(xué)習(xí)也可用于直接使用算法
  • 介紹pysnooper--man's debugger

algorithms

algorithms第三方庫

  • 根據(jù)目錄學(xué)習(xí)


    {075C5F19-7EAA-4CB0-B5A5-F115464FD08E}_20190827160419.jpg
  • 舉例學(xué)習(xí) josephus問題
    算法源代碼
    josephus問題相關(guān)著名的Josephus問題
"""
介紹算法解決的是什么問題
There are people sitting in a circular fashion,
print every third member while removing them,
the next counter starts immediately after the member is removed.
Print till all the members are exhausted.
For example:
Input: consider 123456789 members sitting in a circular fashion,
Output: 369485271
"""

# 具體算法
def josephus(int_list, skip):
    skip = skip - 1                     # list starts with 0 index
    idx = 0
    len_list = (len(int_list))
    while len_list > 0:
        idx = (skip + idx) % len_list   # hash index to every 3rd
        yield int_list.pop(idx)
        len_list -= 1

pysooper Python運行記錄利器

import pysnooper

# 跟蹤日志記錄
@pysnooper.snoop()
def josephus(int_list, skip):
    skip = skip - 1  # list starts with 0 index
    idx = 0
    len_list = (len(int_list))
    res = []
    while len_list > 0:
        idx = (skip + idx) % len_list  # hash index to every 3rd
        res.append(int_list.pop(idx))
        len_list -= 1
    return res


josephus([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)
  • 輸出日志
  • 是否現(xiàn)在更清楚知道解決思路?
Source path:... C:/Users/74109/Desktop/MxShop-master/l.py
Starting var:.. int_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Starting var:.. skip = 3
15:52:54.569968 call        10 def josephus(int_list, skip):
15:52:54.569968 line        11     skip = skip - 1  # list starts with 0 index
Modified var:.. skip = 2
15:52:54.569968 line        12     idx = 0
New var:....... idx = 0
15:52:54.569968 line        13     len_list = (len(int_list))
New var:....... len_list = 9
15:52:54.569968 line        14     res = []
New var:....... res = []
15:52:54.569968 line        15     while len_list > 0:
15:52:54.569968 line        16         idx = (skip + idx) % len_list  # hash index to every 3rd
Modified var:.. idx = 2
15:52:54.569968 line        17         res.append(int_list.pop(idx))
Modified var:.. int_list = [1, 2, 4, 5, 6, 7, 8, 9]
Modified var:.. res = [3]
15:52:54.569968 line        18         len_list -= 1
Modified var:.. len_list = 8
15:52:54.569968 line        15     while len_list > 0:
15:52:54.569968 line        16         idx = (skip + idx) % len_list  # hash index to every 3rd
Modified var:.. idx = 4
15:52:54.569968 line        17         res.append(int_list.pop(idx))
Modified var:.. int_list = [1, 2, 4, 5, 7, 8, 9]
Modified var:.. res = [3, 6]
15:52:54.569968 line        18         len_list -= 1
Modified var:.. len_list = 7
15:52:54.569968 line        15     while len_list > 0:
15:52:54.569968 line        16         idx = (skip + idx) % len_list  # hash index to every 3rd
Modified var:.. idx = 6
15:52:54.569968 line        17         res.append(int_list.pop(idx))
Modified var:.. int_list = [1, 2, 4, 5, 7, 8]
Modified var:.. res = [3, 6, 9]
15:52:54.569968 line        18         len_list -= 1
Modified var:.. len_list = 6
15:52:54.569968 line        15     while len_list > 0:
15:52:54.569968 line        16         idx = (skip + idx) % len_list  # hash index to every 3rd
Modified var:.. idx = 2
15:52:54.569968 line        17         res.append(int_list.pop(idx))
Modified var:.. int_list = [1, 2, 5, 7, 8]
Modified var:.. res = [3, 6, 9, 4]
15:52:54.569968 line        18         len_list -= 1
Modified var:.. len_list = 5
15:52:54.570964 line        15     while len_list > 0:
15:52:54.570964 line        16         idx = (skip + idx) % len_list  # hash index to every 3rd
Modified var:.. idx = 4
15:52:54.570964 line        17         res.append(int_list.pop(idx))
Modified var:.. int_list = [1, 2, 5, 7]
Modified var:.. res = [3, 6, 9, 4, 8]
15:52:54.570964 line        18         len_list -= 1
Modified var:.. len_list = 4
15:52:54.570964 line        15     while len_list > 0:
15:52:54.570964 line        16         idx = (skip + idx) % len_list  # hash index to every 3rd
Modified var:.. idx = 2
15:52:54.570964 line        17         res.append(int_list.pop(idx))
Modified var:.. int_list = [1, 2, 7]
Modified var:.. res = [3, 6, 9, 4, 8, 5]
15:52:54.570964 line        18         len_list -= 1
Modified var:.. len_list = 3
15:52:54.570964 line        15     while len_list > 0:
15:52:54.570964 line        16         idx = (skip + idx) % len_list  # hash index to every 3rd
Modified var:.. idx = 1
15:52:54.570964 line        17         res.append(int_list.pop(idx))
Modified var:.. int_list = [1, 7]
Modified var:.. res = [3, 6, 9, 4, 8, 5, 2]
15:52:54.570964 line        18         len_list -= 1
Modified var:.. len_list = 2
15:52:54.570964 line        15     while len_list > 0:
15:52:54.570964 line        16         idx = (skip + idx) % len_list  # hash index to every 3rd
15:52:54.570964 line        17         res.append(int_list.pop(idx))
Modified var:.. int_list = [1]
Modified var:.. res = [3, 6, 9, 4, 8, 5, 2, 7]
15:52:54.570964 line        18         len_list -= 1
Modified var:.. len_list = 1
15:52:54.570964 line        15     while len_list > 0:
15:52:54.570964 line        16         idx = (skip + idx) % len_list  # hash index to every 3rd
Modified var:.. idx = 0
15:52:54.570964 line        17         res.append(int_list.pop(idx))
Modified var:.. int_list = []
Modified var:.. res = [3, 6, 9, 4, 8, 5, 2, 7, 1]
15:52:54.570964 line        18         len_list -= 1
Modified var:.. len_list = 0
15:52:54.572959 line        15     while len_list > 0:
15:52:54.572959 line        19     return res
15:52:54.572959 return      19     return res
Return value:.. [3, 6, 9, 4, 8, 5, 2, 7, 1]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末凳枝,一起剝皮案震驚了整個濱河市以舒,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖豌研,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件哮幢,死亡現(xiàn)場離奇詭異,居然都是意外死亡扇救,警方通過查閱死者的電腦和手機刑枝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來迅腔,“玉大人装畅,你說我怎么就攤上這事〔琢遥” “怎么了掠兄?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長锌雀。 經(jīng)常有香客問我蚂夕,道長,這世上最難降的妖魔是什么汤锨? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任双抽,我火速辦了婚禮,結(jié)果婚禮上闲礼,老公的妹妹穿的比我還像新娘牍汹。我一直安慰自己,他們只是感情好柬泽,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布慎菲。 她就那樣靜靜地躺著,像睡著了一般锨并。 火紅的嫁衣襯著肌膚如雪露该。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天第煮,我揣著相機與錄音解幼,去河邊找鬼。 笑死包警,一個胖子當(dāng)著我的面吹牛撵摆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播害晦,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼特铝,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起鲫剿,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤鳄逾,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后灵莲,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體雕凹,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年笆呆,在試婚紗的時候發(fā)現(xiàn)自己被綠了请琳。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片粱挡。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡赠幕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出询筏,到底是詐尸還是另有隱情榕堰,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布嫌套,位于F島的核電站逆屡,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏踱讨。R本人自食惡果不足惜魏蔗,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望痹筛。 院中可真熱鬧莺治,春花似錦、人聲如沸帚稠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽滋早。三九已至榄审,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間杆麸,已是汗流浹背搁进。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留昔头,地道東北人饼问。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像减细,于是被迫代替她去往敵國和親匆瓜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容