。。。。

    技术2025-11-28  7

    玩程序真能玩出精神病,以下是在这里的两个Python实现阶乘的例子

     

    精简的实现

    f = lambda x: x and x * f(x - 1) or 1 print f(6)

    公司员工的实现

    def new(cls, *args, **kwargs): return cls(*args, **kwargs) class Number(object): pass class IntegralNumber(int, Number): def toInt(self): return new (int, self) class InternalBase(object): def __init__(self, base): self.base = base.toInt() def getBase(self): return new (IntegralNumber, self.base) class MathematicsSystem(object): def __init__(self, ibase): Abstract @classmethod def getInstance(cls, ibase): try: cls.__instance except AttributeError: cls.__instance = new (cls, ibase) return cls.__instance class StandardMathematicsSystem(MathematicsSystem): def __init__(self, ibase): if ibase.getBase() != new (IntegralNumber, 2): raise NotImplementedError self.base = ibase.getBase() def calculateFactorial(self, target): result = new (IntegralNumber, 1) i = new (IntegralNumber, 2) while i <= target: result = result * i i = i + new (IntegralNumber, 1) return result print StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6)) 

     

    。。。。。。。。。。。。。。。。。。。。。。。。。。。

     

    (工资是按程序的行数记的)

     

    最新回复(0)