Last active
February 21, 2017 08:27
-
-
Save hanksudo/82d163f66d7dd03281303168d5cd24c2 to your computer and use it in GitHub Desktop.
Find newest note of every number occur and also remove duplicate number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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