Skip to content

Commit c9e1f17

Browse files
committed
added Functions Advanced - Exercise
1 parent 46cb84b commit c9e1f17

11 files changed

+248
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def negative_vs_positive(*args):
2+
global sum_negative
3+
global sum_positive
4+
5+
for num in args:
6+
if num < 0:
7+
sum_negative += num
8+
9+
elif num > 0:
10+
sum_positive += num
11+
12+
13+
sum_negative = 0
14+
sum_positive = 0
15+
16+
numbers = list(map(int, input().split()))
17+
18+
negative_vs_positive(*numbers)
19+
20+
print(sum_negative)
21+
print(sum_positive)
22+
23+
if abs(sum_negative) > sum_positive:
24+
print("The negatives are stronger than the positives")
25+
26+
elif sum_positive > abs(sum_negative):
27+
print("The positives are stronger than the negatives")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def kwargs_length(**kwargs):
2+
return len(kwargs)
3+
4+
5+
''' TESTS '''
6+
# dictionary = {'name': 'Peter', 'age': 25}
7+
8+
# print(kwargs_length(**dictionary))
9+
# ---------------------------------
10+
# dictionary = {}
11+
12+
# print(kwargs_length(**dictionary))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def even_odd(*args):
2+
command = args[-1]
3+
numbers = args[:-1]
4+
5+
if command == 'even':
6+
return [num for num in numbers if num % 2 == 0]
7+
8+
elif command == 'odd':
9+
return [num for num in numbers if num % 2 != 0]
10+
11+
12+
''' TESTS '''
13+
# print(even_odd(1, 2, 3, 4, 5, 6, "even"))
14+
# print(even_odd(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "odd"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def even_odd_filter(**kwargs):
2+
for key, numbers in kwargs.items():
3+
if key == 'even':
4+
kwargs[key] = [num for num in numbers if num % 2 == 0]
5+
6+
elif key == 'odd':
7+
kwargs[key] = [num for num in numbers if num % 2 != 0]
8+
9+
return {key: numbers for key, numbers in sorted(kwargs.items(), key=lambda x: -len(x[1]))}
10+
11+
12+
''' TESTS '''
13+
# print(even_odd_filter(
14+
# odd=[1, 2, 3, 4, 10, 5],
15+
# even=[3, 4, 5, 7, 10, 2, 5, 5, 2],
16+
# ))
17+
# print(even_odd_filter(
18+
# odd=[2, 2, 30, 44, 10, 5],
19+
# ))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def concatenate(*args, **kwargs):
2+
string = ''.join(args)
3+
4+
for key in kwargs.keys():
5+
if key in string:
6+
string = string.replace(key, kwargs[key])
7+
8+
return string
9+
10+
11+
''' TESTS '''
12+
# print(concatenate("Soft", "UNI", "Is", "Grate", "!", UNI="Uni", Grate="Great"))
13+
# print(concatenate("I", " ", "Love", " ", "Cythons", C="P", s="", java='Java'))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def func_executor(*args):
2+
function_results = []
3+
4+
for function, elements in args:
5+
function_results.append(f"{function.__name__} - {function(*elements)}")
6+
7+
return '\n'.join(function_results)
8+
9+
10+
''' TESTS '''
11+
# def sum_numbers(num1, num2):
12+
# return num1 + num2
13+
14+
15+
# def multiply_numbers(num1, num2):
16+
# return num1 * num2
17+
18+
19+
# print(func_executor(
20+
# (sum_numbers, (1, 2)),
21+
# (multiply_numbers, (2, 4))
22+
# ))
23+
24+
25+
# def make_upper(*strings):
26+
# result = tuple(s.upper() for s in strings)
27+
# return result
28+
29+
30+
# def make_lower(*strings):
31+
# result = tuple(s.lower() for s in strings)
32+
# return result
33+
34+
35+
# print(func_executor(
36+
# (make_upper, ("Python", "softUni")),
37+
# (make_lower, ("PyThOn",)),
38+
# ))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def grocery_store(**kwargs):
2+
'''
3+
4+
The groceries should be sorted by their quantity in descending order.
5+
If there are two or more products with the same quantity,
6+
the groceries should be sorted by their name's length in descending order.
7+
If there are two or more products with the same name's length,
8+
the groceries should be sorted by their name in ascending order (alphabetically).
9+
10+
'''
11+
12+
string = ''
13+
14+
for item, quantity in sorted(kwargs.items(), key=lambda x: (-x[1], -len(x[0]), x[0])):
15+
string += f'{item}: {quantity}\n'
16+
17+
return string
18+
19+
20+
''' TESTS '''
21+
# print(grocery_store(
22+
# bread=5,
23+
# pasta=12,
24+
# eggs=12,
25+
# ))
26+
27+
# print(grocery_store(
28+
# bread=2,
29+
# pasta=2,
30+
# eggs=20,
31+
# carrot=1,
32+
# ))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def age_assignment(*args, **kwargs):
2+
age_dictionary = {}
3+
4+
for letter in kwargs.keys():
5+
for person in args:
6+
if person[0] == letter:
7+
age_dictionary[person] = kwargs[letter]
8+
9+
return '\n'.join([f'{name} is {age} years old.' for name, age in sorted(age_dictionary.items())])
10+
11+
12+
''' TESTS '''
13+
# print(age_assignment("Peter", "George", G=26, P=19))
14+
# print(age_assignment("Amy", "Bill", "Willy", W=36, A=22, B=61))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def palindrome(word, index):
2+
3+
if index == len(word) // 2:
4+
return f"{word} is a palindrome"
5+
6+
if word[index] == word[-index - 1]:
7+
return palindrome(word, index + 1)
8+
9+
else:
10+
return f"{word} is not a palindrome"
11+
12+
13+
''' TESTS '''
14+
# print(palindrome("abcba", 0))
15+
# print(palindrome("peter", 0))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def fill_the_box(*args):
2+
height, length, width, *data = args
3+
size_of_the_box = height * length * width
4+
no_free_space = 0
5+
6+
for symbol in data:
7+
8+
if symbol == "Finish":
9+
break
10+
11+
if size_of_the_box - symbol <= 0:
12+
symbol -= size_of_the_box
13+
size_of_the_box = 0
14+
15+
if size_of_the_box > 0:
16+
size_of_the_box -= symbol
17+
18+
else:
19+
no_free_space += symbol
20+
21+
if size_of_the_box > 0:
22+
return f"There is free space in the box. You could put {size_of_the_box} more cubes."
23+
24+
return f"No more free space! You have {no_free_space} more cubes."
25+
26+
27+
''' TESTS '''
28+
# print(fill_the_box(2, 8, 2, 2, 1, 7, 3, 1, 5, "Finish"))
29+
# print(fill_the_box(5, 5, 2, 40, 11, 7, 3, 1, 5, "Finish"))
30+
# print(fill_the_box(10, 10, 10, 40, "Finish", 2, 15, 30))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def math_operations(*args, **kwargs):
2+
result, pos = "", 1
3+
4+
for num in args:
5+
6+
if pos == 1:
7+
kwargs["a"] += num
8+
9+
elif pos == 4:
10+
kwargs["m"] *= num
11+
12+
elif pos == 2:
13+
kwargs["s"] -= num
14+
15+
elif pos == 3:
16+
17+
if num != 0:
18+
kwargs["d"] /= num
19+
20+
pos += 1
21+
22+
if pos > 4:
23+
pos = 1
24+
25+
for key, value in sorted(kwargs.items(), key=lambda x: (-x[1], x[0])):
26+
result += f"{key}: {value:.1f}\n"
27+
28+
return result
29+
30+
31+
''' TESTS '''
32+
# print(math_operations(2.1, 12.56, 0.0, -3.899, 6.0, -20.65, a=1, s=7, d=33, m=15))
33+
# print(math_operations(-1.0, 0.5, 1.6, 0.5, 6.1, -2.8, 80.0, a=0, s=(-2.3), d=0, m=0))
34+
# print(math_operations(6.0, a=0, s=0, d=5, m=0))

0 commit comments

Comments
 (0)