From 4e2ac2c531aa0603534bc72e37343af5fa61fbca Mon Sep 17 00:00:00 2001 From: jannahsherif Date: Wed, 5 Jun 2024 11:51:43 +0300 Subject: [PATCH 1/5] two positive integers --- calculator.py | 3 ++- test_calculator.py | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/calculator.py b/calculator.py index 5717061..6ddeaf0 100644 --- a/calculator.py +++ b/calculator.py @@ -56,4 +56,5 @@ def add(s: str) -> str: '6' >>> """ - return "7" # TODO: Implement the function logic. + + return str(7) # TODO: Implement the function logic. diff --git a/test_calculator.py b/test_calculator.py index d283522..5b5e563 100644 --- a/test_calculator.py +++ b/test_calculator.py @@ -19,3 +19,5 @@ def test_basic_add(): Tests addition. This test is written for you, extend it and add others! """ assert add("5,2") == "7", "Failed on 5+2==7" + + From 1f1a7dece54134d2fcf9a6b3d6a116e5437412ff Mon Sep 17 00:00:00 2001 From: jannahsherif Date: Wed, 5 Jun 2024 11:53:53 +0300 Subject: [PATCH 2/5] empty string --- calculator.py | 3 ++- test_calculator.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/calculator.py b/calculator.py index 6ddeaf0..ffff009 100644 --- a/calculator.py +++ b/calculator.py @@ -56,5 +56,6 @@ def add(s: str) -> str: '6' >>> """ - + if s == "": + return str(0) return str(7) # TODO: Implement the function logic. diff --git a/test_calculator.py b/test_calculator.py index 5b5e563..966660d 100644 --- a/test_calculator.py +++ b/test_calculator.py @@ -19,5 +19,6 @@ def test_basic_add(): Tests addition. This test is written for you, extend it and add others! """ assert add("5,2") == "7", "Failed on 5+2==7" + assert add("") == "0" From 899e2669c4efaae5cf1c047b0792a5995b5d5f8a Mon Sep 17 00:00:00 2001 From: jannahsherif Date: Wed, 5 Jun 2024 12:02:01 +0300 Subject: [PATCH 3/5] any two one digit integers --- calculator.py | 7 +++++++ test_calculator.py | 1 + 2 files changed, 8 insertions(+) diff --git a/calculator.py b/calculator.py index ffff009..461f3f8 100644 --- a/calculator.py +++ b/calculator.py @@ -58,4 +58,11 @@ def add(s: str) -> str: """ if s == "": return str(0) + if "-" not in s: + x = int(s[0]) + print(x) + y = int(s[2]) + print(y) + z = x + y + return str(z) return str(7) # TODO: Implement the function logic. diff --git a/test_calculator.py b/test_calculator.py index 966660d..a8c39ba 100644 --- a/test_calculator.py +++ b/test_calculator.py @@ -20,5 +20,6 @@ def test_basic_add(): """ assert add("5,2") == "7", "Failed on 5+2==7" assert add("") == "0" + assert add("1,3") == "4" From beb98ca6ddedfeebc8d30dfe755be3364df7b1ca Mon Sep 17 00:00:00 2001 From: jannahsherif Date: Wed, 5 Jun 2024 12:09:02 +0300 Subject: [PATCH 4/5] any two integers --- calculator.py | 8 +++++--- test_calculator.py | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/calculator.py b/calculator.py index 461f3f8..81258af 100644 --- a/calculator.py +++ b/calculator.py @@ -59,10 +59,12 @@ def add(s: str) -> str: if s == "": return str(0) if "-" not in s: - x = int(s[0]) + start = 0 + mid = s.index(",") + x = int(s[start:mid]) print(x) - y = int(s[2]) + y = int(s[mid+1:]) print(y) z = x + y return str(z) - return str(7) # TODO: Implement the function logic. + diff --git a/test_calculator.py b/test_calculator.py index a8c39ba..0ec10e8 100644 --- a/test_calculator.py +++ b/test_calculator.py @@ -21,5 +21,6 @@ def test_basic_add(): assert add("5,2") == "7", "Failed on 5+2==7" assert add("") == "0" assert add("1,3") == "4" + assert add("30,45") == "75" From c34a70eacd86a2e0ec11b37ca336c99ce819b4b5 Mon Sep 17 00:00:00 2001 From: jannahsherif Date: Wed, 5 Jun 2024 12:21:46 +0300 Subject: [PATCH 5/5] all --- calculator.py | 13 ++----------- test_calculator.py | 1 + 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/calculator.py b/calculator.py index 81258af..701633b 100644 --- a/calculator.py +++ b/calculator.py @@ -56,15 +56,6 @@ def add(s: str) -> str: '6' >>> """ - if s == "": - return str(0) - if "-" not in s: - start = 0 - mid = s.index(",") - x = int(s[start:mid]) - print(x) - y = int(s[mid+1:]) - print(y) - z = x + y - return str(z) + if not s: return "0" + return str(sum(list(map(int, s.split(","))) + [0])) diff --git a/test_calculator.py b/test_calculator.py index 0ec10e8..08c73bc 100644 --- a/test_calculator.py +++ b/test_calculator.py @@ -22,5 +22,6 @@ def test_basic_add(): assert add("") == "0" assert add("1,3") == "4" assert add("30,45") == "75" + assert add("20,30,45") == "95"