-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_both_tasks.py
More file actions
113 lines (90 loc) · 3.73 KB
/
Copy pathtest_both_tasks.py
File metadata and controls
113 lines (90 loc) · 3.73 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from assignment_solution import assign, Analyser
def test_task1():
print("=== Testing Task 1: Two Students on a Bus ===\n")
# Test data from assignment
L = 16
roads = [(0,1,3), (0,2,5), (0,3,10), (1,4,1), (2,5,2), (5,6,3),
(2,7,4), (0,8,1), (0,9,1), (0,10,1), (0,11,1), (6,12,2), (6,13,4),
(6,14,3), (7,15,1)]
# Test Case 1: Should return valid assignment
print("Test Case 1: Expected valid assignment")
students = [4, 10, 8, 12, 12, 13, 13, 13, 13, 13, 13, 13, 5, 7, 7, 7, 7, 7, 15, 15, 7, 4, 8, 9]
buses = [(0, 3, 5), (6, 5, 10), (15, 5, 10), (6, 5, 10)]
D = 5
T = 22
result1 = assign(L, roads, students, buses, D, T)
print(f"Result: {result1}")
if result1:
total_assigned = sum(1 for x in result1 if x != -1)
bus_counts = [0] * len(buses)
for bus_id in result1:
if bus_id != -1:
bus_counts[bus_id] += 1
print(f"Total students assigned: {total_assigned} (required: {T})")
print("Bus assignments:")
all_valid = True
for j, count in enumerate(bus_counts):
min_cap, max_cap = buses[j][1], buses[j][2]
valid = min_cap <= count <= max_cap
status = "✓" if valid else "✗"
print(f" Bus {j}: {count} students (range: {min_cap}-{max_cap}) {status}")
if not valid:
all_valid = False
if total_assigned == T and all_valid and all(count > 0 for count in bus_counts):
print("✓ VALID ASSIGNMENT")
else:
print("✗ INVALID ASSIGNMENT")
else:
print("✗ No valid assignment found")
print("\n" + "="*50)
# Test Case 2: Should return None
print("Test Case 2: Expected None")
students2 = [5, 8, 3, 7, 7, 15, 15, 8, 15, 7, 6, 15]
buses2 = [(0, 3, 5), (15, 5, 6)]
D2 = 5
T2 = 7
result2 = assign(L, roads, students2, buses2, D2, T2)
print(f"Result: {result2}")
if result2 is None:
print("✓ CORRECTLY RETURNED None")
else:
print("✗ Should have returned None")
def test_task2():
print("\n\n=== Testing Task 2: Music Pattern Analysis ===\n")
# Test case from the assignment
demo_songs = ["cegec", "gdfhd", "cdfhd"]
analyser = Analyser(demo_songs)
print("Test sequences:", demo_songs)
print()
# Test different pattern lengths
test_lengths = [2, 3, 4]
for K in test_lengths:
result = analyser.getFrequentPattern(K)
print(f"K={K} => {result}")
# Verify the result is correct length
if result:
assert len(result) == K, f"Pattern length should be {K}, got {len(result)}"
print(f" Pattern: {''.join(result)}, Length: {len(result)} ✓")
else:
print(" No pattern found")
print()
# Test interval-based matching
print("=== Testing Interval-Based Pattern Matching ===\n")
# These should be considered the same pattern (intervals [+1, +1])
test_songs = ["abc", "bcd", "cde"]
analyser2 = Analyser(test_songs)
print("Test sequences for interval matching:", test_songs)
result = analyser2.getFrequentPattern(3)
print(f"Most frequent pattern of length 3: {result}")
# Verify frequency counting is correct
three_length_patterns = analyser2.pattern_freq_by_length[3]
if three_length_patterns:
intervals, freq, example = three_length_patterns[0]
print(f"Frequency for length 3 patterns: {freq}")
if freq == 3:
print("✓ CORRECT: All 3 songs contributed to the same pattern")
else:
print("✗ WRONG: Frequency should be 3")
if __name__ == "__main__":
test_task1()
test_task2()