Image Credit: Giphy
Let's break it down into classes
from abc import ABC, abstractmethod
class Tool(ABC):
def __init__(self):
pass
@abstractmethod
def describe(self):
pass
from Tool import *
from abc import ABC, abstractmethod
class Wrench(Tool, ABC):
def __init__(self, length):
self.__length = length
@property
def length(self):
return self.__length
@abstractmethod
def tighten(self, clearance, size):
pass
from Tool import *
from abc import ABC, abstractmethod
class Saw(Tool, ABC):
def __init__(self, length):
self.__length = length
@property
def length(self):
return self.__length
@abstractmethod
def cut(self, length, material):
pass
from Wrench import *
class AdjustableWrench(Wrench):
def __init__(self, length, min_size, max_size):
super().__init__(length)
self.__min_size = min_size
self.__max_size = max_size
@property
def min_size(self):
return self.__min_size
@property
def max_size(self):
return self.__max_size
# other methods go here
from Wrench import *
class AdjustableWrench(Wrench):
def __init__(self, length, min_size, max_size):
super().__init__(length)
self.__min_size = min_size
self.__max_size = max_size
@property
def min_size(self):
return self.__min_size
@property
def max_size(self):
return self.__max_size
def describe(self):
return "AdjustableWrench: Length: {} MinSize: {}
MaxSize: {}".format(self.__length,
self.__min_size, self.__max_size)
# other methods go here
from Wrench import *
class AdjustableWrench(Wrench):
def __init__(self, length, min_size, max_size):
super().__init__(length)
self.__min_size = min_size
self.__max_size = max_size
@property
def min_size(self):
return self.__min_size
@property
def max_size(self):
return self.__max_size
def describe(self):
return "AdjustableWrench: Length: {} MinSize: {}
MaxSize: {}".format(self.__length,
self.__min_size, self.__max_size)
# other methods go here
from Wrench import *
class AdjustableWrench(Wrench):
def __init__(self, length, min_size, max_size):
super().__init__(length)
self.__min_size = min_size
self.__max_size = max_size
@property
def min_size(self):
return self.__min_size
@property
def max_size(self):
return self.__max_size
def describe(self):
return "AdjustableWrench: Length: {} MinSize: {}
MaxSize: {}".format(self.__length,
self.__min_size, self.__max_size)
# other methods go here
from Wrench import *
class AdjustableWrench(Wrench):
def __init__(self, length, min_size, max_size):
super().__init__(length)
self.__min_size = min_size
self.__max_size = max_size
@property
def min_size(self):
return self.__min_size
@property
def max_size(self):
return self.__max_size
def describe(self):
return "AdjustableWrench: Length: {} MinSize: {}
MaxSize: {}".format(self.length,
self.__min_size, self.__max_size)
# other methods go here
from Wrench import *
class AdjustableWrench(Wrench):
def __init__(self, length, min_size, max_size):
super().__init__(length)
self.__min_size = min_size
self.__max_size = max_size
@property
def min_size(self):
return self.__min_size
@property
def max_size(self):
return self.__max_size
def describe(self):
return "AdjustableWrench: Length: {} MinSize: {}
MaxSize: {}".format(self.length,
self.min_size, self.max_size)
def tighten(self, clearance, size):
return clearance >= self.length and
size >= self.min_size and
size <= self.max_size
from Wrench import *
class CombinationWrench(Wrench):
def __init__(self, length, size):
super().__init__(length)
self.__size = size
@property
def size(self):
return self.__size
def describe(self):
return "CombinationWrench Length: {} Size: {}"
.format(self.length, self.size)
def tighten(self, clearance, size):
return clearance >= self.length and
size == self.size
from Wrench import *
class OpenEndWrench(Wrench):
def __init__(self, length, size_one, size_two):
super().__init__(length)
self.__size_one = size_one
self.__size_two = size_two
@property
def size_one(self):
return self.__size_one
@property
def size_two(self):
return self.__size_two
def describe(self):
return "CombinationWrench Length: {} SizeOne: {}
SizeTwo: {}".format(self.length,
self.size_one, self.size_two)
def tighten(self, clearance, size):
return clearance >= self.length and
(size == self.size_one or
size == self.size_two)
from Saw import *
class CrossCutSaw(Saw):
def __init__(self, length, materials):
super().__init__(length)
self.__materials = materials.split(":")
@property
def materials(self):
return ", ".join(self.__materials)
def describe(self):
"CrossCutSaw Length: {} Materials: {}".format(
self.length, self.materials)
# additional methods here
from Saw import *
class CrossCutSaw(Saw):
def __init__(self, length, materials):
super().__init__(length)
self.__materials = materials.split(":")
@property
def materials(self):
return ", ".join(self.__materials)
def describe(self):
"CrossCutSaw Length: {} Materials: {}".format(
self.length, self.materials)
# additional methods here
from Saw import *
class CrossCutSaw(Saw):
def __init__(self, length, materials):
super().__init__(length)
self.__materials = materials.split(":")
@property
def materials(self):
return ", ".join(self.__materials)
def describe(self):
"CrossCutSaw Length: {} Materials: {}".format(
self.length, self.materials)
def __find_material(self, material):
for m in self.__materials:
if m == material:
return True
return False
# additional methods here
from Saw import *
class CrossCutSaw(Saw):
def __init__(self, length, materials):
super().__init__(length)
self.__materials = materials.split(":")
@property
def materials(self):
return ", ".join(self.__materials)
def describe(self):
"CrossCutSaw Length: {} Materials: {}".format(
self.length, self.materials)
def __find_material(self, material):
for m in self.__materials:
if m == material:
return True
return False
def cut(self, length, material):
return length < self.length
and self.__find_material(material)
from Saw import *
class HackSaw(Saw):
def __init__(self, length):
super().__init__(length)
def describe(self):
return "HackSaw Length: {} Material: metal".
format(self.length)
def cut(self, length, material):
return length < self.length and
material == "metal"
from Wrench import *
from AdjustableWrench import *
from CombinationWrench import *
from OpenEndWrench import *
from Saw import *
from HackSaw import *
from CrossCutSaw import *
import sys
class Main:
# methods go here
# main guard
if __name__ == "__main__":
Main.main(sys.argv)
from Wrench import *
from AdjustableWrench import *
from CombinationWrench import *
from OpenEndWrench import *
from Saw import *
from HackSaw import *
from CrossCutSaw import *
import sys
class Main:
# methods go here
# main guard
if __name__ == "__main__":
Main.main(sys.argv)
@staticmethod
def read_input(filename):
try:
with open(filename) as reader:
num_tools = int(reader.readline())
tools = []
return tools
except Exception:
print("Invalid Input")
return []
@staticmethod
def read_input(filename):
try:
with open(filename) as reader:
num_tools = int(reader.readline())
tools = []
for i in range(0, num_tools):
return tools
except Exception:
print("Invalid Input")
return []
@staticmethod
def read_input(filename):
try:
with open(filename) as reader:
num_tools = int(reader.readline())
tools = []
for i in range(0, num_tools):
line = reader.readline().split(" ")
return tools
except Exception:
print("Invalid Input")
return []
@staticmethod
def read_input(filename):
try:
with open(filename) as reader:
num_tools = int(reader.readline())
tools = []
for i in range(0, num_tools):
line = reader.readline().split(" ")
if line[0] == "AdjustableWrench":
elif line[0] == "OpenEndWrench":
elif line[0] == "CombinationWrench":
elif line[0] == "CrossCutSaw":
elif line[0] == "HackSaw":
else:
raise Exception("Unknown Tool: " + line[0])
return tools
except Exception:
print("Invalid Input")
return []
@staticmethod
def read_input(filename):
try:
with open(filename) as reader:
num_tools = int(reader.readline())
tools = []
for i in range(0, num_tools):
line = reader.readline().split(" ")
length = int(line[1])
if line[0] == "AdjustableWrench":
min_size = int(line[2])
max_size = int(line[3])
tools.append(AdjustableWrench(length, min_size, max_size))
elif line[0] == "OpenEndWrench":
size_one = int(line[2])
size_two = int(line[3])
tools.append(OpenEndWrench(length, size_one, size_two))
elif line[0] == "CombinationWrench":
size = int(line[2])
tools.append(CombinationWrench(length, size))
elif line[0] == "CrossCutSaw":
tools.append(CrossCutSaw(length, line[2]))
elif line[0] == "HackSaw":
tools.append(HackSaw(length))
else:
raise Exception("Unknown Tool: " + line[0])
return tools
except Exception:
print("Invalid Input")
return []
@staticmethod
def find_tool(tools, query):
query_parts = query.split(" ")
@staticmethod
def find_tool(tools, query):
query_parts = query.split(" ")
if query_parts[0] == "tighten":
elif query_parts[0] == "cut":
else:
@staticmethod
def find_tool(tools, query):
query_parts = query.split(" ")
if query_parts[0] == "tighten":
clearance = int(query_parts[1])
size = int(query_parts[2])
elif query_parts[0] == "cut":
length = int(query_parts[1])
else:
@staticmethod
def find_tool(tools, query):
query_parts = query.split(" ")
if query_parts[0] == "tighten":
clearance = int(query_parts[1])
size = int(query_parts[2])
for t in tools:
if isinstance(t, Wrench):
elif query_parts[0] == "cut":
length = int(query_parts[1])
for t in tools:
if isinstance(t, Saw):
else:
@staticmethod
def find_tool(tools, query):
query_parts = query.split(" ")
if query_parts[0] == "tighten":
clearance = int(query_parts[1])
size = int(query_parts[2])
for t in tools:
if isinstance(t, Wrench):
if t.tighten(clearance, size):
return t
elif query_parts[0] == "cut":
length = int(query_parts[1])
for t in tools:
if isinstance(t, Saw):
if t.cut(length, query_parts[2]):
return t
else:
@staticmethod
def find_tool(tools, query):
query_parts = query.split(" ")
if query_parts[0] == "tighten":
clearance = int(query_parts[1])
size = int(query_parts[2])
for t in tools:
if isinstance(t, Wrench):
if t.tighten(clearance, size):
return t
return ??
elif query_parts[0] == "cut":
length = int(query_parts[1])
for t in tools:
if isinstance(t, Saw):
if t.cut(length, query_parts[2]):
return t
return ??
else:
return ??
@staticmethod
def find_tool(tools, query):
query_parts = query.split(" ")
if query_parts[0] == "tighten":
clearance = int(query_parts[1])
size = int(query_parts[2])
for t in tools:
if isinstance(t, Wrench):
if t.tighten(clearance, size):
return t
return None
elif query_parts[0] == "cut":
length = int(query_parts[1])
for t in tools:
if isinstance(t, Saw):
if t.cut(length, query_parts[2]):
return t
return None
else:
return None
@staticmethod
def find_tool(tools, query):
query_parts = query.split(" ")
if query_parts[0] == "tighten":
clearance = int(query_parts[1])
size = int(query_parts[2])
for t in tools:
if isinstance(t, Wrench):
if t.tighten(clearance, size):
return t
return None
elif query_parts[0] == "cut":
length = int(query_parts[1])
for t in tools:
if isinstance(t, Saw):
if t.cut(length, query_parts[2]):
return t
return None
else:
return None
@staticmethod
def main(args):
if len(args) != 2:
print("Invalid Input")
return
@staticmethod
def main(args):
if len(args) != 2:
print("Invalid Input")
return
tools = Main.read_input(args[1])
@staticmethod
def main(args):
if len(args) != 2:
print("Invalid Input")
return
tools = Main.read_input(args[1])
if len(tools) == 0:
return
@staticmethod
def main(args):
if len(args) != 2:
print("Invalid Input")
return
tools = Main.read_input(args[1])
if len(tools) == 0:
return
try:
with sys.stdin as reader:
except Exception:
print("Invalid Tool")
return
@staticmethod
def main(args):
if len(args) != 2:
print("Invalid Input")
return
tools = Main.read_input(args[1])
if len(tools) == 0:
return
try:
with sys.stdin as reader:
query = reader.readline()
t = Main.find_tool(tools, query)
except Exception:
print("Invalid Tool")
return
@staticmethod
def main(args):
if len(args) != 2:
print("Invalid Input")
return
tools = Main.read_input(args[1])
if len(tools) == 0:
return
try:
with sys.stdin as reader:
query = reader.readline()
t = Main.find_tool(tools, query)
if t is not None:
print(t.describe())
else:
print("Invalid Tool")
except Exception:
print("Invalid Tool")
return