Skip to content

Commit

Permalink
[프로그래머스] N으로 표현
Browse files Browse the repository at this point in the history
  • Loading branch information
ehdgua01 committed Sep 12, 2024
1 parent 64656da commit 48be977
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions coding_test/programmers/dp/express_by_n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def solution(n, number):
answer = -1
dp = []
for i in range(1, 9):
numbers = set()
numbers.add(int(str(n) * i))
for j in range(0, i - 1):
for x in dp[j]:
for y in dp[-j - 1]:
numbers.add(x + y)
numbers.add(x - y)
numbers.add(x * y)
try:
numbers.add(x // y)
except ZeroDivisionError:
...
if number in numbers:
return i
dp.append(numbers)
return answer


def test_solution():
assert solution(3, 9) == 2
assert solution(5, 12) == 4
assert solution(2, 11) == 3
assert solution(4, 17) == 4
assert solution(4, 22) == 5

0 comments on commit 48be977

Please sign in to comment.