Skip to content
Open

. #3

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-312.pyc
Binary file not shown.
Binary file added __pycache__/test_analytics.cpython-312.pyc
Binary file not shown.
30 changes: 24 additions & 6 deletions analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ def filter_sales_above_threshold(sales: list, threshold: int):
- Return empty list if no values qualify
- Use a loop or list comprehension
"""
# TODO: Write your code here
pass

# # TODO: Write your code here
# pass
list=[]
for i in list:
if i>threshold:
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.

Missing spacing around the comparison operator. Should be if i > threshold: according to PEP 8 style guidelines.

Suggested change
if i>threshold:
if i > threshold:

Copilot uses AI. Check for mistakes.
return i
Comment on lines +22 to +24
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.

Variable name i is not descriptive for this context. Use a more meaningful name like sale or amount to improve code readability.

Suggested change
for i in list:
if i>threshold:
return i
for sale in list:
if sale>threshold:
return sale

Copilot uses AI. Check for mistakes.
else:
return list
Comment on lines +21 to +26
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.

Using list as a variable name shadows Python's built-in list type, which is a common anti-pattern and violates Python naming best practices. Use a descriptive name like result or filtered_sales instead.

Suggested change
list=[]
for i in list:
if i>threshold:
return i
else:
return list
filtered_sales = []
for sale in sales:
if sale > threshold:
filtered_sales.append(sale)
return filtered_sales

Copilot uses AI. Check for mistakes.
Comment on lines +21 to +26
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 implementation has multiple critical bugs:

  1. Variable shadowing: Line 21 uses list as a variable name, which shadows Python's built-in list type.
  2. Iterating over empty list: Line 22 iterates over the newly created empty list, so the loop body never executes.
  3. Incorrect parameter usage: The function should iterate over the sales parameter, not the local list variable.
  4. Premature return: Lines 24 and 26 return immediately on the first iteration, preventing the function from collecting all qualifying values.
  5. Wrong return type: The function returns individual integers instead of a list of all qualifying values.

The correct implementation should iterate over sales, collect all values greater than threshold in a result list, and return that list after checking all values.

Suggested change
list=[]
for i in list:
if i>threshold:
return i
else:
return list
result = []
for sale in sales:
if sale > threshold:
result.append(sale)
return result

Copilot uses AI. Check for mistakes.
# print(filter_sales_above_threshold(([100, 250, 75, 300], 150)))



def count_product_codes(codes: list, prefix: str):
"""
Expand All @@ -34,6 +42,13 @@ def count_product_codes(codes: list, prefix: str):
"""
# TODO: Write your code here
pass
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.

Unreachable code: The pass statement on line 44 is never executed because the code after it (lines 45-49) will always run first. Since lines 45-49 contain the actual implementation, this pass statement should be removed.

Suggested change
pass

Copilot uses AI. Check for mistakes.
count = 0
for code in codes:
if code.startswith(prefix):
count += 1
return count




def calculate_moving_average(numbers: list, window_size: int):
Expand All @@ -50,9 +65,12 @@ def calculate_moving_average(numbers: list, window_size: int):
- Return the average as a float rounded to 2 decimal places
- Return 0.0 for empty list
"""
# TODO: Write your code here
pass

# # TODO: Write your code here
# pass
total=sum(numbers)
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.

Missing spacing around the assignment operator. Should be total = sum(numbers) according to PEP 8 style guidelines.

Suggested change
total=sum(numbers)
total = sum(numbers)

Copilot uses AI. Check for mistakes.
result=total/window_size
Comment on lines +70 to +71
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.

Missing spacing around the assignment and division operators. Should be result = total / window_size according to PEP 8 style guidelines.

Suggested change
total=sum(numbers)
result=total/window_size
total = sum(numbers)
result = total / window_size

Copilot uses AI. Check for mistakes.
return result
Comment on lines +70 to +72
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 implementation has multiple bugs that fail the documented requirements:

  1. Incorrect window calculation: The function sums all elements in numbers instead of only the last window_size elements. It should use numbers[-window_size:].
  2. Missing edge case handling: Returns ZeroDivisionError for empty lists instead of 0.0 as specified.
  3. Missing rounding: The result should be rounded to 2 decimal places using round(result, 2).
  4. Wrong logic for fewer elements: When the list has fewer elements than window_size, it incorrectly divides by window_size instead of the actual number of elements.
