Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find newest note of every number occur and also remove duplicate number.
import itertools
def summarize_notes(case):
found = set()
for note, numbers in case:
snumbers = set(numbers)
output = list(snumbers - found)
found |= snumbers
if output:
yield [note, output]
def summarize_notes2(case):
found = set(range(5))
for note, numbers in case:
output = list(itertools.takewhile(lambda x: x in found, numbers))
found -= set(output)
if output:
yield [note, output]
def summarize_notes3(case):
found = set(range(5))
for note, numbers in case:
snumbers = set(numbers)
output = list(snumbers & found)
found -= set(output)
if output:
yield [note, output]
cases = [
[
[["Note2", [1, 2, 3]], ["Note1", []]],
[["Note2", [1, 2, 3]]]
],
[
[["Note2", [1]], ["Note1", [0, 1]]],
[["Note2", [1]], ["Note1", [0]]]
],
[
[["Note2", [0, 1]], ["Note1", [0]]],
[["Note2", [0, 1]]]
],
[
[["Note2", [1, 2, 3]], ["Note1", [0, 1, 2]]],
[["Note2", [1, 2, 3]], ["Note1", [0]]]
],
[
[["Note5", [1, 2]], ["Note4", [4]], ["Note3", [0, 2]], ["Note2", [2]], ["Note1", [0]]],
[["Note5", [1, 2]], ["Note4", [4]], ["Note3", [0]]]
]
]
for case, answer in cases:
assert list(summarize_notes(case)) == answer
assert list(summarize_notes2(case)) == answer
assert list(summarize_notes3(case)) == answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.