Cierp, córko! Teraz jesteś w innej rodzinie i musisz się dostosować do ich zasad. Wyszłaś za mąż, nie przyszłaś w gości. Jakie zasady, mamo? Oni tu wszyscy mają nie po kolei w głowie! Zwłaszcza teściowa! Nienawidzi mnie, to oczywiste! A słyszałaś kiedyś, żeby teściowe były dobre?
Hulanka! Znowu hulanka! Barbara Stanisławowa stała na środku kuchni, jej twarz była czerwona ze złości, a oczy płonęły wściekłością. Jak mąż hula, to baba sama winna. Mam ci wszystko tłumaczyć?
Teściowa straciła panowanie nad sobą. Krzyczała na swoją synową, Kasię, jak opętana. A wszystko przez to, że dziewczyna podejrzewała swojego męża, jej syna Wojtka, o niewierność.
Kasia, młoda, drobna dziewczyna o wielkich, naiwnych oczach, stała przyciśnięta do ściany, próbując przekonać rozwścieczoną kobietę.
Barbaro Stanisławo, ale to nie jest w porządku. On ma rodzinę, dzieci zaczęła Kasia, ale teściowa natychmiast ją przerwała, machając ręką, jakby odpędzała natrętnego komara.
Ty to rodzina? Albo twoje dziecko, które nawet do nas nie chce podejść? Teściowa prychnęła z pogardą. Twoje wychowanie, swoją drogą!
Jakie wychowanie? Jasiek ma dopiero rok! cicho zaprotestowała Kasia.
Rok? Kobieta wykrzywiła usta. U Kowalskich wnuk jest młodszy, a już chodzi na ręce i nie wrzeszczy jak twój
Przecież to wasz wnuk odparła Kasia, choć głos jej drżał. Dzieci czują złych ludzi. Może dlatego do was nie podchodzi.
My źli? A kto tu żyje na nasz koszt? Czyje jedzenie jesz? Czyje pieniądze wydajesz? Niewdzięcznica!
Kasia nie chciała już się k# HW6
## **Task 1** (4 points)
Write a function `my_add` that takes any number of arguments (numeric or strings) and returns their sum.
If an argument cannot be coerced to a number, it must be skipped.
If no arguments are passed, the function should return `None`.
**Example 1:**
Input: `my_add(1, 2, '3′, 'four’, 'five’)`
Output: `6`
**Example 2:**
Input: `my_add(1, 'a’)`
Output: `1`
**Example 3:**
Input: `my_add()`
Output: `None`
### Solution:
„`python
def my_add(*args):
if not args:
return None
total = 0
for arg in args:
try:
num = float(arg)
total += num
except (ValueError, TypeError):
continue
return total
„`
### Explanation:
– The function `my_add` takes any number of positional arguments.
– If no arguments are provided, it returns `None`.
– For each argument, it attempts to convert it to a float (which handles both numeric types and numeric strings).
– If conversion fails (raises `ValueError` or `TypeError`), the argument is skipped.
– The valid numbers are summed and returned.
—
## **Task 2** (4 points)
Write a function `my_filter` that takes an iterable and any number of additional arguments. Each additional argument can be a function (which takes one argument and returns `True`/`False`) or any other value.
The function must return a list of elements from the original iterable that satisfy the following conditions:
– If the additional argument is a function, the element must satisfy (return `True`) for all such functions.
– If the additional argument is not a function, the element must be equal to all such arguments.
**Example 1:**
Input: `my_filter([1, 2, 3, 4, 5], lambda x: x > 2, lambda x: x % 2)`
Output: `[3, 5]`
**Example 2:**
Input: `my_filter([’foo’, 1, 10, ”, 'bar’, None, 0], ”)`
Output: `[”]`
**Example 3:**
Input: `my_filter([1, 2, 3], lambda x: isinstance(x, int), 2)`
Output: `[2]`
### Solution:
„`python
def my_filter(iterable, *args):
result = []
for item in iterable:
include = True
for arg in args:
if callable(arg):
if not arg(item):
include = False
break
else:
if item != arg:
include = False
break
if include:
result.append(item)
return result
„`
### Explanation:
– The function `my_filter` processes each item in the provided iterable.
– For each item, it checks against all additional arguments:
– If the argument is a function (`callable`), the item must satisfy the function (return `True`).
– If the argument is not a function, the item must be equal to the argument.
– Only items that meet all conditions are included in the result list.
—
## **Task 3** (4 points)
Write a function `weird_func` that takes any number of positional and keyword arguments.
The function should return a string generated as follows:
1. All positional arguments that are strings and have even length are concatenated in order.
2. All keyword arguments that are strings and have odd length are concatenated in order of sorted keys.
**Example 1:**
Input: `weird_func(’ab’, 'cde’, 'fghi’, first=’1′, second=’22’, third=’333′)`
Output: `’abfghi1333’`
**Example 2:**
Input: `weird_func()`
Output: `”`
### Solution:
„`python
def weird_func(*args, **kwargs):
positional_part = ”.join([arg for arg in args if isinstance(arg, str) and len(arg) % 2 == 0])
sorted_keys = sorted(kwargs.keys())
keyword_part = ”.join([kwargs[key] for key in sorted_keys if isinstance(kwargs[key], str) and len(kwargs[key]) % 2 != 0])
return positional_part + keyword_part
„`
### Explanation:
– **Positional Arguments Handling**: The function collects all positional arguments that are strings with even lengths and concatenates them in the order they were provided.
– **Keyword Arguments Handling**: The function collects all keyword arguments that are strings with odd lengths, sorts the keys alphabetically, and concatenates the corresponding values in the order of the sorted keys.
– The results from both parts are combined into a single string and returned.
—
## **Task 4** (4 points)
Write a function `partial_apply` that takes a function `func` and any number of positional and/or keyword arguments.
The function must return a new function that takes any number of positional and/or keyword arguments. When called, the new function combines the arguments passed to it with the arguments passed to `partial_apply` (positional arguments go first, then keyword arguments; in case of conflicts, the new function’s arguments take precedence) and calls `func` with the combined arguments.
**Example 1:**
Input:
„`python
def greet(name, surname, greeting=’Hello’):
return f'{greeting}, {name} {surname}!’
f = partial_apply(greet, 'Jane’, 'Doe’)
print(f(greeting=’Hi’))
„`
Output: `’Hi, Jane Doe!’`
**Example 2:**
Input:
„`python
def foo(a, b, c=1, d=2):
return a + b + c + d
f = partial_apply(foo, c=10, d=20)
print(f(1, 2, d=30))
„`
Output: `43`
*(1 + 2 + 10 + 30)*
### Solution:
„`python
def partial_apply(func, *args, **kwargs):
def wrapped(*new_args, **new_kwargs):
combined_args = args + new_args
combined_kwargs = kwargs.copy()
combined_kwargs.update(new_kwargs)
return func(*combined_args, **combined_kwargs)
return wrapped
„`
### Explanation:
– **Function Creation**: `partial_apply` creates a new function (`wrapped`) that combines the arguments it receives with those passed to `partial_apply`.
– **Argument Combination**: Positional arguments from `partial_apply` come first, followed by any new positional arguments passed to `wrapped`. Keyword arguments are combined



