Function sm() returns a tuple of the maximum and second maximum of a list of integers without any comparison or loop.
#!/usr/bin/python3 from functools import * # Returns 1 if a>b, else 0. gt = lambda a,b: ( (b-a)>>((b-a).bit_length()) ) & 0x1 gtn = lambda a,b: (b == None) or gt(a,b) # a<=b. Returns 1 if a<y<b, else 0. btn = lambda a,b,y: gtn(y,a) and gtn(b,y) # (neq) Returns 0 if a=b, 1 otherwise --not needed. neq = lambda a,b: gt(a,b)+gt(b,a) eq = lambda a,b: not neq(a,b) # t=(a,b). Returns (x2,x1) where # x1 is max of a,b,y and x2 is second max of a,b,y. m2 = lambda t,y: (t[1],y) if gtn(y,t[1]) \ else ( (y,t[1]) if btn(t[0],t[1],y) \ else (t[0],t[1])) # Executes m2() cumulatively on l. sm = lambda l: reduce(m2,l, (None,None))[0]