Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tests/group15/lecture_test_code/lec10_complexity_part1.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

# lec10_test.py
import pytest
import lec10_complexity_part1 as lec

# ---------- linear_search ----------

@pytest.mark.parametrize(
"L,e,expect",
Expand Down
4 changes: 2 additions & 2 deletions tests/group15/lecture_test_code/lec7.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/group15/lecture_test_code/lec7_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import math
import lec7_debug_except as lec
import lec7 as lec

def test_rev_list():
"""測試串列反轉函式"""
Expand Down
5 changes: 3 additions & 2 deletions tests/group15/lecture_test_code/lec8.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -138,6 +138,7 @@ def __str__(self):
s.remove(3)
assert str(s) == "{4,6}"

####
try:
s.remove(3) # 應該拋出 ValueError
except ValueError as e:
Expand Down
2 changes: 1 addition & 1 deletion tests/group15/lecture_test_code/lec8_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from lec8_classes import Coordinate, Fraction, intSet
from lec8 import Coordinate, Fraction, intSet

# ==================================
# ===== 測試 Coordinate Class =====
Expand Down
3 changes: 1 addition & 2 deletions tests/group15/lecture_test_code/test_lec12_sorting.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
17 changes: 4 additions & 13 deletions tests/group15/lecture_test_code/test_lec9_inheritance.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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__ 和自己的方法"""
Expand All @@ -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")
Expand Down Expand Up @@ -98,7 +91,7 @@ def test_student_further_inheritance():

# ====================================================
# ===== 測試 Rabbit (類別變數和特殊方法) =====
# ====================================================
# =================================================
@pytest.fixture(autouse=True)
def reset_rabbit_tag():
"""這個 fixture 會在每次測試 Rabbit 前自動重設 tag,確保測試獨立性"""
Expand Down Expand Up @@ -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

# 測試與沒有父母的兔子比較
Expand Down