Question
CODE IS IN PYTHON outline for ordered_list.py and ordered_list_tests.py has been given work according to it please thanks create test cases too that covers 100%
CODE IS IN PYTHON
outline for ordered_list.py and ordered_list_tests.py has been given
work according to it please thanks
create test cases too that covers 100% of the code
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#ordered_lists.py
from __future__ import annotations
from typing import Any, Optional
class Node:
"""A node to be used in a doubly linked list."""
def __init__(self, value: Any, prev: Optional[Node], nxt: Optional[Node]):
self.value = value
# NOTE: This means that if prev and nxt are None, self.prev and
# self.next will be self. You may find this useful. This means
# that self.prev and self.next aren't Optional Nodes, they are
# always Nodes.
self.prev: Node = prev or self
self.next: Node = nxt or self
class OrderedList:
"""A circular, doubly linked list, ordered from lowest to highest.
The contents of the list *must* have a accurate notation of less
than and of equality. That is to say, the contents of the list must
implement both __lt__ and __eq__. This *does not* mean that your
OrderedList (or your Nodes) should have __lt__ and __eq__.
Your implementation should use a single dummy node as the "head".
"""
def __init__(self) -> None:
...
def insert(lst: OrderedList, value: Any) -> None:
"""Insert the value into the list in the proper (ordered) location."""
def remove(lst: OrderedList, value: Any) -> None:
"""Remove the first occurrence of value from the list.
Raises ValueError if the value is not present.
"""
def contains(lst: OrderedList, value: Any) -> bool:
"""Return True if the value is in the list, False otherwise."""
def index_of(lst: OrderedList, value: Any) -> int:
"""Return the index of the first occurrence of value in the list.
Raises ValueError if the value is not present.
"""
def get(lst: OrderedList, index: int) -> Any:
"""Return the value at index in the list.
Raises IndexError if the index is out of range.
"""
def pop(lst: OrderedList, index: int) -> Any:
"""Remove and returns the value at index in the list.
Raises IndexError if the index is out of range.
"""
def is_empty(lst: OrderedList) -> bool:
"""Return True if the list is empty, False otherwise."""
def size(lst: OrderedList) -> int:
"""Return the number if items in the list."""
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#ordered_list_tests.py
import unittest
from ordered_list import (
OrderedList,
contains,
get,
index_of,
insert,
is_empty,
pop,
remove,
size,
)
class Tests(unittest.TestCase):
def test_simple(self) -> None:
my_list = OrderedList()
self.assertEqual(size(my_list), 0)
self.assertTrue(is_empty(my_list))
insert(my_list, 10)
self.assertEqual(size(my_list), 1)
self.assertTrue(contains(my_list, 10))
self.assertFalse(is_empty(my_list))
self.assertEqual(index_of(my_list, 10), 0)
self.assertEqual(get(my_list, 0), 10)
remove(my_list, 10)
self.assertEqual(size(my_list), 0)
self.assertFalse(contains(my_list, 10))
self.assertTrue(is_empty(my_list))
insert(my_list, 10)
self.assertEqual(pop(my_list, 0), 10)
self.assertEqual(size(my_list), 0)
self.assertFalse(contains(my_list, 10))
self.assertTrue(is_empty(my_list))
if __name__ == "__main__":
unittest.main()
1 Doubly Linked Ordered List The structure of an ordered list is a collection of items where each item holds a relative position that is based upon some underlying characteristic of the item. The ordering is typically either ascending or descending, and we assume that list items have a meaningful comparison operation that is already defined. Many of the ordered list operations are the same as those of the unordered list. Implement the following operations for an ordered list of items ordered in ascending order using a circular doubly linked list. That is to say the "first" item in the list will be the smallest and the "last" item in the list will be the largest. Your implementation should use a single dummy node as the "head" as seen in class. - insert This function takes an ordered list and a value as arguments and inserts the value into the proper location in the list. This function must have O(n) performance in the worst case. - remove This function takes an ordered list and a value as arguments and removes the value from the list. If the value is not in the list, then this function should raise a ValueError. This function must have O(n) performance in the worst case. - contains This function takes an ordered list and a value as arguments and returns whether or not the value is in the list (True if it is, False otherwise). This function must have O(n) performance in the worst case. - index_of This function takes an ordered list and a value as arguments and returns the index of the first occurrence of the value in the list. If the value is not in the list, then this function should raise a ValueError. This function must have O(n) performance in the worst case. - get This function takes an ordered list and an index as arguments and returns the value at the given index. If the index is outside the bounds of the list, then this function should raise an IndexError. This function must have O(n) performance in the worst case. - pop This function takes an ordered list and an index as arguments and removes (and returns) the value at the given index. If the index is outside the bounds of the list, then this function should raise an IndexError. This function must have O(n) performance in the worst case. - is_empty This function takes an ordered list as an argument and returns whether or not the list is empty (True if it is, False otherwise). This function must have O(1) performance in the worst case. - size This function takes an ordered list as an argument and returns the number of items in the list. This function must have O(1) performance in the worst case. Note that you can (and should) assume that all items added to your list are comparable with the
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started