X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=grab_bag.py;h=807f74f6384e5cde38e3fc16db438004ca69d979;hb=7eae23537dcc61565a24d5c957d4325b7337b63a;hp=a427256ca0f1b1530a0e757391dc027b1391099a;hpb=5e241dc47e497c547463cecc07946ea6882835a7;p=kiosk.git diff --git a/grab_bag.py b/grab_bag.py index a427256..807f74f 100644 --- a/grab_bag.py +++ b/grab_bag.py @@ -1,31 +1,34 @@ +#!/usr/bin/env python3 + +from collections import Counter import random +from typing import Iterable, List, Optional, Set class grab_bag(object): - def __init__(self): - self.contents = set() + def __init__(self) -> None: + self.contents: Set[str] = set() - def clear(self): + def clear(self) -> None: self.contents.clear() - def add(self, item): + def add(self, item: str) -> None: if item not in self.contents: self.contents.add(item) - def add_all(self, collection): + def add_all(self, collection: Iterable[str]) -> None: for x in collection: self.add(x) - def subset(self, count): + def subset(self, count: int) -> Optional[List[str]]: if len(self.contents) < count: return None - subset = random.sample(self.contents, count) - return subset + return random.sample(self.contents, count) - def size(self): + def size(self) -> int: return len(self.contents) # x = grab_bag() -# x.add_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) -# print x.subset(3) +# x.add_all(["oneA", "two", "three", "oneB", "four", "five", "oneC", "oneD"]) +# print(x.subset(3))