我試圖解決從書中探索python的二維隨機遊走問題。 但是,我無法弄清楚我該如何解決這個問題。我做了一些研究,但是這些太複雜了,無法理解它是什麼。我是初學者。所以,我無法通過查看來理解代碼。請詳細解釋我這個問題。另一個簡單的隨機遊走模擬使用Python(二維)
無論如何,問題是:
上隨機遊走二維變化在網格的中間 開始,例如11 11陣列。在每一步醉酒都有四個選擇:上,下,左或右。在本章前面我們描述了 如何創建一個二維數組數組。使用這個數據 類型,寫一個二維隨機遊走的模擬。
好的,我所知道的; 我知道如何在Python創建二維數組:
times = [0] * 11
for i in range(0,11):
times[i] = [0] * 11
而且我得到了「randint」功能的想法:
而且也是我最近寫了這個問題的一個尺寸變化。但它是一個意大利麪代碼,而且它非常髒,我也不確定它是否正確。
我的代碼是在這裏:
'''
Created on Feb 11, 2012
@author: msarialp
'''
from random import randint
def drunken_man():
steps = 0
times = [0] * 11
left_move = 0
right_move = 0
i = 0
while left_move < 5 or right_move < 5:
value = randint(0,1)
times[5] = 1
if value == 1:
steps += 1
print("He moved left")
left_move += 1
if right_move > 0:
right_move -= 1
if left_move == 1:
times[4] += 1
elif left_move == 2:
times[3] += 1
elif left_move == 3:
times[2] += 1
elif left_move == 4:
times[1] += 1
#elif left_move == 5:
#times[0] += 1
elif value == 0:
steps += 1
print("He moved right")
right_move += 1
if left_move > 0:
left_move -= 1
if right_move == 1:
times[6] += 1
elif right_move == 2:
times[7] += 1
elif right_move == 3:
times[8] += 1
elif right_move == 4:
times[9] += 1
#elif right_move == 5:
#times[10] += 1
times[i] += 1
for i in range(1,10):
print("He took {steps} steps until he reaches end of the sidewalk.".format(steps = steps), "He stood on {1} square at {0} times".format(times[i], i))
def main():
drunken_man()
return 0
if __name__ == '__main__':
main()
編輯一個
從丹Gerhardsson採取了一些很好的建議後。 我決定編輯我的問題。 所以,我在這個問題上: 我明白我該如何遵循和檢查我的醉酒男子在兩個步驟的步驟。
使用元組來解決這個練習是非常容易理解和清楚的。
因此,畢竟我的代碼段在這裏,請檢查並給我任何反饋。
def two_dimensional_random_walk():
steps = 0
times = [0] * 11
for i in range(0,11):
times[i] = [0] * 11
x = 5
y = 5
moves = [(1,0), (0,1), (-1,0), (0,-1)]
while x<11 and x >= 0 or y < 11 and y >= 0:
dx, dy = moves[randint(0,3)]
x += dx
y += dy
if dx == 1 and dy == 0:
print("He moved right")
elif dx == 0 and dy == 1:
print("He moved up")
elif dx == -1 and dy == 0:
print("He moved left")
elif dx == 0 and dy == -1:
print("He moved down")
try:
times[x][y] += 1
steps += 1
except IndexError:
break
我的打印功能:
for i in range(0,11):
for j in range(0,11):
print("He took {steps} steps until he reaches end of the sidewalk.".format(steps = steps), "He stood on {1}x{2} square at {0} times".format(times[i][j], i+1,j+1))
因此,所有的一切我想與幫助丹Gerhardsson,我解決了這個運動。
但是,爲什麼我不能用這些提示來改變我的一維解決方案。
def drunken_man():
steps = 0
x = 6
times = [0] * 11
moves = [(1), (-1)]
while x < 11 and x >= 0:
dx = moves[randint(0,1)]
print(dx, x)
x += dx
try:
times[x] += 1
steps += 1
except IndexError:
break
for i in range(1,11):
print("He took {0} steps until he reaches end of the sidewalk.".format(steps), "He stood on {1} square at {0} times".format(times[i], i))
編輯兩個(最後的潤色)
我不知道是否有必要編輯自己的帖子由丹Gerhardsson應用提示。爲了幫助那些錯過了像我這樣的觀點的人,我決定把所有東西結合在一起。
因此,這裏是我的功能與丹Gerhardsson提示合併:
def two_dimensional_random_walk():
steps = 0 # Steps counter for understand how many steps that our drunken man take
grid_size = 11 # Grid size variable,
# Creating Two dimensional array by using lists
times = [0] * grid_size
for i in range(0,grid_size):
times[i] = [0] * grid_size
# Initial variables to start in the middle of grid
x = 5
y = 5
# Tuples to get directions and decide where to go
moves = [(1,0, "right"), (0,1, "up"), (-1,0, "left"), (0,-1, "down")]
# My loop for evaluate the steps
while True:
dx, dy, position = moves[randint(0,3)] # By using randint I could make decision randomly
x += dx
y += dy
print("He moved", position)
try:
times[x][y] += 1 # And here is, how many times have he stood on each square
steps += 1
except IndexError: # The exit of loop
break
# My print function which answers these questions (How long will it be until he reaeches the end of the sidewalk, and how many times will he have stood on each square)
for i in range(0,11):
for j in range(0,11):
print("He took {steps} steps until he reaches end of the sidewalk.".format(steps = steps), "He stood on {1}x{2} square at {0} times".format(times[i][j], i+1,j+1))
感謝您的幫助很大丹Gerhardsson。 我想我終於得到了解決方案。
這功課嗎? – amindfv 2012-02-23 21:59:40
不,我只是在學習Python這本書,http://books.google.com.tr/books/about/Exploring_Python.html?id=VnAsHwAACAAJ&redir_esc=y – mustafaSarialp 2012-02-23 22:03:52