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):
# 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
......
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
# 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)
......@@ -33,3 +33,4 @@ for solution in solve():
for row in solution:
print(row)
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