Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"*test*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
Binary file added __pycache__/analytics.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/test_analytics.cpython-313.pyc
Binary file not shown.
55 changes: 48 additions & 7 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import statistics
# ==========================================
# SECTION A: LIST PROCESSING
# ==========================================
Expand All @@ -17,9 +18,12 @@ def filter_sales_above_threshold(sales: list, threshold: int):
- Use a loop or list comprehension
"""
# TODO: Write your code here
pass


new_list = []
for i in sales:
if i >= threshold:
new_list.append(i)
return new_list
# print(filter_sales_above_threshold([100, 250, 75, 300], 150))
def count_product_codes(codes: list, prefix: str):
"""
QUESTION 2
Expand All @@ -33,7 +37,13 @@ def count_product_codes(codes: list, prefix: str):
- Return 0 if no matches found
"""
# TODO: Write your code here
pass
count = 0
for code in codes:
code = str(code)
if code.startswith(prefix):
count += 1
return count
# print(count_product_codes(["PROD-001", "PROD-002", "SERV-001"], "PROD"))


def calculate_moving_average(numbers: list, window_size: int):
Expand All @@ -51,7 +61,13 @@ def calculate_moving_average(numbers: list, window_size: int):
- Return 0.0 for empty list
"""
# TODO: Write your code here
pass
if numbers == []:
return float(0)

new = numbers[-window_size:]

return float(statistics.mean(new))
# print(calculate_moving_average([], 3))


