Created
March 31, 2018 20:43
-
-
Save LeReverandNox/08cd3a65a6098efadb6b4bceee25d443 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def count_up_to_50(n): | |
| return [] if n > 50 else [n] + count_up_to_50(n + 1) | |
| def sum_to(n): | |
| return n if n == 1 else n + sum_to(n - 1) | |
| def fibonacci(nb, prev_n = 0, next_n = 1): | |
| return [next_n] if nb == 0 else [next_n] + fibonacci(nb - 1, next_n, prev_n + next_n) | |
| def print_list(l): | |
| if l == []: return | |
| print(l[0]) | |
| print_list(l[1:]) | |
| def count_digits(n, i = 1): | |
| return i if n % (10 ** i) == n else count_digits(n, i + 1) | |
| def sum_of_digits(n): | |
| return 0 if n == 0 else (n % 10) + sum_of_digits(n//10) | |
| def pgcd(n1, n2): | |
| return n2 if n1%n2 == 0 else pgcd(n2, n1%n2) | |
| def max_list(l, max_n = 0): | |
| if l == []: return max_n | |
| if l[0] > max_n: max_n = l[0] | |
| return max_list(l[1:], max_n) | |
| def reverse_string(string): | |
| return string if string == "" else string[len(string) - 1] + reverse_string(string[:-1]) | |
| def fact(n): | |
| if n <= 0: return 0 | |
| return n if n == 1 else n * fact(n-1) | |
| def dec_to_bin(n): | |
| def func(n): | |
| if n == 1: return str(n) | |
| return str(n % 2) + dec_to_bin(n//2) | |
| return int(func(n)) | |
| # print(count_up_to_50(1)) | |
| # print(sum_to(5)) | |
| # print(fibonacci(10)) | |
| # print_list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) | |
| # print(count_digits(12345)) | |
| # print(sum_of_digits(999)) | |
| # print(pgcd(96, 36)) | |
| # print(max_list([9, 3, 13, 5])) | |
| # print(reverse_string('Hello, World!')) | |
| # print(fact(5)) | |
| print(dec_to_bin(156)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment