Skip to content

Instantly share code, notes, and snippets.

@rayannott
Last active March 15, 2024 08:36
Show Gist options
  • Save rayannott/3291a185744d7c71ab14844ff3d9b484 to your computer and use it in GitHub Desktop.
Save rayannott/3291a185744d7c71ab14844ff3d9b484 to your computer and use it in GitHub Desktop.
Funny python one-liners
# 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]
@rayannott
Copy link
Author

@rayannott
Copy link
Author

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