Skip to content
Open
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
34 changes: 34 additions & 0 deletions Week03/sequences_heval_sogut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def remove_duplicates(seq: list) -> list:
"""
Remove duplicate elements from a list while preserving the original order.
Only the first occurrence of each element is kept.
"""
seen = set()
result = []
for item in seq:
if item not in seen:
seen.add(item)
result.append(item)
return result


def list_counts(seq: list) -> dict:
"""
Count how many times each element appears in a list.
Returns a dictionary where keys are elements and values are counts.
"""
counts = {}
for item in seq:
counts[item] = counts.get(item, 0) + 1
return counts


def reverse_dict(d: dict) -> dict:
"""
Reverse the keys and values of a dictionary.
If multiple keys have the same value, the last one overwrites the earlier ones.
"""
reversed_dict = {}
for key, value in d.items():
reversed_dict[value] = key
return reversed_dict