From 52bf2bb5f91dedc61962bac5d94159ee40d96e10 Mon Sep 17 00:00:00 2001
From: tnbeats <tnbeatsprod@gmail.com>
Date: Tue, 17 Oct 2023 23:36:46 +0200
Subject: [PATCH] 22211572

---
 2.7 Exercises.py            | 20 ++++++++++----------
 4_9_Exercises/Question 1.py | 20 +++++++++-----------
 4_9_Exercises/Question 2.py |  9 +++++----
 4_9_Exercises/Question 5.py |  1 +
 4 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/2.7 Exercises.py b/2.7 Exercises.py
index f9ca9f9..2af7278 100644
--- a/2.7 Exercises.py	
+++ b/2.7 Exercises.py	
@@ -10,11 +10,11 @@ def rotateL(val):
 
 # question 3
 """
-The provided functions rotateR and rotateL are designed to work with both strings and lists. 
-There's no issue with their functionality for either data type.
-example rotateR using strings and lists  -- rotateR("abc") == "cab" and rotateR([1,2,3,4]) == [4,1,2,3]
-example rotateL using strings and lists  -- rotateL("abc") == "bca" and rotateL([1,2,3,4 ]) == [2,3,4,1]
+    The functions rotateR(val) rotateL(val) work for both lists and strings
+because they use slicing, which is supported by both data types in Python.
 
+Example rotateR using strings and lists  -- rotateR("abc") == "cab" and rotateR([1,2,3,4]) == [4,1,2,3]
+Example rotateL using strings and lists  -- rotateL("abc") == "bca" and rotateL([1,2,3,4]) == [2,3,4,1]
 """
 
 
@@ -24,12 +24,12 @@ def rotateRx(val):
 
 
 # question 5
-def rotateRx_modified(val):
-    # cannot be modified to work for strings, without returning a value. Python strings are immutable. They cannot be
-    # modified once they are created:
-    # ```TypeError: 'str' object does not support item assignment```
-    # So the only way to achieve this would be to return a value and assign it back.
-    pass
+'''
+    rotateRx cannot be modified to work for strings without returning a value. Python strings are immutable. They cannot be
+    modified once they are created:
+    ```TypeError: 'str' object does not support item assignment```
+    So the only way to achieve this would be to return a value and assign it back.
+'''
 
 
 # question 6
diff --git a/4_9_Exercises/Question 1.py b/4_9_Exercises/Question 1.py
index dbc1d56..e00bfd3 100644
--- a/4_9_Exercises/Question 1.py	
+++ b/4_9_Exercises/Question 1.py	
@@ -1,13 +1,11 @@
-def reverse_list_or_string(input_list_or_string):
-    if len(input_list_or_string) == 0:
-        return input_list_or_string
-    else:
-        return reverse_list_or_string(input_list_or_string[1:]) + [input_list_or_string[0]]
+def rev(val):
+    if len(val) == 0:
+        return val
 
-# Example usage for reversing a list
-my_list = [1, 2, 3, 4, 5]
-print(reverse_list_or_string(my_list))  # Output: [5, 4, 3, 2, 1]
+    first = val[0]
+    rest = val[1:]
 
-# Example usage for reversing a string
-my_string = "Hello, World!"
-print(''.join(reverse_list_or_string(list(my_string))))  # Output: "!dlroW ,olleH"
+    try:
+        return rev(rest) + [first]
+    except TypeError:
+        return rev(rest) + first
diff --git a/4_9_Exercises/Question 2.py b/4_9_Exercises/Question 2.py
index d3240ff..67b2e13 100644
--- a/4_9_Exercises/Question 2.py	
+++ b/4_9_Exercises/Question 2.py	
@@ -1,19 +1,20 @@
 # question 2
-def fibonacci(n):
+def fib(n):
     # inefficient recursive fibonacci, counts down from "n" while each iteration spawns 2 recursive calls
     if n == 0:
         return 0
     if n == 1:
         return 1
     else:
-        return fibonacci(n - 1) + fibonacci(n - 2)
+        return fib(n - 1) + fib(n - 2)
 
 
-def fast_fibonacci(n, n_minus_2=0, n_minus_1=1):
+def fast_fib(n, n_minus_2=0, n_minus_1=1):
     # efficient recursive fibonacci, by going forward with each iteration spawning only 1 recursive call
     if n == 0:
         return 0
     if n == 1:
         return n_minus_1
     else:
-        return fast_fibonacci(n - 1, n_minus_1, n_minus_1 + n_minus_2)
\ No newline at end of file
+        return fast_fib(n - 1, n_minus_1, n_minus_1 + n_minus_2)
+
diff --git a/4_9_Exercises/Question 5.py b/4_9_Exercises/Question 5.py
index 20deecc..e2ed5d5 100644
--- a/4_9_Exercises/Question 5.py	
+++ b/4_9_Exercises/Question 5.py	
@@ -33,3 +33,4 @@ for solution in solve():
     for row in solution:
         print(row)
     print("--------")
+    input("Press Enter for the next solution...")
-- 
GitLab