Files
Vial-Game-Solver/exceptions.py
2022-02-11 13:17:15 +01:00

61 lines
2.4 KiB
Python

"""Exceptions used for the vial solver."""
from collections import Counter
class InvalidVial(Exception):
"""Raised when an invalid vial is initialized."""
def __init__(self, balls, size):
self.message = "The vial you tried to initialize is not possible."
self.message += f"\n(balls: {balls}, size: {size})"
super().__init__(self.message)
class InvalidGame(Exception):
"""Raised when an invalid game is initialized."""
def __init__(self, vial_string):
self.message = "The game you tried to initialize is not possible."
self.message += f"\n(input string: {vial_string})"
count = Counter(list(vial_string.replace(" ", "")))
count_counter = Counter(list(count.values()))
if 1 in count_counter.values():
wrong_letter = list(count.keys())[
list(count.values()).index(list(count_counter.keys())[
list(count_counter.values()).index(1)
])
]
pointers = ' '*len("(input string: ")
indices = [index for index, value in enumerate(list(vial_string))
if value == wrong_letter]
for i, index in enumerate(indices):
if i > 0:
index -= indices[i-1]+1
pointers += ' '*index + "^"
self.message += "\n"+pointers
super().__init__(self.message)
class NoSolutions(Exception):
"""Raised when an invalid game is initialized."""
def __init__(self):
self.message = "There are no valid solutions for the game."
super().__init__(self.message)
class VialFull(Exception):
"""Raised when a full vial is pushed to."""
def __init__(self, balls, size):
self.message = "The vial you tried to push to is already full."
self.message += f"\n(balls: {balls}, size: {size})"
super().__init__(self.message)
class VialEmpty(Exception):
"""Raised when an empty vial is popped."""
def __init__(self, balls, size):
self.message = "The vial you tried to pull from is empty."
self.message += f"\n(balls: {balls}, size: {size})"
super().__init__(self.message)
class HeapEmpty(Exception):
"""Raised when extract_min() is tried on an empty heap."""
def __init__(self):
self.message = "The heap you tried to extract from is empty."
super().__init__(self.message)