From 124d79d3ea72523f12a39da97dd6ae0fc81d1450 Mon Sep 17 00:00:00 2001
From: Michael Mutote <130656746+mr1Michael@users.noreply.github.com>
Date: Thu, 12 Oct 2023 11:12:51 +0200
Subject: [PATCH] 22202956 Exercise 4.9 question 3 done.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Works okay for smaller grids but is a lot slower for larger grids. Might be worthwhile to use Dijkstra’s Algorithm
---
 4_9_Exercises/Question 3.py | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 4_9_Exercises/Question 3.py

diff --git a/4_9_Exercises/Question 3.py b/4_9_Exercises/Question 3.py
new file mode 100644
index 0000000..a2bd84b
--- /dev/null
+++ b/4_9_Exercises/Question 3.py	
@@ -0,0 +1,20 @@
+def nsp(x, y, current=(0, 0)):
+    if current is None:
+        return 0
+    if current == (x, y):
+        return 1
+    else:
+        next_corner = new_index(x, y, current)
+        # uses helper function to find the next corner
+        return nsp(x, y, next_corner[0]) + nsp(x, y, next_corner[1])
+    pass
+
+
+def new_index(x, y, current):
+    # Helper function to find the possible next states, reduced clutter
+    if current[0] < x and current[1] < y:
+        return (current[0], current[1] + 1), (current[0] + 1, current[1])
+    if current[0] >= x and current[1] < y:
+        return (current[0], current[1] + 1), None
+    if current[0] < x and current[1] >= y:
+        return (current[0] + 1, current[1]), None
-- 
GitLab