Skip to content

Instantly share code, notes, and snippets.

@byBretema
Last active May 9, 2017 14:03
Show Gist options
  • Save byBretema/8951b7f25e3cc3b3c19ee8aea075d906 to your computer and use it in GitHub Desktop.
Save byBretema/8951b7f25e3cc3b3c19ee8aea075d906 to your computer and use it in GitHub Desktop.
COLLATZ: functions VS lambdas.
# Lambda way...
L_Collatz = lambda n : [] if n == 1 else \
[n / 2] + L_Collatz(n / 2) if n % 2 == 0 else \
[3 * n + 1] + L_Collatz(3 * n + 1)
# Function way...
def F_Collatz(n):
if n == 1:
return []
elif n % 2 == 0:
return [n / 2] + F_Collatz(n / 2)
else:
return [3 * n + 1] + F_Collatz(3 * n + 1)
# Test...
print(L_Collatz(6))
print(F_Collatz(6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment