Skip to content

Instantly share code, notes, and snippets.

@Kxrr
Created February 26, 2021 02:23
Show Gist options
  • Save Kxrr/6b18401a77d5ad51a2df42cb257b6462 to your computer and use it in GitHub Desktop.
Save Kxrr/6b18401a77d5ad51a2df42cb257b6462 to your computer and use it in GitHub Desktop.
Shamis's solution
# https://stackoverflow.com/a/66368555/5512649
from typing import List
# problem description
class Object(object):
def __init__(self, integers):
self.integers = integers
def integers_total(self):
return len(self.integers)
object_1 = Object([1, 4])
object_2 = Object([1, 3])
object_3 = Object([100])
def _find_subset_objects(integers): # type: (List[int]) -> List[Object]
raise NotImplementedError()
def test(find_subset_objects=_find_subset_objects):
assert find_subset_objects([1]) == []
assert find_subset_objects([1, 4]) == [object_1]
assert find_subset_objects([1, 3, 4]) == [object_1, object_2]
# solutions
class Solution(object):
def __init__(self):
self.id2object = [object_1, object_2, object_3]
self.id2object_integers_total = [o.integers_total() for o in self.id2object]
self.integer2appear_object_ids = {
1: [0, 1],
2: [],
3: [1],
4: [0],
100: [2]
}
def solve(self, integers):
object_id2integer_appear_count = [0, 0, 0]
for integer in integers:
for object_id in self.integer2appear_object_ids[integer]:
object_id2integer_appear_count[object_id] += 1
return [
self.id2object[object_id]
for object_id, appear_count in enumerate(object_id2integer_appear_count)
if self.id2object_integers_total[object_id] == appear_count
]
if __name__ == '__main__':
test(find_subset_objects=Solution().solve)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment