From 63491c9b6952683571d7af1837cf6da05d4396ce Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Wed, 8 Jan 2020 18:54:35 +0200 Subject: [PATCH] Create find_parity_outliers.md --- snippets/find_parity_outliers.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 snippets/find_parity_outliers.md diff --git a/snippets/find_parity_outliers.md b/snippets/find_parity_outliers.md new file mode 100644 index 000000000..10b03e7ea --- /dev/null +++ b/snippets/find_parity_outliers.md @@ -0,0 +1,23 @@ +--- +title: find_parity_outliers +tags: list,math,intermediate +--- + +Given a list, returns the items that are parity outliers. + +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. + +```py +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] + ] +``` + +```py +find_parity_outliers([1, 2, 3, 4, 6]) # [1, 3] +```