Skip to content

Commit 75f9d8b

Browse files
committed
Python tasks
1 parent 10a53b0 commit 75f9d8b

4 files changed

Lines changed: 129 additions & 58 deletions

File tree

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,51 @@
11
from typing import Dict, List
22

33

4-
def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
5-
"""
6-
Calculate the sum and product of integers in a list.
7-
8-
Note: the sum is every number added together
9-
and the product is every number multiplied together
10-
so for example: [2, 3, 5] would return
11-
{
12-
"sum": 10, // 2 + 3 + 5
13-
"product": 30 // 2 * 3 * 5
14-
}
15-
Time Complexity:
16-
Space Complexity:
17-
Optimal time complexity:
18-
"""
19-
# Edge case: empty list
4+
# def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
5+
# """
6+
# Calculate the sum and product of integers in a list.
7+
8+
# Note: the sum is every number added together
9+
# and the product is every number multiplied together
10+
# so for example: [2, 3, 5] would return
11+
# {
12+
# "sum": 10, // 2 + 3 + 5
13+
# "product": 30 // 2 * 3 * 5
14+
# }
15+
# Time Complexity:
16+
# Space Complexity:
17+
# Optimal time complexity:
18+
# """
19+
# # Edge case: empty list
20+
# if not input_numbers:
21+
# return {"sum": 0, "product": 1}
22+
23+
# sum = 0
24+
# for current_number in input_numbers:
25+
# sum += current_number
26+
27+
# product = 1
28+
# for current_number in input_numbers:
29+
# product *= current_number
30+
31+
# return {"sum": sum, "product": product}
32+
33+
def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str,int]:
2034
if not input_numbers:
2135
return {"sum": 0, "product": 1}
22-
36+
2337
sum = 0
24-
for current_number in input_numbers:
25-
sum += current_number
26-
2738
product = 1
28-
for current_number in input_numbers:
29-
product *= current_number
39+
40+
for num in input_numbers:
41+
sum += num
42+
product *= num
3043

3144
return {"sum": sum, "product": product}
45+
46+
# """
47+
# Time Complexity: O(n)
48+
# Space Complexity: O(1)
49+
# Optimal time complexity: O(n)
50+
# """
51+

Sprint-1/Python/find_common_items/find_common_items.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,40 @@
33
ItemType = TypeVar("ItemType")
44

55

6+
# def find_common_items(
7+
# first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType]
8+
# ) -> List[ItemType]:
9+
# """
10+
# Find common items between two arrays.
11+
12+
# Time Complexity:
13+
# Space Complexity:
14+
# Optimal time complexity:
15+
# """
16+
# common_items: List[ItemType] = []
17+
# for i in first_sequence:
18+
# for j in second_sequence:
19+
# if i == j and i not in common_items:
20+
# common_items.append(i)
21+
# return common_items
22+
23+
624
def find_common_items(
7-
first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType]
8-
) -> List[ItemType]:
25+
first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType]) -> List[ItemType]:
926
"""
1027
Find common items between two arrays.
1128
12-
Time Complexity:
13-
Space Complexity:
14-
Optimal time complexity:
29+
Time Complexity: O(n+m)
30+
Space Complexity: O(m+k)
31+
Optimal time complexity: O(n+m)
1532
"""
16-
common_items: List[ItemType] = []
17-
for i in first_sequence:
18-
for j in second_sequence:
19-
if i == j and i not in common_items:
20-
common_items.append(i)
21-
return common_items
33+
set_1 = set(first_sequence)
34+
set_2 = []
35+
36+
for seq in set_1:
37+
if seq in second_sequence:
38+
set_2.append(seq)
39+
return set_2
40+
41+
42+

Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,31 @@
33
Number = TypeVar("Number", int, float)
44

55

6+
# def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
7+
# """
8+
# Find if there is a pair of numbers that sum to a target value.
9+
10+
# Time Complexity:
11+
# Space Complexity:
12+
# Optimal time complexity:
13+
# """
14+
# for i in range(len(numbers)):
15+
# for j in range(i + 1, len(numbers)):
16+
# if numbers[i] + numbers[j] == target_sum:
17+
# return True
18+
# return False
19+
620
def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
7-
"""
8-
Find if there is a pair of numbers that sum to a target value.
21+
set_1 = set(numbers)
22+
result = []
923

10-
Time Complexity:
11-
Space Complexity:
12-
Optimal time complexity:
13-
"""
14-
for i in range(len(numbers)):
15-
for j in range(i + 1, len(numbers)):
16-
if numbers[i] + numbers[j] == target_sum:
17-
return True
24+
for num in numbers:
25+
differ = target_sum - num
26+
if differ in set_1:
27+
return True
28+
result.append(differ)
1829
return False
30+
31+
# Time Complexity: O(n)
32+
# # Space Complexity: O(n)
33+
# # Optimal time complexity: O(n)
Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
from typing import List, Sequence, TypeVar
2+
from collections import OrderedDict
23

34
ItemType = TypeVar("ItemType")
45

56

7+
# def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
8+
# """
9+
# Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
10+
11+
# Time complexity:
12+
# Space complexity:
13+
# Optimal time complexity:
14+
# """
15+
# unique_items = []
16+
17+
# for value in values:
18+
# is_duplicate = False
19+
# for existing in unique_items:
20+
# if value == existing:
21+
# is_duplicate = True
22+
# break
23+
# if not is_duplicate:
24+
# unique_items.append(value)
25+
26+
# return unique_items
27+
628
def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
729
"""
830
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
931
10-
Time complexity:
11-
Space complexity:
12-
Optimal time complexity:
32+
Time complexity: O(n)
33+
Space complexity: O(n)
34+
Optimal time complexity: O(n)
1335
"""
14-
unique_items = []
15-
16-
for value in values:
17-
is_duplicate = False
18-
for existing in unique_items:
19-
if value == existing:
20-
is_duplicate = True
21-
break
22-
if not is_duplicate:
23-
unique_items.append(value)
24-
25-
return unique_items
36+
return list(OrderedDict.fromkeys(values))
37+
38+
# Used OrderedDict to create a dictionary with the list elements as keys (preserves order)
39+
# Then, convert it back to a list to remove duplicates
40+
# https://www.w3resource.com/python-exercises/list-advanced/python-list-advanced-exercise-8.php#:~:text=The%20Python%20OrderedDict.,order%20of%20the%20remaining%20elements.

0 commit comments

Comments
 (0)