Last active
May 9, 2017 14:03
-
-
Save byBretema/8951b7f25e3cc3b3c19ee8aea075d906 to your computer and use it in GitHub Desktop.
COLLATZ: functions VS lambdas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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