From 48be97746ac547a662d70a37a4875f5ad869f611 Mon Sep 17 00:00:00 2001 From: KDH Date: Thu, 12 Sep 2024 23:42:34 +0900 Subject: [PATCH] =?UTF-8?q?[=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=98=EB=A8=B8?= =?UTF-8?q?=EC=8A=A4]=20N=EC=9C=BC=EB=A1=9C=20=ED=91=9C=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coding_test/programmers/dp/express_by_n.py | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 coding_test/programmers/dp/express_by_n.py diff --git a/coding_test/programmers/dp/express_by_n.py b/coding_test/programmers/dp/express_by_n.py new file mode 100644 index 0000000..a1c9218 --- /dev/null +++ b/coding_test/programmers/dp/express_by_n.py @@ -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