Advanced loops challenge 5 help.
I have completed challenge 5 and getting the correct results back (first list comp is true and second list comp is false) but when I check the answer it is saying it is wrong. my code is different to the suggested code, so is this why it is not working?
#Write your function here
def reversed_list(lst1, lst2):
for index in lst1:
if lst1 == lst2[::-1]:
return True
else:
return False
#Uncomment the lines below when your function is done
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))
````Preformatted text`
I think the problem stems from your return statements. You have your return false inside an else statement. Instead, I believe you need to format it like this:
def reversed_list(lst1, lst2):
for index in lst1:
if lst1 == lst2[::-1]:
return True
return False
#Uncomment the lines below when your function is done
print(reversed_list([1, 2, 3], [3, 2, 1]))
print(reversed_list([1, 5, 3], [3, 2, 1]))
I don’t have access to the challenge instructions, but what about the following?
print(reversed_list([], []))
I would imagine the above should print True, but your proposed solution prints False.
Wouldn’t it print False because it’s an empty list though?
Now that I think about it a bit more, you bring up a good point I feel like it’s not True and it’s not False. In my brain it would be undefined maybe?
EDIT:
Okay, I made some tweaks to account for undefined lists, this should work I think.
def reversed_list(lst1, lst2):
if len(lst1) == 0 and len(lst2) == 0:
return "Undefined"
for index in lst1:
if lst1 == lst2[::-1]:
return True
return False