# ==========================================
Expand All @@ -72,8 +88,25 @@ def get_top_seller(sales_data: dict):
- If there's a tie, return the name that appears first alphabetically
"""
# TODO: Write your code here
pass
if sales_data == {}:
return "No Data"
sales = []
length = len(sales_data.keys())
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable length is calculated but never used. This should be removed.

Suggested change
length = len(sales_data.keys())

Copilot uses AI. Check for mistakes.
sales =(sorted(sales_data.values()))
print(sales)
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug print statement should be removed before submitting code. This will produce unwanted output when the function is called.

Suggested change
print(sales)

Copilot uses AI. Check for mistakes.
names = sorted(sales_data.keys())
if len(names) == 1:
return names[0]
if sales[0] == sales[1]:
return names[0]

for name, sale in sales_data.items():
if sale == sales[-1]:
return name
return name

Comment on lines +91 to +107
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for finding the top seller is incorrect. This implementation sorts sales values and names separately, then tries to match them, which doesn't preserve the relationship between names and their sales. For example, if the input is {"Alice": 5000, "Bob": 7500, "Carol": 6000}, sorting names alphabetically gives ["Alice", "Bob", "Carol"] and sorting values gives [5000, 6000, 7500], but this incorrectly pairs Alice with 5000, Bob with 6000, and Carol with 7500.

The correct approach is to find the maximum value in the dictionary and return the corresponding key, or use max() with a key function: return max(sales_data, key=sales_data.get) (accounting for alphabetical ordering in case of ties).

Suggested change
if sales_data == {}:
return "No Data"
sales = []
length = len(sales_data.keys())
sales =(sorted(sales_data.values()))
print(sales)
names = sorted(sales_data.keys())
if len(names) == 1:
return names[0]
if sales[0] == sales[1]:
return names[0]
for name, sale in sales_data.items():
if sale == sales[-1]:
return name
return name
if not sales_data:
return "No Data"
max_sales = max(sales_data.values())
top_sellers = [name for name, sale in sales_data.items() if sale == max_sales]
return sorted(top_sellers)[0]

Copilot uses AI. Check for mistakes.
Comment on lines +93 to +107
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name is undefined if the function reaches this return statement. This will occur when no name in sales_data matches sales[-1], causing a NameError. This logic issue is related to the fundamental flaw in the algorithm (sorting names and values separately).

Suggested change
sales = []
length = len(sales_data.keys())
sales =(sorted(sales_data.values()))
print(sales)
names = sorted(sales_data.keys())
if len(names) == 1:
return names[0]
if sales[0] == sales[1]:
return names[0]
for name, sale in sales_data.items():
if sale == sales[-1]:
return name
return name
# Find the maximum sales value
max_sale = max(sales_data.values())
# Collect all names with the maximum sales value
top_names = [name for name, sale in sales_data.items() if sale == max_sale]
# Return the alphabetically first name among them
return sorted(top_names)[0]

Copilot uses AI. Check for mistakes.
Comment on lines +91 to +107
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tie-breaking logic is flawed. This checks if sales[0] == sales[1], but sales is sorted in ascending order, so sales[0] is the minimum value, not involved in determining the top seller. The tie should be checked among the maximum values (e.g., sales[-1] == sales[-2]). However, this entire approach is fundamentally broken due to separating names from their corresponding sales values.

Suggested change
if sales_data == {}:
return "No Data"
sales = []
length = len(sales_data.keys())
sales =(sorted(sales_data.values()))
print(sales)
names = sorted(sales_data.keys())
if len(names) == 1:
return names[0]
if sales[0] == sales[1]:
return names[0]
for name, sale in sales_data.items():
if sale == sales[-1]:
return name
return name
if not sales_data:
return "No Data"
max_sale = max(sales_data.values())
top_sellers = [name for name, sale in sales_data.items() if sale == max_sale]
return sorted(top_sellers)[0]

Copilot uses AI. Check for mistakes.
Comment on lines +91 to +107
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential IndexError when accessing sales[1]. If the dictionary has only one entry, sales will have length 1, and sales[1] will raise an IndexError. While there is a check for len(names) == 1 at line 98, it returns before reaching this code. However, this entire section is flawed due to the incorrect algorithm.

Suggested change
if sales_data == {}:
return "No Data"
sales = []
length = len(sales_data.keys())
sales =(sorted(sales_data.values()))
print(sales)
names = sorted(sales_data.keys())
if len(names) == 1:
return names[0]
if sales[0] == sales[1]:
return names[0]
for name, sale in sales_data.items():
if sale == sales[-1]:
return name
return name
if not sales_data:
return "No Data"
max_sale = max(sales_data.values())
top_sellers = [name for name, sale in sales_data.items() if sale == max_sale]
return sorted(top_sellers)[0]

Copilot uses AI. Check for mistakes.

print(get_top_seller({"Alice": 5000, "Bob": 7500, "Carol": 6000}))
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test/debug code should be removed before submitting. This print statement is left over from testing.

Suggested change
print(get_top_seller({"Alice": 5000, "Bob": 7500, "Carol": 6000}))

Copilot uses AI. Check for mistakes.

def merge_inventory(warehouse_a: dict, warehouse_b: dict):
"""
Expand All @@ -93,7 +126,15 @@ def merge_inventory(warehouse_a: dict, warehouse_b: dict):
- Do NOT modify the original dictionaries
"""
# TODO: Write your code here
pass
new = {}
for key, value in warehouse_a.items():
for key1, value1 in warehouse_b.items():
if key == key1:
nv = value+ value1
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing around the + operator. Should be nv = value + value1 for consistency with Python style guidelines (PEP 8).

Suggested change
nv = value+ value1
nv = value + value1

Copilot uses AI. Check for mistakes.
return new[k]
Comment on lines +129 to +134
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The merge_inventory function is incomplete and will cause multiple errors:

  1. The variable nv is calculated inside the nested loop but never used
  2. The variable k is undefined, causing a NameError
  3. The function doesn't handle products that exist in only one warehouse
  4. The nested loop approach is inefficient - it creates O(n*m) complexity when a simpler approach would be O(n+m)

The correct approach would be to:

  • Create a new dictionary starting with a copy of warehouse_a
  • Iterate through warehouse_b and add/update quantities
new = warehouse_a.copy()
for key, value in warehouse_b.items():
    if key in new:
        new[key] += value
    else:
        new[key] = value
return new
Suggested change
new = {}
for key, value in warehouse_a.items():
for key1, value1 in warehouse_b.items():
if key == key1:
nv = value+ value1
return new[k]
new = warehouse_a.copy()
for key, value in warehouse_b.items():
if key in new:
new[key] += value
else:
new[key] = value
return new

Copilot uses AI. Check for mistakes.
warehouse_a = {"PROD-001": 50, "PROD-002": 30}
warehouse_b = {"PROD-002": 20, "PROD-003": 40}
print(merge_inventory(warehouse_a,warehouse_b))


Comment on lines +135 to 139
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test/debug code should be removed before submitting. These variable definitions and print statement are left over from testing.

Suggested change
warehouse_a = {"PROD-001": 50, "PROD-002": 30}
warehouse_b = {"PROD-002": 20, "PROD-003": 40}
print(merge_inventory(warehouse_a,warehouse_b))

Copilot uses AI. Check for mistakes.
# ==========================================
Expand Down