the reversing of your list doesn’t go well. manipulating the same list as you are looping over is very tricky
if you print lst2, you will see: [1, 2, 1]:
#Write your function here
def reversed_list(lst1, lst2):
for i in range(len(lst2)):
print(i, lst2[-i-1])
lst2[i] = lst2[-i-1]
print(lst2)
if lst2 == lst1:
return True
else:
return False
#Uncomment the lines below when your function is done
print(reversed_list([1, 2, 3], [3, 2, 1]))
I completed this using very little code and it seemed to do the trick. Everything else I’ve seen here seems to over complicate this problem, even the hint of the solution
are you familiar with the big O notation? Its interesting, i recommend to do some reading about it, then think what the big O notation for the exercise solution is and what the big O notation is for your solution.
Simplicity and complexity (and many other factors) each have there pros and cons, as a programmer its your job to known the pros and cons, consider them, then choice the right implementation for the job.
def reversed_list(lst1, lst2):
for i in lst1:
if lst1[i]==lst2[-i-1]:
return True
else: return "False"
a = [1,3,1]
b = [2,3,1]
if reversed_list(a, b):
print(f"{a} is the reverse of {b}")
else:
print(f"{a} is NOT the reverse of {b}")
if we don’t want to modify original list (and as alternative to list slicing) we also can create new list from reversed iterator of original list with list(reversed(original_list)).
I have seen the other solutions to this problem, but was curious if I could shorten my own in some way. Is there a way to shorten my ‘check’ or lst? I saw some solutions put return False inside the if statement and then return True outside the loop, but I fail to see how this would be different than the for loop returning True in the first loop if the True was inside rather then outside, which I attempted before my workaround list.
def reversed_list(lst1, lst2):
lst = []
for i in range(1, len(lst1)):
if lst1[i - 1] == lst2[i * -1]:
lst.append(1)
else:
lst.append(0)
if lst.count(0) == 0:
return True
else:
return False
Hi. I have solved the question with the I[::-1] method, but after reading the comments decided not to use slicing and came up with the following solution:
Do you think it is good enough? Or should I try to implement a solution that will actually make a proper algorithm? Don’t want to be cheating