---
# System prepended metadata

title: 期末考 411135053 林序叡

---

# 期末考 411135053 林序叡
## 1:
```python=
import re as r
def check_id(id_num):
    with open('3.txt', 'r') as f:
        content = f.read()
    pattern = r'ID:{}\s*TEL:(\d+)-(\d+)-(\d+)'.format(id_num)
    match = r.search(pattern, content)
    if match:
        print(''.join(match.groups()))
    else:
        print('學號不存在請重新輸入')
id_num = input('請輸入學號：')
check_id(id_num)
```
![](https://i.imgur.com/BXsrFT3.png)
![](https://i.imgur.com/X9Yt81b.png)

## 2:
```python=
department_dict = {
    '中文系': '01',
    '英美系': '02',
    '歷史系': '03',
    '應數系': '11',
    '化學系': '12',
    '生科系': '13',
    '資工系': '21',
    '材料系': '22',
    '電機系': '23',
    '光電系': '25'
}
def get_department_code(department):
    return department_dict.get(department)
import csv
def read_csv(file_path):
    records = []
    with open(file_path, 'r') as csv_file:
        reader = csv.DictReader(csv_file)
        for row in reader:
            records.append(row)
    return records
students = read_csv('2.csv')
def generate_student_id(student, department_codes, department_counts):
    department = student['Department']
    department_code = department_codes[department]
    department_count = department_counts[department]
    student_id = f'4111{department_code}{department_count:03d}'
    department_counts[department] += 1
    return student_id
department_counts = {
    department: 0
    for department in department_dict
}
students = read_csv('2.csv')
for student in students:
    student_id = generate_student_id(student, department_dict, department_counts)
    print(student_id)
def update_csv(file_path, students):
    with open(file_path, 'w') as csv_file:
        fieldnames = ['Department', 'Name', 'Student ID']
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        writer.writeheader()
        for student in students:
            writer.writerow(student)
for student in students:
    student_id = generate_student_id(student, department_dict, department_counts)
    student['Student ID'] = student_id
update_csv('students.csv', students)
students = read_csv('students.csv')
print(students)

```
![](https://i.imgur.com/uH6KIrV.png)

### test code:
```python=
import finalexam2
def test_get_department_code():
    assert get_department_code('中文系') == '01'
    assert get_department_code('電機系') == '23'
    assert get_department_code('光電系') == '25'
    assert get_department_code('應數系') == '11'

def test_read_csv():
    students = read_csv('students.csv')
    assert students == [{'Department': '電機系', 'Name': 'Eva Pedrazzini'}, {'Department': '光電系', 'Name': 'Dott. Guarino Palumbo'}, {'Department': '歷史系', 'Name': '徐慧君'}, {'Department': '中文系', 'Name': '陳哲瑋'}, {'Department': '電機系', 'Name': 'Alice Boone'}]
def test_generate_student_id():
    department_counts = {
        department: 0
        for department in department_dict
    }
    assert generate_student_id({'Department': '電機系', 'Name': 'Eva Pedrazzini'}, department_dict, department_counts) == '411123000'
    assert generate_student_id({'Department': '光電系', 'Name': 'Dott. Guarino Palumbo'}, department_dict, department_counts) == '411125001'
    assert generate_student_id({'Department': '歷史系', 'Name': '徐慧君'}, department_dict, department_counts) == '411123002'
    assert generate_student_id({'Department': '中文系', 'Name': '陳哲瑋'}, department_dict, department_counts) == '411123003'
    assert generate_student_id({'Department': '電機系', 'Name': 'Alice Boone'}, department_dict, department_counts) == '411123004'
test_get_department_code()
test_read_csv()
test_generate_student_id()
```
## 3:

```python=
import csv
class Student:
    school = '東華大學'
    def __init__(self, name, student_id, department):
        self.name = name
        self.student_id = student_id
        self.department = department
    def find_name_by_id(student_id):
        for student in students:
            if student.student_id == student_id:
                return student.name
        return '學號不存在'
    def find_id_by_name(name):
        for student in students:
            if student.name == name:
                return student.student_id
        return '姓名不存在'
students = []
with open('2.csv', 'r') as csv_file:
    reader = csv.DictReader(csv_file)
    for row in reader:
        students.append(Student(row['Name'], row['Student ID'], row['Department']))
#測試
print(Student.find_name_by_id('411123000'))
print(Student.find_name_by_id('411123001'))
print(Student.find_name_by_id('411112015'))
print(Student.find_id_by_name('周美玲'))
print(Student.find_id_by_name('Eva Pedrazzini'))
print(Student.find_id_by_name('Alice Boone'))
```
![](https://i.imgur.com/PQ6QIMT.png)



