'How can I return a sorted list of integers here? [closed]

def pairs (x):
    i = 0  
    z = []  
    while i < len(x)-1:
       if x[i] == x[i+1]:
            z.append(x[i])
            i+=1
       i+=1
    return z

print (pairs([4, 5, 6, 6, 6, 6, 9, 12, 12]))
print (pairs([4, 5, 4, 10, 10, 8, 9, 8,9]))

My problem is on the last line. In the last line, I wanted to return 4, 8, 9, 10 but it's only returning 10 instead. Where should I put the sort operation? I tried assigning first, it worked, but only on the assigned value and not whatever the test case would use.



Solution 1:[1]

I used a set to track pair elements and sorted the resulting list.

Program:

def pairs(x) :
    i = 0
    nums = set()
    pairs = []
    for item in x:
        if item in nums:
            pairs.append(item)
            nums.remove(item)
        else:
            nums.add(item)
    pairs.sort()
    return pairs
        

print (pairs([4, 5, 4, 10, 10, 8, 9, 8,9]))

Result:

[4, 8, 9, 10]

Solution 2:[2]

You need to count the number of occurrences.

import functools
def f(s):
    return functools.reduce(lambda x, y: x+y, [[x] * (s.count(x)//2) for x in set(s)])

f([4, 5, 6, 6, 6, 6, 9, 12, 12]) gives [6, 6, 12]

Solution 3:[3]

Because your first list which is [4, 5, 6, 6, 6, 6, 9, 12, 12] is sorted in order but the second [4, 5, 4, 10, 10, 8, 9, 8,9] is not.

Your code is only working with the sorted list, you can try this one:

def pairs (x):
    i = 0
    z = []
    while i < len(x)-1:
       if x[i] == x[i+1]:
            z.append(x[i])
            i+=1
       i+=1
    return z

print (pairs([4, 5, 6, 6, 6, 6, 9, 12, 12]))
print (pairs(sorted([4, 5, 4, 10, 10, 8, 9, 8,9])))

Result:

[6, 6, 12]
[4, 8, 9, 10]

Solution 4:[4]

You can sort before or after finding the counts of elements. Essentially, what you want is to count all of the elements, then chain together the sorted elements, with each repeated floor(count(element)/2) times.

Python's standard library makes it a lot easier to write short recipes like this. Using collections.Counter() to count, and itertools to repeat and chain iterables, you can do:

def pairs(x):
    counts = collections.Counter(x)
    return list(itertools.chain.from_iterable(itertools.repeat(k, counts[k] // 2)
                                              for k in sorted(counts)))
>>> print(pairs([4, 5, 6, 6, 6, 6, 9, 12, 12]))
[6, 6, 12]
>>> print(pairs([4, 5, 4, 10, 10, 8, 9, 8, 9]))
[4, 8, 9, 10]

Solution 5:[5]

Assuming you can't use built-in functions such as .sort(), here's a way to do so using .count():

def pairs(x):
    i = 0
    z = []
    while i < len(x):
        if x.count(x[i]) - 2 * z.count(x[i]) > 1:
            index = 0
            if z and x[i] <= z[-1]:
                while x[i] > z[index] and index < len(z):
                    index += 1
                z.insert(index, x[i])
            else:
                z.append(x[i])
        i += 1
    return z


print(pairs([4, 5, 6, 6, 6, 6, 9, 12, 12]))  # [6, 6, 12]
print(pairs([4, 5, 4, 10, 10, 8, 9, 8, 9]))  # [4, 8, 9, 10]

  • For each number in the list, we check if the number of times it appears is greater than 1, while subtracting those already counted.
  • If x[i] is present more than once, we execute a while loop to insert the number in the list so that it is in ascending order.

Solution 6:[6]

Here is another way to do so, using the Walrus operator.

def pairs(x):
    nums = set(x)
    pairs_list = []
    [pairs_list := pairs_list + [num] * (x.count(num) // 2) for num in nums]
    return pairs_list

Solution 7:[7]

your question is not so clear so i am assuming that you want a sorted list of duplicates in a list

def pairs(x):
    z = sorted(set(x))
    return [i for i in z if x.count(i) >1]

Solution 8:[8]

There may be more efficient ways to do this but this works:

alist = [4, 5, 4, 10, 10, 8, 9, 8,9]

blist = sorted({x for x in alist if alist.count(x) == 2})

print(blist)

Output:

[4, 8, 9, 10]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 an4
Solution 2 yzhang
Solution 3 Tony
Solution 4 kcsquared
Solution 5 Cubix48
Solution 6
Solution 7 Electron X
Solution 8 Albert Winestein