Wiggs
Wiggs

Reputation: 61

Pair two elements in a list and make a conditional statement to see if the pairs are equal to each other

I have this homework assignment and seem to be stuck on this question.

Write a function that takes, as an argument, a list called aList. It returns a Boolean True if the list contains three pairs of integers, and False otherwise.

Example:

>>>threePairs([5, 6, 3, 2, 1, 4])
False

>>>threePairs([1, 1, 2, 2, 2, 2])
True

I've tried using indexes and I don't really know how to slice so I'm stuck in figuring out how I make those pairs equal to each other in the condition so it is True.

This is what I had previous to deleting it and trying again.

def threePairs(aList):
    if [0] == [1] and [2] == [3] and [4] == [5]:
        return True
    else:
        return False

Upvotes: 0

Views: 1267

Answers (4)

guest324
guest324

Reputation: 1

def threepairs(aList):
    counter = 0
    for element in aList:
        if counter >= 3:
            return True
        if element % 2 == 0:
            counter += 1
    return False

Upvotes: -1

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160437

You could zip the a sliced list with itself one position ahead, with steps equal to 2 to get adjacent elements. Then feed that to all with the condition you need:

def threePairs(l):
    return all(i == j for i,j in zip(l[::2], l[1::2])) 

zip simply takes an element from each of the iterables supplied and returns it as a tuple until one of the sequences is exhausted.

So, for example, if l = [5, 6, 3, 2, 1, 4] and zip is used with zip(l[::2], l[1::2]) you'd have:

# l[::2] = [5, 3, 1]
# l[1::2] = [6, 2, 4]
print(list(zip(l[::2], l[1::2])))
[(5, 6), (3, 2), (1, 4)] 

Upvotes: 3

Wayne Werner
Wayne Werner

Reputation: 51807

You don't need to slice, you need index access (as a side note, you should ask your teacher if they've ever heard of pep8, three_pairs(mylist) would be far better):

def threePairs(aList):
    if aList[0] == aList[1] and aList[2] == aList[3] and aList[4] == aList[5]:
        return True
    else:
        return False

But if you're ever returning True and False like this, you can just return the result of the comparison:

def threePairs(aList):
    return aList[0] == aList[1] and aList[2] == aList[3] and aList[4] == aList[5]

Though as other answers allude - your examples are not well defined. What if you have:

>>> threePairs([1, 2, 3, 1, 2, 3])

Should that return True, or False?

Upvotes: 1

alecxe
alecxe

Reputation: 473873

How about making a Counter() and check how many "pairs" you've got:

In [1]: from collections import Counter

In [2]: def has_pairs(l, n):
            return sum(value / 2 for value in Counter(l).values() 
                       if value % 2 == 0) == n 

In [3]: has_pairs([5, 6, 3, 2, 1, 4], 3)
Out[3]: False

In [4]: has_pairs([1, 1, 2, 2, 2, 2], 3)
Out[4]: True

Works for a list with any length and any input number of pairs.

Instead of using a sum() to count all the pairs, you can iterate over the counter one value at a time and have an early exit if number of pairs reaches or exceeds the input number of pairs.

Upvotes: 2

Related Questions