From a619439f02fdc058b44e9f26b91d00af6dc862bd Mon Sep 17 00:00:00 2001 From: shining-ai Date: Mon, 13 May 2024 17:28:47 +0000 Subject: [PATCH 1/6] =?UTF-8?q?=E3=80=90Grind75Hard=E3=80=916=E5=95=8F?= =?UTF-8?q?=E7=9B=AE224.=20Basic=20Calculator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../06_224_Basic Calculator/level_1.py | 39 +++++++++++++++++++ .../06_224_Basic Calculator/level_2.py | 29 ++++++++++++++ .../06_224_Basic Calculator/level_3.py | 28 +++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 grind75_hard/06_224_Basic Calculator/level_1.py create mode 100644 grind75_hard/06_224_Basic Calculator/level_2.py create mode 100644 grind75_hard/06_224_Basic Calculator/level_3.py diff --git a/grind75_hard/06_224_Basic Calculator/level_1.py b/grind75_hard/06_224_Basic Calculator/level_1.py new file mode 100644 index 0000000..b83ab54 --- /dev/null +++ b/grind75_hard/06_224_Basic Calculator/level_1.py @@ -0,0 +1,39 @@ +# 数字符号も含め10進数に直して、最後に全部足し算する +# ()で囲まれた部分を先に計算するため、stackに入れておく +# 各文字に対する処理をそれぞれ記載 +class Solution: + def calculate(self, s: str) -> int: + stack = [] + sign = 1 + num = 0 + for c in s: + if c.isspace(): + continue + if c.isdigit(): + num = num * 10 + int(c) + continue + if c == "(": + stack.append((c, sign)) + sign = 1 + continue + if c == "-": + stack.append((num, sign)) + sign = -1 + if c == "+": + stack.append((num, sign)) + sign = 1 + if c == ")": + num *= sign + while 1: + expression, sign = stack.pop() + if expression == "(": + break + num += expression * sign + stack.append((num, sign)) + num = 0 + if num != 0: + stack.append((num, sign)) + result = 0 + for num, sign in stack: + result += num * sign + return result diff --git a/grind75_hard/06_224_Basic Calculator/level_2.py b/grind75_hard/06_224_Basic Calculator/level_2.py new file mode 100644 index 0000000..b8461c9 --- /dev/null +++ b/grind75_hard/06_224_Basic Calculator/level_2.py @@ -0,0 +1,29 @@ +# 括弧内の計算を再帰的に行う +class Solution: + def calculate(self, s: str) -> int: + def helper_calculate(index): + result = 0 + num = 0 + sign = 1 + while index < len(s): + if s[index].isdigit(): + num = num * 10 + int(s[index]) + if s[index] == "+": + result += num * sign + num = 0 + sign = 1 + if s[index] == "-": + result += num * sign + num = 0 + sign = -1 + if s[index] == ")": + result += num * sign + return index, result + if s[index] == "(": + index, result_in_brackers = helper_calculate(index + 1) + result += result_in_brackers * sign + index += 1 + result += num * sign + return result + + return helper_calculate(0) diff --git a/grind75_hard/06_224_Basic Calculator/level_3.py b/grind75_hard/06_224_Basic Calculator/level_3.py new file mode 100644 index 0000000..d93808b --- /dev/null +++ b/grind75_hard/06_224_Basic Calculator/level_3.py @@ -0,0 +1,28 @@ +class Solution: + def calculate(self, s: str) -> int: + def helper_calculate(index): + num = 0 + sign = 1 + result = 0 + while index < len(s): + if s[index].isdigit(): + num = 10 * num + int(s[index]) + if s[index] == "+": + result += num * sign + num = 0 + sign = 1 + if s[index] == "-": + result += num * sign + num = 0 + sign = -1 + if s[index] == ")": + result += num * sign + return result, index + if s[index] == "(": + result_in_brackets, index = helper_calculate(index + 1) + result += result_in_brackets * sign + index += 1 + result += num * sign + return result + + return helper_calculate(0) From 3db3a27ce1802f2a0c0429c2894c8c84a6fcebb6 Mon Sep 17 00:00:00 2001 From: shining-ai Date: Tue, 14 May 2024 19:19:11 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=E5=86=8D=E5=B8=B0=E4=B8=8B=E9=99=8D?= =?UTF-8?q?=E6=A7=8B=E6=96=87=E8=A7=A3=E6=9E=90=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../06_224_Basic Calculator/level_4.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 grind75_hard/06_224_Basic Calculator/level_4.py diff --git a/grind75_hard/06_224_Basic Calculator/level_4.py b/grind75_hard/06_224_Basic Calculator/level_4.py new file mode 100644 index 0000000..96df556 --- /dev/null +++ b/grind75_hard/06_224_Basic Calculator/level_4.py @@ -0,0 +1,38 @@ +# 再帰下降構文解析 +class Solution: + def calculate(self, s: str) -> int: + index = 0 + + def get_next_expression(): + nonlocal index + index += 1 + while index < len(s) and s[index].isspace(): + index += 1 + + def expr(): + nonlocal index + result = factor() + while index < len(s) and s[index] in "+-": + op = s[index] + get_next_expression() + if op == "+": + result += factor() + else: + result -= factor() + return result + + def factor(): + nonlocal index + result = 0 + while index < len(s) and s[index].isdigit(): + result = 10 * result + int(s[index]) + get_next_expression() + if index < len(s) and s[index] == "(": + get_next_expression() + result = expr() + get_next_expression() # ")"を読み飛ばす + return result + + if index < len(s) and s[0].isspace(): + get_next_expression() + return expr() From f02076288cf7f504729b6f35acaaeecc07e3a500 Mon Sep 17 00:00:00 2001 From: shining-ai Date: Wed, 15 May 2024 02:54:25 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=81=AE=E5=86=85=E5=AE=B9=E3=82=92=E5=8F=8D=E6=98=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../06_224_Basic Calculator/level_5.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 grind75_hard/06_224_Basic Calculator/level_5.py diff --git a/grind75_hard/06_224_Basic Calculator/level_5.py b/grind75_hard/06_224_Basic Calculator/level_5.py new file mode 100644 index 0000000..543afb3 --- /dev/null +++ b/grind75_hard/06_224_Basic Calculator/level_5.py @@ -0,0 +1,39 @@ +# 再帰下降構文解析 +class Solution: + def calculate(self, s: str) -> int: + index = 0 + + def skip_spaces(): + nonlocal index + index += 1 + while index < len(s) and s[index].isspace(): + index += 1 + + def expr(): + nonlocal index + result = factor() + while index < len(s) and s[index] in "+-": + op = s[index] + skip_spaces() + if op == "+": + result += factor() + else: + result -= factor() + return result + + def factor(): + nonlocal index + result = 0 + if index < len(s) and s[index] == "(": + skip_spaces() + result = expr() + skip_spaces() # ")"を読み飛ばす + return result + while index < len(s) and s[index].isdigit(): + result = 10 * result + int(s[index]) + skip_spaces() + return result + + if index < len(s) and s[0].isspace(): + skip_spaces() + return expr() From 2c68e3809c964e3577fa97b335974e5b549d761f Mon Sep 17 00:00:00 2001 From: shining-ai Date: Wed, 15 May 2024 03:02:02 +0000 Subject: [PATCH 4/6] =?UTF-8?q?=E3=82=B9=E3=83=9A=E3=83=BC=E3=82=B9?= =?UTF-8?q?=E3=82=92=E6=9C=80=E5=88=9D=E3=81=AB=E5=8F=96=E3=82=8A=E9=99=A4?= =?UTF-8?q?=E3=81=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- grind75_hard/06_224_Basic Calculator/level_5.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/grind75_hard/06_224_Basic Calculator/level_5.py b/grind75_hard/06_224_Basic Calculator/level_5.py index 543afb3..64bda93 100644 --- a/grind75_hard/06_224_Basic Calculator/level_5.py +++ b/grind75_hard/06_224_Basic Calculator/level_5.py @@ -3,18 +3,12 @@ class Solution: def calculate(self, s: str) -> int: index = 0 - def skip_spaces(): - nonlocal index - index += 1 - while index < len(s) and s[index].isspace(): - index += 1 - def expr(): nonlocal index result = factor() while index < len(s) and s[index] in "+-": op = s[index] - skip_spaces() + index += 1 if op == "+": result += factor() else: @@ -25,15 +19,14 @@ def factor(): nonlocal index result = 0 if index < len(s) and s[index] == "(": - skip_spaces() + index += 1 result = expr() - skip_spaces() # ")"を読み飛ばす + index += 1 # ")"を読み飛ばす return result while index < len(s) and s[index].isdigit(): result = 10 * result + int(s[index]) - skip_spaces() + index += 1 return result - if index < len(s) and s[0].isspace(): - skip_spaces() + s = s.replace(" ", "") return expr() From 4d67ead979e5afec3aaafbfe4944b55401ae2459 Mon Sep 17 00:00:00 2001 From: shining-ai Date: Wed, 15 May 2024 13:15:28 +0000 Subject: [PATCH 5/6] =?UTF-8?q?Revert=20"=E3=82=B9=E3=83=9A=E3=83=BC?= =?UTF-8?q?=E3=82=B9=E3=82=92=E6=9C=80=E5=88=9D=E3=81=AB=E5=8F=96=E3=82=8A?= =?UTF-8?q?=E9=99=A4=E3=81=8F"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 2c68e3809c964e3577fa97b335974e5b549d761f. --- grind75_hard/06_224_Basic Calculator/level_5.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/grind75_hard/06_224_Basic Calculator/level_5.py b/grind75_hard/06_224_Basic Calculator/level_5.py index 64bda93..543afb3 100644 --- a/grind75_hard/06_224_Basic Calculator/level_5.py +++ b/grind75_hard/06_224_Basic Calculator/level_5.py @@ -3,12 +3,18 @@ class Solution: def calculate(self, s: str) -> int: index = 0 + def skip_spaces(): + nonlocal index + index += 1 + while index < len(s) and s[index].isspace(): + index += 1 + def expr(): nonlocal index result = factor() while index < len(s) and s[index] in "+-": op = s[index] - index += 1 + skip_spaces() if op == "+": result += factor() else: @@ -19,14 +25,15 @@ def factor(): nonlocal index result = 0 if index < len(s) and s[index] == "(": - index += 1 + skip_spaces() result = expr() - index += 1 # ")"を読み飛ばす + skip_spaces() # ")"を読み飛ばす return result while index < len(s) and s[index].isdigit(): result = 10 * result + int(s[index]) - index += 1 + skip_spaces() return result - s = s.replace(" ", "") + if index < len(s) and s[0].isspace(): + skip_spaces() return expr() From de5f962378c1a285352dfe50aeab9280e9224f4a Mon Sep 17 00:00:00 2001 From: shining-ai Date: Wed, 15 May 2024 16:18:10 +0000 Subject: [PATCH 6/6] =?UTF-8?q?level=5F6=E3=82=92=E8=BF=BD=E5=8A=A0(level?= =?UTF-8?q?=5F5=E3=81=AE=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=82=92?= =?UTF-8?q?=E5=8F=8D=E6=98=A0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../06_224_Basic Calculator/level_6.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 grind75_hard/06_224_Basic Calculator/level_6.py diff --git a/grind75_hard/06_224_Basic Calculator/level_6.py b/grind75_hard/06_224_Basic Calculator/level_6.py new file mode 100644 index 0000000..495075d --- /dev/null +++ b/grind75_hard/06_224_Basic Calculator/level_6.py @@ -0,0 +1,40 @@ +# 再帰下降構文解析 +# スペースを最初に除去 +# result = 0のタイミングを修正 +# 単行演算子を明示的に処理 +class Solution: + def calculate(self, s: str) -> int: + index = 0 + + def expr(): + nonlocal index + if index < len(s) and s[index] == "-": + index += 1 + result = -factor() + else: + result = factor() + + while index < len(s) and s[index] in "+-": + op = s[index] + index += 1 + if op == "+": + result += factor() + else: + result -= factor() + return result + + def factor(): + nonlocal index + if index < len(s) and s[index] == "(": + index += 1 + result = expr() + index += 1 # ")"を読み飛ばす + return result + result = 0 + while index < len(s) and s[index].isdigit(): + result = 10 * result + int(s[index]) + index += 1 + return result + + s = s.replace(" ", "") + return expr()