Ad

lambda 1 liner, its shorter

Code
Diff
  • square=lambda a:a*a
    • def square(a):
    • return(a*a)
    • square=lambda a:a*a

The 3 farmer problem using lambda to make it one line.

Code
Diff
  • # Although I prefer using lambda for one liners,
    # here is a non-lambda version if you hate lambdas.
    #
    # def count_legs(cows, sheep, chicks): return (cows+sheep)*4+chicks*2
    #
    
    count_legs = lambda c, s, ch: (c+s)*4+ch*2
    • def countlegs(cow,sheep,chick):
    • return cow * 4 + sheep * 4 +chick * 2
    • # Although I prefer using lambda for one liners,
    • # here is a non-lambda version if you hate lambdas.
    • #
    • # def count_legs(cows, sheep, chicks): return (cows+sheep)*4+chicks*2
    • #
    • count_legs = lambda c, s, ch: (c+s)*4+ch*2