List comprehensions where entry occurs twice in a row

Viewed 776

I wrote code for an assignment on Kaggle.

"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."

def menu_is_boring(meals):
    return any([ meals[l] == meals[l+1] for l in range(0,len(meals)-2) ])

Expected return value of True given meals=['Spam', 'Spam'], but got False instead.

4 Answers

in your example, len(meals) is 2, so len(meals)-2 is 0, so the range is empty.

you only need to subtract 1.

try this:

def menu_is_boring(meals):
    return any([meals[l] == meals[l + 1] for l in range(0, len(meals) - 1)])


print(menu_is_boring(["Spam", "Spam"]))

Output:

True

it's worth noting that python's range is not inclusive of the stop param (i.e., list(range(1,5)) is [1, 2, 3, 4])

When you are to compare two successive values over a range, you could use zip to create the pairs and iterate over the pairs and compare:

any(i == j for i, j in zip(meals, meals[1:]))

Here I've used generator expression, but you're free to use list comprehension if you want.

Another option would be to use itertools.tee to create two iterators, consume the first element from second iterator and then iterate over pairs:

def menu_is_boring(meals): 
    it_1, it_2 = itertools.tee(meals) 
    next(it_2) 
    return any(i == j for i, j in zip(it_1, it_2))

This would be slower than the first one.

So for example:

In [1104]: def menu_is_boring(meals): 
      ...:     return any(i == j for i, j in zip(meals, meals[1:])) 
      ...:                                                                                                                                                                                                  

In [1105]: meals=['Spam', 'Spam']   

In [1106]: menu_is_boring(meals)                                                                                                                                                                            
Out[1106]: True

In [1107]: def menu_is_boring(meals): 
      ...:     it_1, it_2 = itertools.tee(meals) 
      ...:     next(it_2) 
      ...:     return any(i == j for i, j in zip(it_1, it_2)) 
      ...:                                                                                                                                                                                                  

In [1108]: menu_is_boring(meals)                                                                                                                                                                            
Out[1108]: True
def menu_is_boring(meals):
    counter=0
    
    for i in range(0, len(meals) - 1):
        if meals[i]==meals[i+1]:
            counter=counter+1
            
    if counter!=0:
        return True
    else:
        return False
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.
    """
    
    for i in range(len(meals)):
         for j in range(len(meals)-(i+1)):            
            #print(meals[i])
            #print(meals[j+(i+1)])
            if meals[i] == meals[j+(i+1)]:               
                return True                
    return False
           
meals= ["water","pear","apple","egg", "orange","apple"]

menu_is_boring(meals)
Related