Skip to content

Commit

Permalink
[프로그래머스] n + 1 카드게임
Browse files Browse the repository at this point in the history
  • Loading branch information
ehdgua01 committed Aug 30, 2024
1 parent 107173d commit 23f01ba
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: check-yaml
- id: end-of-file-fixer

- repo: https://github.com/psf/black
rev: 21.9b0
rev: 24.8.0
hooks:
- id: black

- repo: https://gitlab.com/PyCQA/flake8
rev: 3.9.2
- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
hooks:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.9.3
rev: v5.10.1
hooks:
- id: isort
48 changes: 48 additions & 0 deletions coding_test/programmers/n_1_card_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import itertools


class Deck:
def __init__(self, cards):
self._cards = cards
self._pos = 0

def draw(self, num):
cards = self._cards[self._pos : self._pos + num]
self._pos += num
return cards

def end_of_deck(self) -> bool:
return self._pos >= len(self._cards)


def solution(coin, cards):
num_of_cards_to_draw = 2
target_numer = len(cards) + 1
deck = Deck(cards)
holds = set(deck.draw(len(cards) // 3))
candidates = set()
for answer in itertools.count(1):
if deck.end_of_deck():
break
candidates = candidates.union(deck.draw(num_of_cards_to_draw))
if check(holds, holds, target_numer):
continue
elif coin and check(holds, candidates, target_numer):
coin -= 1
continue
elif coin > 1 and check(candidates, candidates, target_numer):
coin -= 2
continue
else:
break
return answer


def check(left, right, target_number):
for x in left:
other = target_number - x
if other in right:
left.discard(x)
right.discard(other)
return True
return False

0 comments on commit 23f01ba

Please sign in to comment.