FAQ: Code Challenge: Loops - Reversed List


#22

Thanks a lot, It’s much more clear now! :grinning: :sweat_smile:

I remember it was covered but failed to remember…


#23

I am able to solve most of these but I am often going about it much differently than I see in the comments.

def reversed_list(lst1, lst2):
–i = v = 0
–if len(lst1) == len(lst2):
----for num in lst1:
------while i < len(lst1):
--------rev = lst2.pop()
--------if lst1[i] == rev:
----------i += 1
--------else:
----------i += 1
----------v += 1
–else:
----v += 1
if v != 0:
–return False
else:
–return True


#24

Should there really be two loops there? Seems to me like you’d iterate over one of the lists, and then you’re done, there’s nothing to repeat.

There are also two places you could exit early: failing the length check (avoids nesting the rest of the code inside an if-statement), and upon finding a difference (avoids having to count, either it exits early, or they are the same)

One of the buttons in the post editor is for telling the forum to not re-format things.


#26

I honestly don’t have a clue why this line works, specifically the - index part. I understand the rest of the code just fine when I read it out loud…

if lst1[index] != lst2[len(lst2) - 1 - index]:

What does - index do exactly? All I know so far is that we take any index from lst1 and if not equal to the last index of lst2 - index(???).


#27

lets say we have the following two lists:

lst1 = ['a', 'b', 'c', 'd', 'e']
lst2 = ['e', 'd', 'c', 'b', 'a']

lst1 is the same as lst2 in reverse. Now we have to express this into code

what we need to do:

  • compare first item of lst1 with last item of lst2
  • compare second item of lst1 with second to last item of lst2
  • compare last item item of lst1 with first item of lst2

(... represents everything in between, i was too lazy to write them all)

this exactly what this if condition does, for the first iteration of the loop, index will be zero, so then we get:

if lst1[0] != lst2[len(lst2) - 1 - 0]:

lets do the math first, lst2 has a length of 5 (sticking with the example i started this post with). 5 - 1 - 0 is 4, so we get:

if lst1[0] != lst2[4]:

now we resolve the square brackets, which gives us the values from the lists based on indexes:

if 'a' != 'a':

these are equal, so the lst1 could still be the same as lst2 in reverse, so we continue to the next iteration of the loop, index has now become 1:

if lst1[1] != lst2[len(lst2) - 1 - 1]:
# resolving math
if lst1[1] != lst2[3]:
# getting values/resolving square brackets
if 'b' != 'b':

these are equal, so the lst1 could still be the same as lst2 in reverse. Here we compared the second value of lst1 with the second to last value of lst2.

This continues until we completed all the elements from the list, or one of the values doesn’t equal, in which case lst1 isn’t the same as lst2 in reverse, in which case we should return False


#28

Ohh now I get it, thank you for your through explanation! Some of these concepts really need some time to sink in.


#29

I found this to be the easiest way.
Decided to check if anyone else had the same idea but I didn’t see it here so I figured I’d share.

def reversed_list(lst1, lst2):
  if lst1 == lst2[::-1]:
    return True
  else:
    return False

#30

given a comparison gives a boolean value as result, we can just return that:

def reversed_list(lst1, lst2):
  return lst1 == lst2[::-1]