class LimitedSet(object): def __init__(self, allowed): self.contents = {} self.allowed = allowed def add(self, element): if element in self.allowed: self.contents[element] = element return True return False def remove(self, element): if element in self.contents: del self.contents[element] return True return False def contains(self, element): return element in self.contents def is_allowed(self, element): return element in self.allowed class LetterSet(LimitedSet): def __init__(self): LOWERCASE = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] UPPERCASE = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] LimitedSet.__init__(self, LOWERCASE + UPPERCASE) class NumberSet(LimitedSet): def __init__(self): DIGITS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] LimitedSet.__init__(self, DIGITS)