From 695e7252c3fd48ea299df3cf2c506c7bfa3b34b8 Mon Sep 17 00:00:00 2001 From: Roagolden Date: Tue, 2 May 2023 14:43:02 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat=20:=20Bulls=20and=20cows=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EC=8B=9C=EC=9E=91=EC=9D=84=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=EB=A5=BC=20=EA=B5=AC=ED=98=84=ED=95=A9?= =?UTF-8?q?=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1에서 9까지 서로 다른 숫자 4개로 이루어진 비밀 코드를 생성합니다. 비밀 코드와 추측한 숫자를 비교하여 불과 카우의 개수를 계산합니다. --- fugitives.ipynb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 fugitives.ipynb diff --git a/fugitives.ipynb b/fugitives.ipynb new file mode 100644 index 0000000..15ad949 --- /dev/null +++ b/fugitives.ipynb @@ -0,0 +1,38 @@ +import random +def generate_secret_code(): + """ + 1에서 9까지 서로 다른 숫자 4개로 이루어진 비밀 코드를 생성합니다. + """ + numbers = list(range(1, 10)) + random.shuffle(numbers) + return numbers[:4] +def count_bulls_and_cows(secret_code, guess): + """ + 비밀 코드와 추측한 숫자를 비교하여 불과 카우의 개수를 계산합니다. + """ + bulls, cows = 0, 0 + for i in range(4): + if guess[i] == secret_code[i]: + bulls += 1 + elif guess[i] in secret_code: + cows += 1 + return bulls, cows +# 게임 시작 +secret_code = generate_secret_code() +print("불스 앤 카우스 게임을 시작합니다. 1에서 9까지 서로 다른 숫자 4개를 맞춰보세요.") +num_attempts = 0 +while num_attempts < 10: + num_attempts += 1 + guess = input(f"{num_attempts}번째 추측: ") + guess = [int(n) for n in guess if n.isdigit()] + if len(guess) != 4 or len(set(guess)) != 4: + print("잘못된 입력입니다. 1에서 9까지 서로 다른 숫자 4개를 입력해주세요.") + continue + bulls, cows = count_bulls_and_cows(secret_code, guess) + print(f"{bulls} 불, {cows} 카우") + if bulls == 4: + print("축하합니다! 비밀 코드를 맞추셨습니다.") + break +else: + print("게임 오버! 10번의 시도 기회를 모두 사용하셨습니다. 비밀 코드는", secret_code, "였습니다.") + From dfd66f007abaf3d98849636ee41146a637786caa Mon Sep 17 00:00:00 2001 From: Roagolden <96064346+Roagolden@users.noreply.github.com> Date: Tue, 2 May 2023 16:26:07 +0900 Subject: [PATCH 2/2] Update fugitives.ipynb --- fugitives.ipynb | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/fugitives.ipynb b/fugitives.ipynb index 15ad949..79be30a 100644 --- a/fugitives.ipynb +++ b/fugitives.ipynb @@ -1,11 +1,4 @@ -import random -def generate_secret_code(): - """ - 1에서 9까지 서로 다른 숫자 4개로 이루어진 비밀 코드를 생성합니다. - """ - numbers = list(range(1, 10)) - random.shuffle(numbers) - return numbers[:4] + def count_bulls_and_cows(secret_code, guess): """ 비밀 코드와 추측한 숫자를 비교하여 불과 카우의 개수를 계산합니다. @@ -17,22 +10,4 @@ def count_bulls_and_cows(secret_code, guess): elif guess[i] in secret_code: cows += 1 return bulls, cows -# 게임 시작 -secret_code = generate_secret_code() -print("불스 앤 카우스 게임을 시작합니다. 1에서 9까지 서로 다른 숫자 4개를 맞춰보세요.") -num_attempts = 0 -while num_attempts < 10: - num_attempts += 1 - guess = input(f"{num_attempts}번째 추측: ") - guess = [int(n) for n in guess if n.isdigit()] - if len(guess) != 4 or len(set(guess)) != 4: - print("잘못된 입력입니다. 1에서 9까지 서로 다른 숫자 4개를 입력해주세요.") - continue - bulls, cows = count_bulls_and_cows(secret_code, guess) - print(f"{bulls} 불, {cows} 카우") - if bulls == 4: - print("축하합니다! 비밀 코드를 맞추셨습니다.") - break -else: - print("게임 오버! 10번의 시도 기회를 모두 사용하셨습니다. 비밀 코드는", secret_code, "였습니다.")