From c01736bfc8fbfdb9c8d2fd1ce92b9665241ed03b Mon Sep 17 00:00:00 2001 From: gktgkayac <230315009@ogr.cbu.edu.tr> Date: Sat, 14 Nov 2026 20:29:04 +0300 Subject: [PATCH] Implement utility functions for list and dict operations Added functions to remove duplicates from a list, count element occurrences, and reverse a dictionary. --- Week03/sequences_goktug_kayaci.py | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Week03/sequences_goktug_kayaci.py diff --git a/Week03/sequences_goktug_kayaci.py b/Week03/sequences_goktug_kayaci.py new file mode 100644 index 00000000..93205793 --- /dev/null +++ b/Week03/sequences_goktug_kayaci.py @@ -0,0 +1,38 @@ +def remove_duplicates(seq: list) -> list: + """ + Remove repeated elements from a list. + """ + unique_items = [] + + for value in seq: + if value not in unique_items: + unique_items.append(value) + + return unique_items + + +def list_counts(seq: list) -> dict: + """ + Count how many times each element appears in a list. + """ + result = {} + + for value in seq: + if value in result: + result[value] += 1 + else: + result[value] = 1 + + return result + + +def reverse_dict(d: dict) -> dict: + """ + Exchange the keys and values of a dictionary. + """ + reversed_dict = {} + + for key, value in d.items(): + reversed_dict[value] = key + + return reversed_dict