diff --git a/tests/group15/lecture_test_code/lec10_complexity_part1.py b/tests/group15/lecture_test_code/lec10_complexity_part1.py new file mode 100644 index 000000000..fcb13f1e2 --- /dev/null +++ b/tests/group15/lecture_test_code/lec10_complexity_part1.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +""" +Created on Sun Oct 9 11:27:54 2016 + +@author: ericgrimson +""" + +def linear_search(L, e): + found = False + for i in range(len(L)): + if e == L[i]: + found = True + return found + +testList = [1, 3, 4, 5, 9, 18, 27] + +def search(L, e): + for i in range(len(L)): + if L[i] == e: + return True + if L[i] > e: + return False + return False + + +def isSubset(L1, L2): + for e1 in L1: + matched = False + for e2 in L2: + if e1 == e2: + matched = True + break + if not matched: + return False + return True + + +testSet = [1, 2, 3, 4, 5] +testSet1 = [1, 5, 3] +testSet2 = [1, 6] + +def intersect(L1, L2): + tmp = [] + for e1 in L1: + for e2 in L2: + if e1 == e2: + tmp.append(e1) + res = [] + for e in tmp: + if not(e in res): + res.append(e) + return res diff --git a/tests/group15/lecture_test_code/lec10_complexity_part1_test.py b/tests/group15/lecture_test_code/lec10_complexity_part1_test.py index b6acedaae..8fbea9a58 100644 --- a/tests/group15/lecture_test_code/lec10_complexity_part1_test.py +++ b/tests/group15/lecture_test_code/lec10_complexity_part1_test.py @@ -1,9 +1,6 @@ - -# lec10_test.py import pytest import lec10_complexity_part1 as lec -# ---------- linear_search ---------- @pytest.mark.parametrize( "L,e,expect", diff --git a/tests/group15/lecture_test_code/lec7.py b/tests/group15/lecture_test_code/lec7.py index 073b35739..f673a6426 100644 --- a/tests/group15/lecture_test_code/lec7.py +++ b/tests/group15/lecture_test_code/lec7.py @@ -1,8 +1,8 @@ import math -######################################## +##################################### ### EXAMPLE: Buggy code to reverse a list ### Try to debug it! (fixes needed are explained below) -######################################## +##################################### ##def rev_list_buggy(L): ## """ ## input: L, a list diff --git a/tests/group15/lecture_test_code/lec7_test.py b/tests/group15/lecture_test_code/lec7_test.py index 953f2c8d5..d5528c1c4 100644 --- a/tests/group15/lecture_test_code/lec7_test.py +++ b/tests/group15/lecture_test_code/lec7_test.py @@ -1,6 +1,6 @@ import pytest import math -import lec7_debug_except as lec +import lec7 as lec def test_rev_list(): """測試串列反轉函式""" diff --git a/tests/group15/lecture_test_code/lec8.py b/tests/group15/lecture_test_code/lec8.py index fef0a425c..34578611f 100644 --- a/tests/group15/lecture_test_code/lec8.py +++ b/tests/group15/lecture_test_code/lec8.py @@ -34,11 +34,11 @@ def Coordinate_test(): # print(c) -################# +################ ## EXAMPLE: simple class to represent fractions ## Try adding more built-in operations like multiply, divide ### Try adding a reduce method to reduce the fraction (use gcd) -################# +############## class Fraction(object): """ A number represented as a fraction @@ -138,6 +138,7 @@ def __str__(self): s.remove(3) assert str(s) == "{4,6}" +#### try: s.remove(3) # 應該拋出 ValueError except ValueError as e: diff --git a/tests/group15/lecture_test_code/lec8_test.py b/tests/group15/lecture_test_code/lec8_test.py index 28cf4123b..202ece1e7 100644 --- a/tests/group15/lecture_test_code/lec8_test.py +++ b/tests/group15/lecture_test_code/lec8_test.py @@ -1,6 +1,6 @@ import pytest -from lec8_classes import Coordinate, Fraction, intSet +from lec8 import Coordinate, Fraction, intSet # ================================== # ===== 測試 Coordinate Class ===== diff --git a/tests/group15/lecture_test_code/test_lec12_sorting.py b/tests/group15/lecture_test_code/test_lec12_sorting.py index 367a06111..9361eef80 100644 --- a/tests/group15/lecture_test_code/test_lec12_sorting.py +++ b/tests/group15/lecture_test_code/test_lec12_sorting.py @@ -1,6 +1,5 @@ -import tests.group15.lecture_test_code.add_path import pytest -import mit_ocw_exercises.lec12_sorting as S +import lec12_sorting as S @pytest.mark.parametrize( diff --git a/tests/group15/lecture_test_code/test_lec9_inheritance.py b/tests/group15/lecture_test_code/test_lec9_inheritance.py index 39a6c0460..f5e109426 100644 --- a/tests/group15/lecture_test_code/test_lec9_inheritance.py +++ b/tests/group15/lecture_test_code/test_lec9_inheritance.py @@ -1,12 +1,9 @@ import pytest import io import sys -# 假設你的主檔案名稱是 lec9_inheritance.py from lec9_inheritance import Animal, Cat, Person, Student, Rabbit -# =============================== -# ===== 測試 Animal (父類別) ===== -# =============================== +# ===== 測試 Animal (父類別) === def test_animal_base(): """測試 Animal 基礎功能的設定與取得""" animal = Animal(10) @@ -21,9 +18,7 @@ def test_animal_base(): assert str(animal) == "animal::10" -# ======================================== # ===== 測試 Cat (繼承 Animal) ===== -# ======================================== def test_cat_inheritance_and_methods(): """測試 Cat 是否繼承了 Animal 的屬性,以及自己的方法""" cat = Cat(5) @@ -45,7 +40,7 @@ def test_cat_inheritance_and_methods(): assert captured_output.getvalue().strip() == "meow" # ========================================= -# ===== 測試 Person (繼承 Animal) ===== +# ===== 測試 Person (繼承 Animal) === # ========================================= def test_person_extended_init_and_methods(): """測試 Person 擴充的 __init__ 和自己的方法""" @@ -67,10 +62,8 @@ def test_person_extended_init_and_methods(): p1.age_diff(p2) sys.stdout = sys.__stdout__ assert captured_output.getvalue().strip() == "5 year difference" - -# ========================================= + # ===== 測試 Student (繼承 Person) ===== -# ========================================= def test_student_further_inheritance(): """測試 Student 的多層繼承和方法""" s = Student("Alice", 20, "CS") @@ -98,7 +91,7 @@ def test_student_further_inheritance(): # ==================================================== # ===== 測試 Rabbit (類別變數和特殊方法) ===== -# ==================================================== +# ================================================= @pytest.fixture(autouse=True) def reset_rabbit_tag(): """這個 fixture 會在每次測試 Rabbit 前自動重設 tag,確保測試獨立性""" @@ -129,8 +122,6 @@ def test_rabbit_special_methods_add_and_eq(): # 測試 __eq__ r4 = r2 + r1 # rid: 4, parents: r2, r1 - # 現在,使用 '==' 來測試你定義的值相等邏輯 - # 假設你採用了修正後的 __eq__ 方法 assert r3 == r4 # 測試與沒有父母的兔子比較