Designing function f(f(n)) == -n

前端 未结 30 2823
清酒与你
清酒与你 2020-12-02 03:32

A question I got on my last interview:

Design a function f, such that:

f(f(n)) == -n

Where n<

30条回答
  •  自闭症患者
    2020-12-02 04:00

    Nobody ever said f(x) had to be the same type.

    def f(x):
        if type(x) == list:
            return -x[0]
        return [x]
    
    
    f(2) => [2]
    f(f(2)) => -2
    

提交回复
热议问题