Skip to content

Commit c3123e9

Browse files
committed
Redid Functions Advanced - Exercise
1 parent 3595d6d commit c3123e9

File tree

7 files changed

+123
-56
lines changed

7 files changed

+123
-56
lines changed
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
def negative_vs_positive(*args):
2-
global sum_negative
3-
global sum_positive
2+
sum_negatives = sum([num for num in args if num < 0])
3+
sum_positives = sum([num for num in args if num >= 0])
44

5-
for num in args:
6-
if num < 0:
7-
sum_negative += num
5+
print(sum_negatives)
6+
print(sum_positives)
87

9-
elif num > 0:
10-
sum_positive += num
8+
if abs(sum_negatives) > sum_positives:
9+
print("The negatives are stronger than the positives")
1110

11+
elif sum_positives > abs(sum_negatives):
12+
print("The positives are stronger than the negatives")
1213

13-
sum_negative = 0
14-
sum_positive = 0
15-
16-
numbers = list(map(int, input().split()))
1714

15+
numbers = [int(num) for num in input().split()]
1816
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")

Advanced/3.Functions Advanced/Functions Advanced - Exercise/03. Even or Odd.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
def even_odd(*args):
2-
command = args[-1]
3-
numbers = args[:-1]
2+
args, even_or_odd = args[:-1], args[-1]
43

5-
if command == 'even':
6-
return [num for num in numbers if num % 2 == 0]
4+
if even_or_odd == "even":
5+
return [num for num in args if num % 2 == 0]
76

8-
elif command == 'odd':
9-
return [num for num in numbers if num % 2 != 0]
7+
elif even_or_odd == "odd":
8+
return [num for num in args if num % 2 != 0]
109

1110

1211
''' TESTS '''
Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
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]
52

6-
elif key == 'odd':
7-
kwargs[key] = [num for num in numbers if num % 2 != 0]
3+
for key, values in kwargs.items():
84

9-
return {key: numbers for key, numbers in sorted(kwargs.items(), key=lambda x: -len(x[1]))}
5+
if key == "even":
6+
kwargs[key] = [num for num in values if num % 2 == 0]
7+
8+
elif key == "odd":
9+
kwargs[key] = [num for num in values if num % 2 != 0]
10+
11+
sorted_tuple = sorted(kwargs.items(), key=lambda x: -len(x[1]))
12+
sorted_dict = {item[0]: item[1] for item in sorted_tuple}
13+
14+
return sorted_dict
1015

1116

1217
''' TESTS '''
@@ -16,4 +21,26 @@ def even_odd_filter(**kwargs):
1621
# ))
1722
# print(even_odd_filter(
1823
# odd=[2, 2, 30, 44, 10, 5],
19-
# ))
24+
# ))
25+
26+
27+
28+
# def even_odd_filter(**kwargs):
29+
# for key, numbers in kwargs.items():
30+
# if key == 'even':
31+
# kwargs[key] = [num for num in numbers if num % 2 == 0]
32+
#
33+
# elif key == 'odd':
34+
# kwargs[key] = [num for num in numbers if num % 2 != 0]
35+
#
36+
# return {key: numbers for key, numbers in sorted(kwargs.items(), key=lambda x: -len(x[1]))}
37+
#
38+
#
39+
# ''' TESTS '''
40+
# # print(even_odd_filter(
41+
# # odd=[1, 2, 3, 4, 10, 5],
42+
# # even=[3, 4, 5, 7, 10, 2, 5, 5, 2],
43+
# # ))
44+
# # print(even_odd_filter(
45+
# # odd=[2, 2, 30, 44, 10, 5],
46+
# # ))

Advanced/3.Functions Advanced/Functions Advanced - Exercise/05. Concatenate.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
def concatenate(*args, **kwargs):
2-
string = ''.join(args)
2+
message = ''.join(args)
33

4-
for key in kwargs.keys():
5-
if key in string:
6-
string = string.replace(key, kwargs[key])
4+
for old_str, new_str in kwargs.items():
5+
message = message.replace(old_str, new_str)
76

