Patron filtering

Demonstration

Task

Write function f(x, n) that accepts list of unique words x and positive integer n that represents maximum occurences number of one element in list as parameters and returns new list that doesn't include patrons.

Explanation

Word is a list of elements. Element is simple value like string or number. Something comparable. List x is a list of lists of simple values. Simplest example is a list of strings where elements are substrings divided with spaces. Number of elements in one word is called complexity. Elements that occur in list (not in one word!) more than n times are violators. Word that contains one or more violators called patron. Number of violators in one word called patronage. The goal is to remove patrons till the element becomes non-violator. Those patrons which have bigger patronage are first to remove. Next complexity is in priority. And at the end index of the word in list gives bigger priority (this is optional).

Example

f(['a', 'a b', 'a c e', 'a b c', 'b a d', 'b', 'b d', 'e'], 3) gives ['a', 'a b', 'a c e', 'b', 'b d', 'e']

Demo

Unfiltered

Filtered