lambda 1 liner, its shorter
Test.assert_equals(square(4), 16) Test.assert_equals(square(10), 100) Test.assert_equals(square(2), 4) Test.assert_equals(square(8), 64)
print(square(2))- Test.assert_equals(square(4), 16)
- Test.assert_equals(square(10), 100)
- Test.assert_equals(square(2), 4)
- Test.assert_equals(square(8), 64)
The 3 farmer problem using lambda to make it one line.
# 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
test.assert_equals(count_legs(1, 2, 3), 18) test.assert_equals(count_legs(3, 2, 1), 22) test.assert_equals(count_legs(2, 2, 2), 20) test.assert_equals(count_legs(4, 12, 32), 128)
run = countlegs(2,3,4)print(run)print(countlegs(4,5,6))- test.assert_equals(count_legs(1, 2, 3), 18)
- test.assert_equals(count_legs(3, 2, 1), 22)
- test.assert_equals(count_legs(2, 2, 2), 20)
- test.assert_equals(count_legs(4, 12, 32), 128)