-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudents.py
More file actions
63 lines (47 loc) · 2.14 KB
/
Copy pathstudents.py
File metadata and controls
63 lines (47 loc) · 2.14 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import re
from typing import Dict, List, Tuple
def get_code_and_name(student_names: str) -> Tuple[str, str]:
student_names = student_names.strip().split(', ')
student_fullname = student_names[0].strip()
if len(student_names) == 1:
name_pieces = re.search(r'^([A-Z-]+\s)+([A-Za-z-]+)', student_fullname)
student_givenname = name_pieces.group(2).capitalize()
else:
student_givenname = student_names[1]
return student_fullname, student_givenname
def warn_about_duplicates(duplicates: set) -> None:
print(f"The following names are duplicated!! {duplicates}")
def check_name_conflicts(name_dict: Dict[str, str]) -> set:
names = list(name_dict.values())
duplicates = set([name for name in names if names.count(name) > 1])
if duplicates:
warn_about_duplicates(duplicates)
return duplicates
def parse_course_list(filename: str) -> Dict[str, str]:
with open(filename, 'r') as f:
values = {}
for student_info in f.readlines():
student_code, student_name = get_code_and_name(student_info)
values[student_code] = student_name
return values
def load_class_lists(course_names: List[str], class_paths: List[str]) -> Dict[str, Dict[str, str]]:
classes = {}
for course_name, path in zip(course_names, class_paths):
classes[course_name] = parse_course_list(path)
return classes
def get_first_names_in_course(course: Dict[str, str]) -> List[str]:
return list(course.values())
def get_first_names_in_courses(courses: Dict[str, Dict[str, str]]) -> Dict[str, List[str]]:
courses_with_given_name = {}
for course_name, students in courses.items():
courses_with_given_name[course_name] = get_first_names_in_course(students)
return courses_with_given_name
def create_alias(full_name: str) -> str:
first_name, *surnames = full_name.split()
surname = surnames[-1]
first_letter = surname[0]
vowels = "AEIOUaeiou"
surname_alias = "".join([c for c in surname if c not in vowels])
if first_letter in vowels:
surname_alias = first_letter + surname_alias
return f"{first_name.lower()}.{surname_alias.lower()}"