I wrote some basic math functions:
def is_multiple_of(x: float, y: float):
"""Checks if x is a multiple of y and returns a boolean."""
return x / y == int(x / y)
def get_even(iterable, is_sorted: bool, return_tuple: bool):
"""Gets all the even numbers in a list/tuple and returns another list/tuple."""
even_list = []
for obj in iterable:
even_list.append(obj)
if not is_multiple_of(obj, 2):
even_list.remove(obj)
even_list = sorted(even_list) if is_sorted else even_list
return even_list if not return_tuple else tuple(even_list)
def get_odd(iterable, is_sorted: bool, return_tuple: bool):
"""Gets all the odd numbers in a list/tuple and returns another list/tuple."""
odd_list = []
for obj in iterable:
odd_list.append(obj)
if is_multiple_of(obj, 2):
odd_list.remove(obj)
odd_list = sorted(odd_list) if is_sorted else odd_list
return odd_list if not return_tuple else tuple(odd_list)
def get_uniques(iterable, is_sorted: bool, return_tuple: bool):
"""Gets all the unique variables in a list/tuple and returns another list/tuple."""
uniques = []
for obj in iterable:
if not obj in uniques:
uniques.append(obj)
uniques = sorted(uniques) if is_sorted else uniques
return uniques if not return_tuple else tuple(uniques)
Usage:
print(is_multiple_of(14, 7))
True
print(get_even([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], False))
[2, 4, 6, 8, 10]
print(get_odd([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], True))
is_sortedsays whether the input is sorted. If I want to optionally toggle the output, I would have the parameter namedshould_sort. But I agree with Toby Speight that the boolean options should be removed. \$\endgroup\$