Skip to content
Snippets Groups Projects
Commit 52bf2bb5 authored by tnbeats's avatar tnbeats
Browse files

22211572

parent 8aadbc0d
No related branches found
No related tags found
No related merge requests found
...@@ -10,11 +10,11 @@ def rotateL(val): ...@@ -10,11 +10,11 @@ def rotateL(val):
# question 3 # question 3
""" """
The provided functions rotateR and rotateL are designed to work with both strings and lists. The functions rotateR(val) rotateL(val) work for both lists and strings
There's no issue with their functionality for either data type. 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]
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): ...@@ -24,12 +24,12 @@ def rotateRx(val):
# question 5 # question 5
def rotateRx_modified(val): '''
# cannot be modified to work for strings, without returning a value. Python strings are immutable. They cannot be rotateRx cannot be modified to work for strings without returning a value. Python strings are immutable. They cannot be
# modified once they are created: modified once they are created:
# ```TypeError: 'str' object does not support item assignment``` ```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. So the only way to achieve this would be to return a value and assign it back.
pass '''
# question 6 # question 6
......
def reverse_list_or_string(input_list_or_string): def rev(val):
if len(input_list_or_string) == 0: if len(val) == 0:
return input_list_or_string return val
else:
return reverse_list_or_string(input_list_or_string[1:]) + [input_list_or_string[0]]
# Example usage for reversing a list first = val[0]
my_list = [1, 2, 3, 4, 5] rest = val[1:]
print(reverse_list_or_string(my_list)) # Output: [5, 4, 3, 2, 1]
# Example usage for reversing a string try:
my_string = "Hello, World!" return rev(rest) + [first]
print(''.join(reverse_list_or_string(list(my_string)))) # Output: "!dlroW ,olleH" except TypeError:
return rev(rest) + first
# question 2 # question 2
def fibonacci(n): def fib(n):
# inefficient recursive fibonacci, counts down from "n" while each iteration spawns 2 recursive calls # inefficient recursive fibonacci, counts down from "n" while each iteration spawns 2 recursive calls
if n == 0: if n == 0:
return 0 return 0
if n == 1: if n == 1:
return 1 return 1
else: 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 # efficient recursive fibonacci, by going forward with each iteration spawning only 1 recursive call
if n == 0: if n == 0:
return 0 return 0
if n == 1: if n == 1:
return n_minus_1 return n_minus_1
else: else:
return fast_fibonacci(n - 1, n_minus_1, n_minus_1 + n_minus_2) return fast_fib(n - 1, n_minus_1, n_minus_1 + n_minus_2)
\ No newline at end of file
...@@ -33,3 +33,4 @@ for solution in solve(): ...@@ -33,3 +33,4 @@ for solution in solve():
for row in solution: for row in solution:
print(row) print(row)
print("--------") print("--------")
input("Press Enter for the next solution...")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment