• kryptonianCodeMonkey@lemmy.world
    link
    fedilink
    arrow-up
    8
    ·
    edit-2
    13 days ago

    Works for code too

    import math
    
    def multiply_bad(a:int, b:int) -> int:
        return a*b
    
    def multiply_better(a:int, b:int) -> int:
        return (-1 if a<0 else 1)*(-1 if b<0 else 1)*int(math.sqrt(a*a*b*b))
    
    def multiply_perfect(a:int, b:int) -> int:
        product = 0
        negative = False
        if a < 0:
            a = -1*a
            negative = not negative
        if b < 0:
            b = -1*b
            negative = not negative
        for i in range(a):
            for j in range(b):
                product += 1
        if negative:
             return -1*product
        return product