Last active
August 4, 2017 15:51
-
-
Save dongkwan-kim/54e4ba39ad7bc05e8a0cec11a06dbbcf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, sys | |
""" | |
기차에 100명의 승객이 순서대로 탄다. 첫 번째 승객이 티켓을 잃어 버려서 랜덤한 | |
자리에 앉는다. 두 번째 승객부터는 자기 자리가 비어있으면 자기 자리에 앉고, 누가 | |
앉아있으면 랜덤한 자리에 앉는다. 100번째 승객이 자기 자리에 앉을 확률은? | |
""" | |
def run(num): | |
passengers = list(range(1, num+1)) | |
seats = list(range(1, num+1)) | |
for p in passengers: | |
if p == 1 or p not in seats: | |
random.shuffle(seats) | |
selected = seats.pop() | |
else: | |
selected = p | |
del seats[seats.index(p)] | |
return selected | |
# $ python passenger.py 10000 | |
results = [] | |
for i in range(int(sys.argv[1])): | |
results.append(run(100)) | |
print([{x: results.count(x)} for x in set(results)]) | |
""" | |
todo@mymacui-MacBook-Pro:~$ python passenger.py 100000 | |
[{1: 50086}, {100: 49914}] | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment