-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency_analyis.py
More file actions
37 lines (28 loc) · 1.47 KB
/
frequency_analyis.py
File metadata and controls
37 lines (28 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from typing import Dict, List, Tuple, Set
survey_responses: List[str] = ["C#", "Python", "JavaScript", "Java", "C++", "C#", "Python", "JavaScript", "Java", "C++", "C#", "Python", "JavaScript", "Java", "C++"]
def calculate_frequenccies(items: List[str]) -> Dict[str, int]:
"""returns a dictionary where keys are items and values and their frequencies"""
frequencies: Dict[str, int] = {}
for item in items:
frequencies[item] = frequencies.get(item, 0) + 1
return frequencies
def unique_items(items: List[str]) -> Set[str]:
"""returns a set of unique items from the list"""
return set(items)
def top_n_items(frequencies: Dict[str, int], n: int) -> List[Tuple[str, int]]:
"""returns the top N most frequent items using dictionary items and tuple unpacking"""
sorted_items: List[Tuple[str, int]] = sorted(frequencies.items(), key=lambda x: x[1], reverse=True)
return sorted_items[:n]
def print_summary(items: List[str], n: int = 3) -> None:
"""Print a clear, readable summary that shows frequencies, unique item count, and top results"""
freqs = calculate_frequenccies(items)
unique_count = len(unique_items(items))
top_items = top_n_items(freqs, n)
print("Frequency Analysis Summary:")
print(f"Total unique items: {unique_count}")
print("Top items by frequency:")
for item, freq in top_items:
print(f" {item}: {freq}")
if __name__ == "__main__":
print_summary(survey_responses)
print_summary(survey_responses, 6)