Here is the question from Kaggle.
I tried it using a while loop but I guess I am missing something basic here.
The commented code using while loop is my initiation and the uncommented code is the solution. Detailed solution given on website is pasted below :
def menu_is_boring(meals):
"""Given a list of meals served over some period of time, return True if the
same meal has ever been served two days in a row, and False otherwise.
"""
# i = 1
# while (i) < (len(meals)-1) :
# if meals[i] == meals[i 1] :
# return True
# else :
# i = i 1
# return False
for i in range(len(meals)-1):
if meals[i] == meals[i 1]:
return True
return False
# Check your answer
q3.check()
The key to our solution is the call to range. range(len(meals)) would give us all the indices of meals. If we had used that range, the last iteration of the loop would be comparing the last element to the element after it, which is... IndexError! range(len(meals)-1) gives us all the indices except the index of the last element.
But don't we need to check if meals is empty? Turns out that range(0) == range(-1) - they're both empty. So if meals has length 0 or 1, we just won't do any iterations of our for loop.
CodePudding user response:
You can simply use the built-in function any()
def menu_is_boring(meals):
return any(meals[i] == meals[i 1] for i in range(len(meals)-1))
Difference between While and For Loop is simple:
- While loops run until you
break
them or pass in a specific condition - For loops run until the range has ended or when you
break
them.
A for loop can't run forever, but a while loop can.
CodePudding user response:
The difference between for and while loop:
for-loop are for looping or iterating over containers and/or streams of data, such as lists, and the duration of task to be done over that data is usually determinate by the amount of data in said container or stream.
while loop are for looping or repeating a task an usually undetermined number of times and the duration of task to be done is determined by fulfilling some condition that is usually unrelated to the amount of initial data.
While-loop are the more general of the two, and you can in fact do everything a for-loop does with a while-loop, but doing so is more error prone because you need to keep track of the extra things that a for-loop take care off for you like indexing.
And about your sample code, the only problem with your while-loop version is that you start your index a 1 instead of 0 (list indexes start at 0 in python).
And tho there nothing particularly wrong about you for-loop version, there are way of doing it such that you don't need to keep track of any index like
for a, b in zip(meals, meals[1:]): # using the zip function along with using the slice notation in order to pair the first element with second...
if a == b:
return True
return False
from itertools import pairwise # python 3.10
for a, b in pairwise(meals): # using the pairwise function to pair the first element with second...
if a == b:
return True
return False
(or their one liner version...)
CodePudding user response:
maybe you can try :
i = 1
while False :
if meals[i] == meals[i 1] :
return True
else :
i = i 1
return False
but you can make infinite loop.