diff --git a/2.7 Exercises.py b/2.7 Exercises.py index f9ca9f98a5734349df32932283dfad4b05f505a9..2af7278c59ec48308692581b482828bbf23fb214 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 dbc1d56c2e1c3692c88fbb6bd04cea247c5ea906..e00bfd3ce6536be5b389add10e7c16daf0d0fc2a 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 d3240ff1efc4cec021e8af8b8dee27cddb2ceb42..67b2e13f920adbe80a67330d61d39cfc64899dbb 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 20deeccd8a7b773c05ce5270cb9c0e1121175ef5..e2ed5d59a421a4044be10f1aad1b230beda451bd 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...")