From adb17b57434e7acc94a0487e8a252ff4e447f462 Mon Sep 17 00:00:00 2001 From: MstrPikachu <31784486+MstrPikachu@users.noreply.github.com> Date: Sat, 22 Jan 2022 22:08:48 -0500 Subject: [PATCH] A1 P1 Q1 Fix bug in heuristic 3 Fix faulty while loop and now returns true at the end --- assignments/A1/a1_part1.py | 5 +++-- assignments/A1/a1_part1_test.py | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/assignments/A1/a1_part1.py b/assignments/A1/a1_part1.py index 5cc50e1..2578853 100644 --- a/assignments/A1/a1_part1.py +++ b/assignments/A1/a1_part1.py @@ -164,9 +164,10 @@ class CountLinkedList(LinkedList): return True cur = self._first - while (not cur.next) or cur.next.access_count < count: + while cur.next and cur.next.access_count < count: cur = cur.next - cur.next = _CountNode(item, cur.next.next, count) # insert node + cur.next = _CountNode(item, cur.next.next if cur.next else None, count) # insert node + return True def append(self, item: Any) -> None: """Add the given item to the end of this linked list. diff --git a/assignments/A1/a1_part1_test.py b/assignments/A1/a1_part1_test.py index 8fa2c95..4848dd8 100644 --- a/assignments/A1/a1_part1_test.py +++ b/assignments/A1/a1_part1_test.py @@ -28,5 +28,13 @@ import pytest from a1_part1 import MoveToFrontLinkedList, SwapLinkedList, CountLinkedList +def test_all_lists_true() -> None: + movetofront = [MoveToFrontLinkedList(range(i)) for i in range(100)] + swap = [SwapLinkedList(range(i)) for i in range(100)] + count = [CountLinkedList(range(i)) for i in range(100)] + assert(all(all(x in movetofront[i] for x in range(i)) for i in range(100))) + assert(all(all(x in swap[i] for x in range(i)) for i in range(100))) + assert(all(all(x in count[i] for x in range(i)) for i in range(100))) + if __name__ == '__main__': pytest.main(['a1_part1_test.py', '-v'])