-
-
Save AndersonFirmino/7303e58d78c82abba35c8f78d0c20147 to your computer and use it in GitHub Desktop.
random par ou impar (functional example)
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
""" | |
Exemplo para ajudar a responder o problema de aninhamento em https://gist.github.com/AndersonFirmino/b0300923094a5a8450018c5bd32c9de8 | |
""" | |
from itertools import filterfalse | |
from functools import partial | |
from random import choice, random | |
def pipe(*funcs): | |
def inner(data, funcs=funcs): | |
result = data | |
for f in funcs: | |
result = f(result) | |
return result | |
return inner | |
is_par = lambda x: x % 2 == 0 | |
filter_par = partial(filter, is_par) | |
filter_impar = partial(filterfalse, is_par) | |
random_sorted = partial(sorted, key=lambda x: random()) | |
flaged = pipe(range, tuple, filter_par, random_sorted, choice) | |
non_flaged = pipe(range, tuple, filter_impar, random_sorted, choice) | |
def odd_or_even(flag, limit): | |
"""Retorna um numero aleatorio pelo limite | |
:param flag: | |
:param limit: | |
:return: | |
""" | |
return flaged(limit) if flag else non_flaged(limit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment