12: Class & Object

12-3: Object Variable

** ถ้าใช้งานบนมือถือหรือ tablet แนะนำให้ใช้ Chrome หรือ Safari เท่านั้น **

แบบฝึกหัด 12-3 ข้อที่ 1

คลาส Time ที่เขียนให้แล้ว มีไว้ผลิตอ็อบเจกต์ที่แทนเวลา จงเขียนฟังก์ชัน before(t1, t2) รับ t1 และ t2 เป็นอ็อบเจกต์เวลาในวันเดียวกัน ฟังก์ชันนี้คืนจริง ถ้า เวลา t1 มาก่อน t2 ไม่เช่นนั้น ก็คืนเท็จ

class Time: def __init__(self, h, m, s): self.hour = h self.minute = m self.second = s def before(t1, t2): class Time: def __init__(self, h, m, s): self.hour = h self.minute = m self.second = s def before(t1, t2): x1 = (t1.hour, t1.minute, t1.second) x2 = (t2.hour, t2.minute, t2.second) return x1 < x2 class Time: def __init__(self, h, m, s): self.hour = h self.minute = m self.second = s q = "`" fname = "before" qfname = q + fname + q func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + qfname) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') args = ['Time(10,20,30), Time(10,20,31)', 'Time(10,20,30), Time(10,21,20)', 'Time(10,20,30), Time(11,10,20)', 'Time(10,20,31), Time(10,20,30)', 'Time(10,21,20), Time(10,20,30)', 'Time(11,10,20), Time(10,20,30)'] for arg in args: fcall = fname + "(" + arg + ")" func.check_call(fcall).has_equal_value(incorrect_msg = q+fcall+q + " ให้ผลผิด")
แบบฝึกหัด 12-3 ข้อที่ 2

คลาส Point ที่เขียนให้แล้ว มีไว้ผลิตอ็อบเจกต์ที่แทนจุดในระนาบสองมิติ จงเขียนฟังก์ชัน closest_point_distance(points) รับ points เป็นลิสต์ของอ็อบเจกต์ Point สิ่งที่ฟังก์ชันนี้ต้องทำคือ คืนระยะห่างของคู่จุดที่ใกล้สุดใน points (คืนแค่ระยะห่าง ไม่ต้องคืนว่าจุดคู่ใดใกล้สุด)

class Point: def __init__(self, x, y): self.x = x self.y = y def closest_point_distance(points): class Point: def __init__(self, x, y): self.x = x self.y = y def distance(p1, p2): dx = p1.x - p2.x dy = p1.y - p2.y return (dx**2 + dy**2)**0.5 def closest_point_distance(points): min_d = distance(points[0], points[1]) for i in range(len(points)): for j in range(i+1, len(points)): d = distance(points[i], points[j]) if d < min_d: min_d = d return min_d class Point: def __init__(self, x, y): self.x = x self.y = y q = "`" fname = "closest_point_distance" qfname = q + fname + q func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + qfname) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์หนึ่งตัว') arg = '[Point(0,0), Point(10,10), Point(20,40), Point(0,20), Point(13,40)]' fcall = fname + "(" + arg + ")" func.check_call(fcall).has_equal_value(incorrect_msg = qfname + " ให้ผลผิด")
แบบฝึกหัด 12-3 ข้อที่ 3

คลาส Student มีไว้ผลิตอ็อบเจกต์ของนักเรียน ภายในเก็บเลขประจำตัว (ID) กับชื่อ (name) ส่วนคลาส Course มีไว้ผลิตอ็อบเจกต์ของวิชา ภายในเก็บรหัสวิชา (ID) ลิสต์ของนักเรียนทั้งหมดที่ลงทะเบียน (students) สิ่งที่ต้องการให้ทำในข้อนี้คือ เพิ่มตัวแปรประจำอ็อบเจกต์ชื่อ final เป็น dict ที่เก็บข้อมูลในรูปแบบ {เลขประจำตัวนักเรียน: คะแนนสอบปลายภาค} โดยให้เก็บเลขประจำตัวนักเรียนทุกคน (ได้ข้อมูลจาก students) และมีคะแนนปลายภาคของทุกคนเป็น 0

class Student: def __init__(self, ID, name): self.ID = ID self.name = name class Course: def __init__(self, ID, students): self.ID = ID self.students = students # สร้างตัวแปรประจำอ็อบเจกต์ชื่อ final # final เป็น dict ที่เก็บ ID ของ # นักเรียนแต่ละคนใน students เป็น key # โดยมี value เป็น 0 หมด # class Student: def __init__(self, ID, name): self.ID = ID self.name = name class Course: def __init__(self, ID, students): self.ID = ID self.students = students self.final = {} for s in students: self.final[s.ID] = 0 import types obj = types.SimpleNamespace q = "`" clsname = "Course" mname = "__init__" qclsname = q+clsname+q qmname = q+mname+q students = [obj(ID='01',name='A'),obj(ID='11',name='B'),obj(ID='04',name='C')] Ex().check_class_def(clsname, missing_msg="ไม่พบคลาส " + qclsname).\ check_body().check_function_def(mname, missing_msg="ไม่พบเมท็อด " + qmname).multi( has_equal_part_len('args', qmname + " รับพารามิเตอร์สามตัว"), check_args('self', missing_msg = "ช่วยเขียนพารามิเตอร์ตัวแรกของ "+ qmname + " ให้เป็น `self`"), check_body().set_context(obj(), "2110101", students).multi( has_equal_value(name = 'self.final', #error_msg = "Course ไม่มี self.final", incorrect_msg = "ค่าใน self.final ไม่ถูกต้อง"), ) )
แบบฝึกหัด 12-3 ข้อที่ 4

มีคลาส Student และ Course ที่เหมือนกับโจทย์ข้อที่แล้วให้แล้ว จงเขียนฟังก์ชัน update(course, students, scores) ที่มี students กับ scores เป็นลิสต์ขนาดเท่ากันโดยที่ นักเรียน students[i] ได้คะแนน scores[i] ฟังก์ชันนี้จะนำคะแนนนี้ไปตั้งค่าให้กับตัวแปร final ของอ็อบเจกต์ course

class Student: def __init__(self, ID, name): self.ID = ID self.name = name class Course: def __init__(self, ID, students): self.ID = ID self.students = students self.final = {} for s in students: self.final[s.ID] = 0 def update(course, students, scores): def update(course, students, scores): for i in range(len(students)): course.final[students[i].ID] = scores[i] import types obj = types.SimpleNamespace q = "`" fname = "update" qfname = q + fname + q students = [obj(ID='01',name='_'), obj(ID='51',name='_'), obj(ID='26',name='_')] final = {"01":0, "51":0, "26":0} course = obj(ID='101',students=students, final=final) scores = [100, 20, 40] Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + qfname).multi( has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สามตัว'), check_body().set_context(course, students, scores).\ has_equal_value(name = 'course.final', incorrect_msg = "ค่าใน course.final ไม่ถูกต้อง") )