10行Python代碼解決約瑟夫環(類比),10行python

來源:互聯網
上載者:User

10行Python代碼解決約瑟夫環(類比),10行python

寫這篇文章是因為看到別人部落格裡用了很長一個篇幅(超過50行)去解決一個約瑟夫環問題,而且還是用以簡潔著稱的python,另外,如果你用X度搜尋python 約瑟夫,看到得前幾條都是錯的,真是好悲劇。

總的來說,就是誤人子弟。

雖然,用類比去解決這個約瑟夫環問題效率是很低的,但是,這更容易理解。

先上代碼。

def josephus(n,k):    link=range(1,n+1)     ind=0    for loop_i in range(n-1):        ind = (ind+k)% len(link)         ind-=1        print 'Kill:',link[ind]        del link[ind]        if ind==-1: # the last element of link            ind=0    print 'survice :',link[0]   if __name__ == '__main__':    josephus(100000,300)    print '-'*30    josephus(10,5)    print '-'*30    josephus(10,1)
可以看到,整個函數也就是只有十行。

思路非常簡單,按模來找到要刪除得位置,但是,主要到下標從0開始和數字從1開始是有一些不一樣得,另外,py的del後,下標會增1,所以要減回去。

正確看是

del link[ind-1]

ind-=1

但是,因為兩者都需要後退1,所以直接ind-=1就OK了。

另外要主要得是,來到環尾部,即py的-1(這點就是最好的地方,py得tuple 和list 支援負下標),刪除後,開始就要變成0

如果你認為我寫錯了,一定要評論給我指出,不想誤人子弟。


python 指令碼從螢幕輸出讀取資料sysstdin 怎可以每讀取10行的資料輸出一次

import sys

class LineBuffer:
def __init__(self, buffsize=10):
self.buffer = []
self.buffsize=buffsize
def flush(self):
output, self.buffer = self.buffer, []
return output
def append(self, line):
self.buffer.append(line)
if self.buffsize <= len(self.buffer):
return self.flush()

cache = LineBuffer(5)
while True:
ln = sys.stdin.readline()
if ln.strip() == 'EOF':
break
x = cache.append(ln)
if x:
print "-"*32
print ''.join(x)
print "="*32

print "-"*32
print ''.join(cache.flush())
print "="*32

$ python x.py
1
2
3
4
5
--------------------------------
1
2
3
4
5

================================
6
7
8
9
0
--------------------------------
6
7
8
9
0

================================
A
B
C
D
EOF
--------------------------------
A
B
C
D

================================
$
 
python怎有比重的隨機取值? (要代碼簡單高效)

python中沒有你所期望的那種直接搞定的函數,都要自己改造一下的,你現在用的random.randint(1,10)這種方法沒什麼問題,而且並不算笨方法,代碼效率也不算低。
還有一種方法是利用random中的choice函數,random.choice的功能是從序列中擷取一個隨機元素,編程思想是將要隨機輸出的元素按不同個數添加到一個列表中,然後利用random.choice,從中隨機選取一個。
=====================================================
代碼如:
def randomchice(a,b,c,d,fa,fb,fc,fd):
import random
tl=[]
for i in range(0,fa):tl.append(a)
for i in range(0,fb):tl.append(b)
for i in range(0,fc):tl.append(c)
for i in range(0,fd):tl.append(d)
return random.choice(tl)

使用時只需輸入如:
print randomchice('zhangsan','lisi','wangwu','zhouqi',50,20,25,5,)
=====================================================

python所追求的不是速度效率而是代碼效率,用python寫的代碼夠簡潔清晰就行,如果要求速度的話,那可以嵌入c語言,那樣效率比python高至少100倍。
希望對你有所協助。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.