From 85009883b2c8a12785767843361ed622f24aaa45 Mon Sep 17 00:00:00 2001 From: naveennamani Date: Fri, 22 Oct 2021 15:28:57 +0530 Subject: [PATCH 1/8] New solution for Euler problem 67 A faster and memory efficient solution based on the template of sol1.py. Modified the solution to be more memory efficient while reading and generating the array and during the solution finding. No conditions and straightforward logic. --- project_euler/problem_067/sol2.py | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 project_euler/problem_067/sol2.py diff --git a/project_euler/problem_067/sol2.py b/project_euler/problem_067/sol2.py new file mode 100644 index 000000000000..ebe255d91aa4 --- /dev/null +++ b/project_euler/problem_067/sol2.py @@ -0,0 +1,41 @@ +""" +Problem Statement: +By starting at the top of the triangle below and moving to adjacent numbers on +the row below, the maximum total from top to bottom is 23. +3 +7 4 +2 4 6 +8 5 9 3 +That is, 3 + 7 + 4 + 9 = 23. +Find the maximum total from top to bottom in triangle.txt (right click and +'Save Link/Target As...'), a 15K text file containing a triangle with +one-hundred rows. +""" +import os + + +def solution(): + """ + Finds the maximum total in a triangle as described by the problem statement + above. + >>> solution() + 7273 + """ + script_dir = os.path.dirname(os.path.realpath(__file__)) + triangle = os.path.join(script_dir, "triangle.txt") + + a = [] + with open(triangle) as f: + for line in f: + a.append(list(map(int, line.split(" ")))) + + while len(a) != 1: + b = a.pop() + x = a[-1] + for j in range(len(b) - 1): + x[j] += max(b[j], b[j + 1]) + return a[0][0] + + +if __name__ == "__main__": + print(solution()) From b3e765c895be235ad06abbce1863252a48d75e1d Mon Sep 17 00:00:00 2001 From: naveennamani Date: Fri, 22 Oct 2021 17:51:13 +0530 Subject: [PATCH 2/8] added return type hint --- project_euler/problem_067/sol2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_067/sol2.py b/project_euler/problem_067/sol2.py index ebe255d91aa4..de5f9d008d0b 100644 --- a/project_euler/problem_067/sol2.py +++ b/project_euler/problem_067/sol2.py @@ -14,7 +14,7 @@ import os -def solution(): +def solution() -> int: """ Finds the maximum total in a triangle as described by the problem statement above. From d857d0f86b8c12544b656f0097881b2cb92de18d Mon Sep 17 00:00:00 2001 From: naveennamani Date: Mon, 25 Oct 2021 22:20:23 +0530 Subject: [PATCH 3/8] Update project_euler/problem_067/sol2.py Preferring comprehensions over map Co-authored-by: Christian Clauss --- project_euler/problem_067/sol2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_067/sol2.py b/project_euler/problem_067/sol2.py index de5f9d008d0b..d51d9f18787e 100644 --- a/project_euler/problem_067/sol2.py +++ b/project_euler/problem_067/sol2.py @@ -27,7 +27,7 @@ def solution() -> int: a = [] with open(triangle) as f: for line in f: - a.append(list(map(int, line.split(" ")))) + a.append([int(i) for i in line.split()]) while len(a) != 1: b = a.pop() From a978237e768f5c08a18a77f309a661728b0ca436 Mon Sep 17 00:00:00 2001 From: Naveen Namani Date: Thu, 28 Oct 2021 20:59:11 +0530 Subject: [PATCH 4/8] Update sol2.py Self explanatory variable names --- project_euler/problem_067/sol2.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/project_euler/problem_067/sol2.py b/project_euler/problem_067/sol2.py index d51d9f18787e..5de5a55732b6 100644 --- a/project_euler/problem_067/sol2.py +++ b/project_euler/problem_067/sol2.py @@ -22,19 +22,19 @@ def solution() -> int: 7273 """ script_dir = os.path.dirname(os.path.realpath(__file__)) - triangle = os.path.join(script_dir, "triangle.txt") + triangle_path = os.path.join(script_dir, "triangle.txt") - a = [] - with open(triangle) as f: + triangle = [] + with open(triangle_path) as f: for line in f: - a.append([int(i) for i in line.split()]) + triangle.append([int(i) for i in line.split()]) - while len(a) != 1: - b = a.pop() - x = a[-1] - for j in range(len(b) - 1): - x[j] += max(b[j], b[j + 1]) - return a[0][0] + while len(triangle) != 1: + last_row = triangle.pop() + curr_row = triangle[-1] + for j in range(len(last_row) - 1): + curr_row[j] += max(last_row[j], last_row[j + 1]) + return triangle[0][0] if __name__ == "__main__": From 1d69acf2b9667b7218f3074747ae867ce7efdc35 Mon Sep 17 00:00:00 2001 From: Naveen Namani Date: Thu, 28 Oct 2021 21:07:34 +0530 Subject: [PATCH 5/8] Updated sol2 to problem 067 in directory --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 10149eac5aac..26ab11da9a8d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -754,6 +754,7 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_065/sol1.py) * Problem 067 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_067/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_067/sol2.py) * Problem 069 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_069/sol1.py) * Problem 070 From 9435612f8a8679a3cf5ed9ba60b53ed2081122fc Mon Sep 17 00:00:00 2001 From: Naveen Namani Date: Thu, 28 Oct 2021 21:45:13 +0530 Subject: [PATCH 6/8] Update project_euler/problem_067/sol2.py Co-authored-by: Christian Clauss --- project_euler/problem_067/sol2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_067/sol2.py b/project_euler/problem_067/sol2.py index 5de5a55732b6..54612d9d6303 100644 --- a/project_euler/problem_067/sol2.py +++ b/project_euler/problem_067/sol2.py @@ -27,7 +27,7 @@ def solution() -> int: triangle = [] with open(triangle_path) as f: for line in f: - triangle.append([int(i) for i in line.split()]) + triangle = [[int(i) for i in line.split()] for line in in_file] while len(triangle) != 1: last_row = triangle.pop() From 24e123f8669b62725cd49c81227f3c76257c3580 Mon Sep 17 00:00:00 2001 From: Naveen Namani Date: Thu, 28 Oct 2021 21:45:40 +0530 Subject: [PATCH 7/8] Update project_euler/problem_067/sol2.py Co-authored-by: Christian Clauss --- project_euler/problem_067/sol2.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/project_euler/problem_067/sol2.py b/project_euler/problem_067/sol2.py index 54612d9d6303..85546a093a9b 100644 --- a/project_euler/problem_067/sol2.py +++ b/project_euler/problem_067/sol2.py @@ -24,8 +24,7 @@ def solution() -> int: script_dir = os.path.dirname(os.path.realpath(__file__)) triangle_path = os.path.join(script_dir, "triangle.txt") - triangle = [] - with open(triangle_path) as f: + with open(triangle_path) as in_file: for line in f: triangle = [[int(i) for i in line.split()] for line in in_file] From 44b18ea01f4b809ea900f95ee614132a454565ed Mon Sep 17 00:00:00 2001 From: Naveen Namani Date: Thu, 28 Oct 2021 22:18:28 +0530 Subject: [PATCH 8/8] Fixed extra line --- project_euler/problem_067/sol2.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/project_euler/problem_067/sol2.py b/project_euler/problem_067/sol2.py index 85546a093a9b..2e88a57170a8 100644 --- a/project_euler/problem_067/sol2.py +++ b/project_euler/problem_067/sol2.py @@ -25,8 +25,7 @@ def solution() -> int: triangle_path = os.path.join(script_dir, "triangle.txt") with open(triangle_path) as in_file: - for line in f: - triangle = [[int(i) for i in line.split()] for line in in_file] + triangle = [[int(i) for i in line.split()] for line in in_file] while len(triangle) != 1: last_row = triangle.pop()