Suggested change
total=sum(numbers)
result=total/window_size
return result
if not numbers:
return 0.0
window = numbers[-window_size:] if window_size > 0 else []
avg = sum(window) / len(window) if window else 0.0
return round(avg, 2)

Copilot uses AI. Check for mistakes.
# print(calculate_moving_average([10, 20, 30, 40, 50], 3))

# ==========================================
# SECTION B: DICTIONARY OPERATIONS
Expand Down
77 changes: 77 additions & 0 deletions practise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# # def filter_sales_above_threshold(sales: list, threshold: int):
# # """
# # QUESTION 1
# # ----------------------------------------
# # Given a list of sales amounts (integers), return a NEW list containing
# # only values above the threshold.

# # Example: filter_sales_above_threshold([100, 250, 75, 300], 150) → [250, 300]

# # Logic:
# # - Do NOT modify the original list
# # - Return empty list if no values qualify
# # - Use a loop or list comprehension
# # """
# # # TODO: Write your code here
# # pass

# # list=[]
# # for i in list:
# # if i>threshold:
# # return i
# # else:
# # return list
# # print(filter_sales_above_threshold([100, 250, 75, 300], 150))


# # def count_product_codes(codes: list, prefix: str):
# # """
# # QUESTION 2
# # ----------------------------------------
# # Given a list of product codes (strings) and a prefix (string),
# # count how many codes START with the given prefix (case-sensitive).

# # Example: count_product_codes(["PROD-001", "PROD-002", "SERV-001"], "PROD") → 2

# # Logic:
# # - Return 0 if no matches found
# # """
# # # # TODO: Write your code here
# # # pass


# # prefix=0
# # count=(["PROD-001", "PROD-002", "SERV-001"], "PROD")
# # count_prefix=count[1]
# # for i in count:
# # prefix=prefix+1
# # print(prefix)

# count = 0
# for code in codes:
# if code.startswith(prefix):
# count += 1
# return count



def calculate_moving_average(numbers: list, window_size: int):
"""
QUESTION 3
----------------------------------------
Calculate the average of the LAST window_size elements in the list.

Example: calculate_moving_average([10, 20, 30, 40, 50], 3) → 40.0
(average of last 3: 30, 40, 50)

Logic:
- If the list has fewer elements than window_size, use all available elements
- Return the average as a float rounded to 2 decimal places
- Return 0.0 for empty list
"""
# # TODO: Write your code here
# pass
total=sum(numbers)
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.

Missing spacing around the assignment operator. Should be total = sum(numbers) according to PEP 8 style guidelines.

Suggested change
total=sum(numbers)
total = sum(numbers)

Copilot uses AI. Check for mistakes.
result=total/window_size
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.

Missing spacing around the assignment and division operators. Should be result = total / window_size according to PEP 8 style guidelines.

Suggested change
result=total/window_size
result = total / window_size

Copilot uses AI. Check for mistakes.
return result
Comment on lines +72 to +76
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 implementation has multiple bugs that fail the documented requirements:

  1. Incorrect window calculation: The function sums all elements in numbers instead of only the last window_size elements. It should use numbers[-window_size:].
  2. Missing edge case handling: Returns ZeroDivisionError for empty lists instead of 0.0 as specified.
  3. Missing rounding: The result should be rounded to 2 decimal places using round(result, 2).
  4. Wrong logic for fewer elements: When the list has fewer elements than window_size, it incorrectly divides by window_size instead of the actual number of elements.
Suggested change
# # TODO: Write your code here
# pass
total=sum(numbers)
result=total/window_size
return result
if not numbers:
return 0.0
window = numbers[-window_size:]
avg = sum(window) / len(window)
return round(avg, 2)

Copilot uses AI. Check for mistakes.
print(calculate_moving_average([10, 20, 30, 40, 50], 3))