Skip to content

Commit

Permalink
Color type for question asker
Browse files Browse the repository at this point in the history
  • Loading branch information
Joey9801 committed Aug 28, 2017
1 parent 7216d31 commit 6d72254
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions solver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/python3

import collections


def ask_question(question, expected_type):
if expected_type == bool:
Expand Down Expand Up @@ -27,6 +29,102 @@ def ask_question(question, expected_type):
return result


class Color():
color_codes = collections.OrderedDict()
color_codes["U"] = "Blue"
color_codes["B"] = "Black"
color_codes["Y"] = "Yellow"
color_codes["G"] = "Green"
color_codes["R"] = "Red"
color_codes["W"] = "White"

@classmethod
def print_color_reminder(cls):
print("Color codes reminder:")
for key, value in cls.color_codes.items():
print(" {} = {}".format(key, value))

def __init__(self, color_code):
if color_code.upper() not in Color.color_codes:
print("'{}' is not a valid color code".format(color_code))
raise ValueError

self.color_code = color_code.upper()

def __eq__(self, other):
if type(other) == str:
return self.color_code == other
elif type(other) == Color:
return self.color_code == other.color_code
elif other is None:
return False
else:
raise TypeError

def __neq__(self, other):
return not (self == other)

def __hash__(self):
return hash(self.color_code)

def __str__(self):
return Color.color_codes[self.color_code]

def __repr__(self):
return "<Color {}>".format(Color.color_codes[self.color_code])


class ColorList():
def __init__(self, color_code_str=None, min_elements=None, max_elements=None):
if color_code_str is None:
self.colors = list()
return

if min_elements is not None and len(color_code_str) < min_elements:
print("Expected at least {} elements".format(min_elements))
raise ValueError

if max_elements is not None and len(color_code_str) > max_elements:
print("Expected at most {} elements".format(max_elements))
raise ValueError

self.colors = list(map(Color, color_code_str))

def __len__(self):
return len(self.colors)

def __contains__(self, x):
return Color(x) in self.colors

def __getitem__(self, index):
if not type(index) == int:
raise TypeError

return self.colors[index]

def __str__(self):
return "({})".format(", ".join(map(str, self.colors)))

def __repr__(self):
return "<ColorList ({})>".format(", ".join(map(str, self.colors)))

def count(self, color):
return self.colors.count(color)

def append(self, new_color):
if type(new_color) != Color:
new_color = Color(new_color)

self.colors.append(new_color)


def default_ranged_colorlist(min_elements, max_elements):
def f(color_code_str):
return ColorList(color_code_str, min_elements, max_elements)
f.__name__ = "ColorList<{}, {}>".format(min_elements, max_elements)
return f


class Indicators():
def __init__(self):
self._known_indicators = dict()
Expand Down

0 comments on commit 6d72254

Please sign in to comment.