CODE CHALLENGE: LOOPS Reversed List. " - 1 - "

In this challenge the hint gives theses instructions:

" Let’s say the list are of size 5. You want to compare lst1[0] with lst2[4] , lst1[1] with lst2[3] and so on.

Loop through the numbers created by range(len(lst1)) using a variable named index

Compare lst1[index] to lst2[len(lst2) - 1 - index] . If those two items are not equal, return False . If you loop through the entire list and you never return False , that means that every item was equal, and you should return True ."

Can someone explain what the " - 1 - " from “lst2[len(lst2) - 1 - index]” part of the hint means? I don’t remember this showing up before.

indexes are zero based (counting start at zero), while len() starts counting at 1. so you need to subtract one to compensate for this difference.

3 Likes

I understand subtracting 1 from the index, but I have never seen the " - 1 -" operand used before. Maybe I’m not explaining myself correctly but is this style of subtracting from the index commonly used? Also, if there is a link someone could provide how to use this style of subtracting from the index to explain it, would be much appreciated. I’ve tried looking.

- 1 - in this instance isn’t a single operator, it’s two individual subtraction operations.

len(lst2)
- 1 
- index

:slight_smile:

3 Likes

thepitycoder is right, in math we can perfectly subtract twice: 6 - 3 - 2 = 1

in programming we can do this as well.

4 Likes

Thank you that’s exactly what I was asking. Thank you!

1 Like

Can anyone explain why the simple variation below makes the code for this exercise function incorrectly? I initially wrote code that didn’t work but the small adjustment below fixes it and I absolutely cannot figure out why.

This works:

def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2) - 1 - index]:
return False
return True

but this does not work:

def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] == lst2[len(lst2) - 1 - index]:
return True
return False

I assume it is because the exercise wants the answer to return false, and then true. It is probably just a Codecademy requirement, but either would work outside of the Codecademy lessons. If this is not the case, could you please post the full code formatted, thanks!
Happy Coding!

The second code sample (the one that doesn’t work), lets do a test case:

reversed_list([1, 3, 5, 7, 9], [2, 4, 6, 8, 1])

now the function returns True, while clearly list1 isn’t list2 in reverse. However, the first item of list1 and last item of list2 are a match. In the first iteration of the loop, the if condition is true, thus return True is executed, causing the function to hand back data, meaning the function is done executing. But we only compared the first value of list1 with the last value of list 2

when having problems like this, try adding some different function calls with different lists, see if all your tests pass. When you then have a case that doesn’t work, walk through the flow of your code.

if you have problems with that, there are tools like this one: http://www.pythontutor.com/visualize.html

That is extremely helpful
stetim94
. I was under the impression that both methods of writing the code would loop both lists in entirety before returning “True” or “False.” Just to make sure I understand what you’re saying - the “==” loop is stopping after analyzing the first value of lst1 against the last value of lst2 because that is true. Correct? In which case, writing the code with “!=” is correct because it is forced to loop the entire list, looking for values that don’t match whereas “==” doesn’t need to loop the entire list. Did I understand that correctly?

the moment a return keyword is reached, the function ends (even if that means breaking the loop).

this is fine when you do return False inside the loop, given the list are not the reverse of one another

however, trying to return True inside the loop (by checking the values are equal) means only one of the elements in both list have to be a matched, for example:

reversed_list([1, 3, 5, 7, 2], [2, 4, 6, 8, 10])

would give True, given the last item of list one equals the first item of list two. So because only one element equals, the function gives True. But clearly the lists aren’t reversed of each other

not any different
    all equal

Those are equivalent.

Swapping “different” and “equal” + moving “not” is not enough, because that still leaves “any” and “all”

    any equal
not all different

Still equivalent. But they mean something else.

And because you are implementing any and all yourself using loops, your whole structure has to change to accomodate, if you used functions then things become easier to swap out:

# operator.eq is == as a function, operator.ne is !=
from operator import eq, ne

# not any different
def reversed_list(lst1, lst2):
    return not any(map(ne, lst1, reversed(lst2)))

# all equal
def reversed_list(lst1, lst2):
    return all(map(eq, lst1, reversed(lst2)))

# any equal
def reversed_any_match(lst1, lst2):
    return any(map(eq, lst1, reversed(lst2)))

# not all different
def reversed_any_match(lst1, lst2):
    return not all(map(ne, lst1, reversed(lst2)))