我需要模擬一個類似撲克桌的隊列,用戶根據第一個可用座位獲取他們的位置。 我已經看到它完成列表,但我已經使用按位操作來解決它。這是模擬「撲克桌」座位排隊的最有效方式嗎?
def add_user_to_table(max_users=8):
global tbl
# simulate the first position: 00000001
pos = 1
# iterate through all positions. break once found empty position
for i in range (0, max_users):
if not (tbl & pos):
# found an empty position...
# return position, break, etc...
else:
# shift the bit 1 position left to check the next position
# so after the 1st iteration pos will be 00000010
pos = pos << 1
# main. Define a table with some users seated
# a table with seats 1, 3, 4, 6 taken
tbl = int('00101101', 2)
# now place the user on the first available seat (second seat in this example)
add_user_to_table()
在性能方面(我將需要數千個用戶數以千計的表),這將是最快和最有效的方式嗎?
list/queues/deques等會超出這個方法嗎?
也許更適合在http://codereview.stackexchange.com? – 2012-02-01 15:35:10
感謝您的注意。固定。 – user1102018 2012-02-01 15:38:50