83 lines
1.9 KiB
Python
83 lines
1.9 KiB
Python
# a
|
|
def liczba(numbers):
|
|
return len([x for x in numbers if x % 2 == 0])
|
|
|
|
# b
|
|
def median(numbers):
|
|
sorted_numbers = sorted(numbers)
|
|
return (
|
|
0
|
|
if len(numbers) == 0
|
|
else
|
|
sorted_numbers[len(numbers) // 2]
|
|
if len(numbers) % 2 == 1
|
|
else (sorted_numbers[len(numbers) // 2 - 1] + sorted_numbers[len(numbers) // 2]) / 2
|
|
)
|
|
|
|
def pierwiastek(x, epsilon, guess = None):
|
|
guess = x / 2.0 if guess is None else guess
|
|
|
|
next_guess = (guess + x / guess) / 2.0
|
|
return (
|
|
next_guess
|
|
if abs(next_guess**2 - x) < epsilon
|
|
else pierwiastek(x, epsilon, next_guess)
|
|
)
|
|
|
|
|
|
# d
|
|
def make_alpha_dict(string: str):
|
|
return {
|
|
char: [word for word in string.split() if char in word] for char in filter(lambda x: x.isalpha(), string)
|
|
}
|
|
|
|
def flatten(lst):
|
|
return [
|
|
element for sublist in lst for element in (
|
|
flatten(sublist) if isinstance(sublist, (list, tuple)) else [sublist]
|
|
)
|
|
]
|
|
|
|
if __name__ == "__main__":
|
|
lists = [
|
|
[1,2,3,4],
|
|
[],
|
|
[4,4,5,4,4],
|
|
[3,3,1,3,2],
|
|
]
|
|
print("1.a")
|
|
for array in lists:
|
|
print(f"liczba liczb parzystych w {array}: {liczba(array)}")
|
|
print("1.b")
|
|
|
|
for array in lists:
|
|
print(f"mediana w {array}: {median(array)}")
|
|
|
|
print("1.c")
|
|
sqrt_list = [0.5, 2,3,10,25,50,100]
|
|
for x in sqrt_list:
|
|
print(f"pierwiastek z {x}: {pierwiastek(x, 0.01)}")
|
|
|
|
strings_list = [
|
|
"",
|
|
"test test test",
|
|
"ala ma kota",
|
|
"kluczami są znaki występujące alfabetyczne występujące ciągu"
|
|
]
|
|
|
|
print("1.d")
|
|
for string in strings_list:
|
|
print(f"make_alpha_dict \"{string}\": {make_alpha_dict(string)}")
|
|
|
|
|
|
flatten_lists = [
|
|
[],
|
|
[1,2,3],
|
|
[[1,2], [1,2]],
|
|
[[[[[1]]]]],
|
|
[1,2,3, [4], [[5], 6]]
|
|
]
|
|
|
|
print("1.e")
|
|
for l in flatten_lists:
|
|
print(f"flatten {l}: {flatten(l)}") |