diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..184c4eb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "python.testing.unittestArgs": [ + "-v", + "-s", + ".", + "-p", + "test_*.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true +} \ No newline at end of file diff --git a/__pycache__/analytics.cpython-312.pyc b/__pycache__/analytics.cpython-312.pyc new file mode 100644 index 0000000..cc6b214 Binary files /dev/null and b/__pycache__/analytics.cpython-312.pyc differ diff --git a/__pycache__/test_analytics.cpython-312.pyc b/__pycache__/test_analytics.cpython-312.pyc new file mode 100644 index 0000000..592ebbf Binary files /dev/null and b/__pycache__/test_analytics.cpython-312.pyc differ diff --git a/analytics.py b/analytics.py index 8acadc8..5603621 100644 --- a/analytics.py +++ b/analytics.py @@ -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: + return i + else: + return list +# print(filter_sales_above_threshold(([100, 250, 75, 300], 150))) + + def count_product_codes(codes: list, prefix: str): """ @@ -34,6 +42,13 @@ def count_product_codes(codes: list, prefix: str): """ # TODO: Write your code here pass + count = 0 + for code in codes: + if code.startswith(prefix): + count += 1 + return count + + def calculate_moving_average(numbers: list, window_size: int): @@ -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) + result=total/window_size + return result +# print(calculate_moving_average([10, 20, 30, 40, 50], 3)) # ========================================== # SECTION B: DICTIONARY OPERATIONS diff --git a/practise.py b/practise.py new file mode 100644 index 0000000..9f1cc95 --- /dev/null +++ b/practise.py @@ -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) + result=total/window_size + return result +print(calculate_moving_average([10, 20, 30, 40, 50], 3)) \ No newline at end of file