Skip to content
This repository was archived by the owner on May 7, 2023. It is now read-only.

Files

Latest commit

 

History

History
27 lines (22 loc) · 686 Bytes

find-parity-outliers.md

File metadata and controls

27 lines (22 loc) · 686 Bytes
title type tags cover dateModified
Find parity outliers
snippet
list
math
beach-pineapple
2020-11-02 19:27:53 +0200

Finds the items that are parity outliers in a given list.

  • Use collections.Counter with a list comprehension to count even and odd values in the list.
  • Use collections.Counter.most_common() to get the most common parity.
  • Use a list comprehension to find all elements that do not match the most common parity.
from collections import Counter

def find_parity_outliers(nums):
  return [
    x for x in nums
    if x % 2 != Counter([n % 2 for n in nums]).most_common()[0][0]
  ]
find_parity_outliers([1, 2, 3, 4, 6]) # [1, 3]