파이썬 독학기 [Pearl Box] - Empire of Code
IT 어떻게든 혼자 해결해보자/IT 파이썬 독학 입문기2018. 4. 5. 00:56
파이썬 프로그래밍 시작한지 한달째 인데
왼손코딩님 유튜브 보다가 코딩게임("Empire of Code")을 알게 되어 열심히 하고 있습니다.
게임형식은 디펜스 웹게임 스타일(건축, 공격, 방어로 이루어지는 전형적인 웹게임)입니다.
그래픽도 깔끔하고 코딩문제를 풀면 건물 효율성도 좋아지고 재밌게 플레이 할 수 있습니다.
지금 제 Empire 모습입니다.
아직 시작한지 3일밖에 안되어서 썰렁하지만, 앞으로 마구마구 채워넣어야 겠습니다.
오늘 막혀서 엄청 오랜시간 고민했던 Pearl Box를 소개하겠습니다.
Let's play with pearls. 진주로 게임 해봅시다.
To start the game, 게임을 시작하기 위해
robots put several black and white pearls in one of the boxes.
로봇은 상자 중 하나에 여러 개 흑백의 진주를 넣습니다.
For each move, the robots take a pearl out of the box and put one of the opposite color back.
움직일 때마다 로봇은 진주를 상자에서 꺼내어 반대쪽 색을 다시 넣습니다.
The winner is the one who pulls the white pearl on the Nth step.
승자는 N 번째 단계에서 흰색 진주를 가져 오는 사람이 됩니다.
Our robots don't like indeterminacy and want to know the probability of a white pearl on the Nth step.
로봇은 불확정성을 좋아하지 않아서 N 번째 단계에서의 흰색 진주가 나올 가능성을 알고 싶어합니다.
The probability is a value between 0 (0% chance or will not happen) and 1 (100% chance or will happen).
확률은 0 (확률 0 % 또는 발생하지 않음)과 1 (확률 100 % 또는 발생 간격) 사이의 값입니다.
The result is a float from 0 to 1 with two digits precision (±0.01).
결과는 두 자리 정밀도 (± 0.01)로 0에서 1까지의 부동 소수점 숫자입니다.
You are given a start set of pearls as a string that contains "b" (black) and "w" (white) and the number of the step (N).
"b"(검정색)와 "w"(흰색) 및 단계 수 (N)를 포함하는 문자열로 진주의 시작 세트가 제공됩니다.
The order of the pearls does not matter.
진주의 순서는 중요하지 않습니다.
Input: The start sequence of the pearls as a string and the step number as an integer.
입력 : 진주의 시작 순서(문자열), 단계 수 (정수)
Output: The probability for a white pearl as a number.
출력 : 하얀 진주가 될 확률.
어떠신가요? 감이 오시나요?
확률문제를 알려면 이산수학을 공부를 해야겠어요 진짜.. (다들 칸아카데미를 추천하시던데)
풀이는 아래에 올리겠습니다. 생각해보는 시간을 가집시다아!
출처: https://github.com/kpbochenek/empireofcode/blob/master/pearl_box.py
cache = {} | |
def count(marbles, step): | |
ctest = (marbles.count('w'), marbles.count('b'), step) | |
if ctest in cache: return cache[ctest] | |
if step == 0: return (marbles.count('w') / len(marbles), 1) | |
wp, c = 0, 0 | |
for i in range(len(marbles)): | |
if marbles[i] == 'b': | |
(wpx, cx) = count(marbles[:i] + 'w' + marbles[i+1:], step-1) | |
else: | |
(wpx, cx) = count(marbles[:i] + 'b' + marbles[i+1:], step-1) | |
wp, c = wp+wpx, c+cx | |
cache[ctest] = (wp, c) | |
return (wp,c) | |
def probability(marbles, step): | |
(wp, c) = count(marbles, step-1) | |
return round(wp / c, 2) | |
'IT 어떻게든 혼자 해결해보자 > IT 파이썬 독학 입문기' 카테고리의 다른 글
파이썬 독학기 [Broken Reports] - Empire of Code (0) | 2018.04.10 |
---|---|
파이썬 독학기 [Bird Language] - Empire of Code (0) | 2018.04.10 |
패키지 Strat! 도트를 이용한 모듈의 관리체계 (0) | 2018.03.07 |
모듈이란 함수, 변수, 클래스를 모아 놓은 파일 (0) | 2018.03.07 |
생성자(Constructor), 클래스 활용하기 (0) | 2018.03.05 |
댓글