8-
return string
7+
return message
98

109

1110
''' TESTS '''

Advanced/3.Functions Advanced/Functions Advanced - Exercise/06. Function Executor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ def func_executor(*args):
77
return '\n'.join(function_results)
88

99

10+
""" ON ONE LINE!!! """
11+
# def func_executor(*args):return '\n'.join([f'{f.__name__} - {f(*fa)}' for f,fa in args])
12+
13+
14+
1015
''' TESTS '''
1116
# def sum_numbers(num1, num2):
1217
# return num1 + num2
Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
def grocery_store(**kwargs):
2-
'''
2+
products = sorted(kwargs.items(), key=lambda x: (-x[1], -len(x[0]), x[0]))
3+
return '\n'.join([f"{item[0]}: {item[1]}" for item in products])
34

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).
95

10-
'''
6+
""" ON ONE LINE!!! """
7+
# def grocery_store(**kwargs): return '\n'.join([f"{item[0]}: {item[1]}" for item in sorted(kwargs.items(), key=lambda x: (-x[1], -len(x[0]), x[0]))])
118

12-
string = ''
139

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
1810

1911

2012
''' TESTS '''
@@ -30,3 +22,40 @@ def grocery_store(**kwargs):
3022
# eggs=20,
3123
# carrot=1,
3224
# ))
25+
26+
27+
28+
29+
30+
# def grocery_store(**kwargs):
31+
# '''
32+
#
33+
# The groceries should be sorted by their quantity in descending order.
34+
# If there are two or more products with the same quantity,
35+
# the groceries should be sorted by their name's length in descending order.
36+
# If there are two or more products with the same name's length,
37+
# the groceries should be sorted by their name in ascending order (alphabetically).
38+
#
39+
# '''
40+
#
41+
# string = ''
42+
#
43+
# for item, quantity in sorted(kwargs.items(), key=lambda x: (-x[1], -len(x[0]), x[0])):
44+
# string += f'{item}: {quantity}\n'
45+
#
46+
# return string
47+
#
48+
#
49+
# ''' TESTS '''
50+
# # print(grocery_store(
51+
# # bread=5,
52+
# # pasta=12,
53+
# # eggs=12,
54+
# # ))
55+
#
56+
# # print(grocery_store(
57+
# # bread=2,
58+
# # pasta=2,
59+
# # eggs=20,
60+
# # carrot=1,
61+
# # ))
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
def age_assignment(*args, **kwargs):
2-
age_dictionary = {}
32

4-
for letter in kwargs.keys():
5-
for person in args:
6-
if person[0] == letter:
7-
age_dictionary[person] = kwargs[letter]
3+
people_ages = []
84

9-
return '\n'.join([f'{name} is {age} years old.' for name, age in sorted(age_dictionary.items())])
5+
for letter, ages in kwargs.items():
6+
7+
for name in args:
8+
if name[0] == letter:
9+
people_ages.append(f"{name} is {ages} years old.")
10+
11+
return '\n'.join(sorted(people_ages))
1012

1113

1214
''' TESTS '''
1315
# print(age_assignment("Peter", "George", G=26, P=19))
1416
# print(age_assignment("Amy", "Bill", "Willy", W=36, A=22, B=61))
17+
18+
19+
20+
# def age_assignment(*args, **kwargs):
21+
# age_dictionary = {}
22+
#
23+
# for letter in kwargs.keys():
24+
# for person in args:
25+
# if person[0] == letter:
26+
# age_dictionary[person] = kwargs[letter]
27+
#
28+
# return '\n'.join([f'{name} is {age} years old.' for name, age in sorted(age_dictionary.items())])
29+
#
30+
#
31+
# ''' TESTS '''
32+
# # print(age_assignment("Peter", "George", G=26, P=19))
33+
# # print(age_assignment("Amy", "Bill", "Willy", W=36, A=22, B=61))

0 commit comments

Comments
 (0)