Last active
March 15, 2024 08:36
-
-
Save rayannott/3291a185744d7c71ab14844ff3d9b484 to your computer and use it in GitHub Desktop.
Funny python one-liners
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
# First 20 Fibonacci numbers: | |
print(*map((fib := lambda n: n if n < 2 else fib(n-1) + fib(n-2)), range(1, 21))) | |
# FizzBuzz for 1..100: | |
print(*((i%3==0) * 'Fizz' + (i%5==0) * 'Buzz' or i for i in range(1,101)), sep='\n') | |
def add_one(a: list[int]) -> list[int]: | |
return list(map(1.__add__, a) | |
def add_one_onenumber(n: int) -> int: | |
return -~n | |
from typing import Literal | |
def even_or_odd(num: int) -> Literal['odd', 'even']: | |
return 'eovdedn'[num % 2::2] |
damn, this is even better:
from typing import Literal
def even_or_odd(n: int) -> Literal["even", "odd"]:
return 'even' if n == 0 else {'even': 'odd', 'odd': 'even'}[even_or_odd(n-1)]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also (my other gists):
itertools.starmap
is_prime
using regular expressions