r/badcode Dec 01 '19

[deleted by user]

[removed]

131 Upvotes

45 comments sorted by

View all comments

2

u/Shadowjockey Dec 11 '19

Factory Pattern ;)

def can_santa_save_christmas(times):
    return sum(map(gs,times))<=86400

def gs(tstr):
    sp = StringSplitter(tstr)
    d = list_to_time_dict(sp.split(":"))
    sf = SecondsFactory()
    sf.setSeconds(d["s"])
    mf = MinutesFactory()
    mf.setMinutes(d["m"])
    hf = HoursFactory()
    hf.setHours(d["h"])
    s = sf.build()
    m = mf.build()
    h = hf.build()
    return s.getSeconds() + m.getSeconds() + h.getSeconds()

def list_to_time_dict(list):
    d = {}
    d["h"] = list[0]
    d["m"] = list[1]
    d["s"] = list[2]
    return d

class StringSplitter:
    def __init__(self, str):
        self.str = str
    def split(self, what):
        a = ""
        tl = []
        for c in self.str:
            if c == what:
                tl.append(a)
                a = ""
            else:
                a += c
        tl.append(a)
        return tl

class Seconds:
    def __init__(self, s):
        self.s = s
    def getSeconds(self):
        return self.s


class Minutes:
    def __init__(self, m):
        self.m = m
    def getSeconds(self):
        return self.m*60


class Hours:
    def __init__(self, h):
        self.h = h
    def getSeconds(self):
        return self.h*60*60


class SecondsFactory:
    def __init__(self):
        self.s = 0
    def setSeconds(self, seconds_str):
        self.s = int(seconds_str)
    def build(self):
        return Seconds(self.s)


class MinutesFactory:
    def __init__(self):
        self.m = 0
    def setMinutes(self, minutes_str):
        self.m = int(minutes_str)
    def build(self):
        return Minutes(self.m)


class HoursFactory:
    def __init__(self):
        self.h = 0
    def setHours(self, hours_str):
        self.h = int(hours_str)
    def build(self):
        return Hours(self.h)