Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding numbers that cannot be paired

I’ve a function which aim is to find numbers that you cannot pair with others. If a number cannot be paired up with another identical number, then you will have to return it.

example:

[1,1,1,1, 2, 3, 4]

expected result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[2,3,4]
def pair(input):
    while len(input) > 2:
        for i in range(len(input) - 1):
            for j in range(len(input) - 1):
                print(input)
                if input[i] == input[j]:
                    input.pop(i)
                    input.pop(j)
                    print(input)
                else:
                    break
            
        return input


print(pair([1,1,1,1, 2, 3, 4]))

>Solution :

One way to solve this problem is to find the unique elements in the list, and if an element occurs an odd number of times the in the list, returned it.

def pair(lst):
    set_lst = set(lst)
    output = []
    for elem in set_lst:
        if lst.count(elem) % 2 != 0:
            output.append(elem)
    return output


print(pair([1, 1, 1, 1, 2, 3, 4]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading