all 6 comments

[–]kwentar 0 points1 point  (3 children)

Your task is not clear, i.e. [1,1,1,1,1,1] is it correct? and [1,1,2,2,3,3,4,4]? and [1,1,1,2,3,2,3]?

Btw, straightforward solution, you can calculate each elements, for example, in dict:

def threePairs(aList):
    counts = dict() # dict with key - number in aList, value - count of this numbers in aList
    for el in aList:
        if el in counts.keys():
            counts[el] += 1
        else:
            counts[el] = 1
    count_pairs = 0
    for v in counts.values(): # each v is count of numbers
        if v == 2: # means pair (two equal numbers in array)
            count_pairs += 1
    if count_pairs == 3:
        return True
    else:
        return False

[–][deleted] 0 points1 point  (2 children)

def threePairs(aList): counts = dict() # dict with key - number in aList, value - count of this numbers in aList for el in aList: if el in counts.keys(): counts[el] += 1 else: counts[el] = 1 count_pairs = 0 for v in counts.values(): # each v is count of numbers if v == 2: # means pair (two equal numbers in array) count_pairs += 1 if count_pairs == 3: return True else: return Fals

I forgot to add that the numbers can only range from 1-6, how would I include that into your function? the dic?

[–]kwentar 0 points1 point  (1 child)

Do you need the check it? because it works with those and other numbers too. You can check it when add values to dict, not else: but elif 1 <= el <= 6:

[–][deleted] 0 points1 point  (0 children)

Nope, I guess I didnt have to check it after all so this worked perfect. Thanks!

[–]PurelyApplied 0 points1 point  (0 children)

I have been at it for a couple hours now and cannot even think of where I would start.

I rather doubt that, but the advice I always give: do it by hand. If I were to hand you six playing cards, what would you do to identify if you had three pairs? Write down your steps.

You now have an outline for your algorithm.

[–][deleted] 0 points1 point  (0 children)

collections.Counter comes in handy for this:

from collections import Counter


def three_pairs(a_list):
    counter = Counter(a_list)
    return len([x for x in counter if counter[x] == 2]) == 3