Question

Finish each function python 3 LList.py class node(object): """ A version of the Node class with...

Finish each function
python 3

LList.py

class node(object):

""" A version of the Node class with public attributes.

This makes the use of node objects a bit more convenient for

implementing LList class.

  

Since there are no setters and getters, we use the attributes directly.

  

This is safe because the node class is defined in this module.

No one else will use this version of the class.
'''

def __init__(self, data, next=None):
"""
Create a new node for the given data.
Pre-conditions:
data: Any data value to be stored in the node
next: Another node (or None, by default)
"""
self.data = data
self.next = next
  
# Note: use the attributes directly; no setters or getters!

class LList(object):
def __init__(self):
"""
Purpose
creates an empty list
"""
self._size = 0 # how many elements in the stack
self._head = None # the node chain starts here; initially empty
self._tail = None # the last node in the node chain; initially empty

############
def retrieve_data_at_index(self,idx):
"""
Purpose
Return the value stored at the index idx
Preconditions:
:param idx: a non-negative integer
Post-conditions:
none
Return:
:return (True, val) if val is stored at index idx and idx is valid
:return (False, None) if the idx is not valid for the list
"""
pass

######
def set_data_at_index(self, idx, val):
"""
Purpose
Store val into alist at the index idx
Preconditions:
:param val: a value of any kind
:param idx: a non-negative integer
Post-conditions:
The value stored at index idx changes to val
Return:
:return True if the index was valid, False otherwise
"""
pass

############
def insert_value_at_index(self, val, idx):
"""
Purpose
Insert val into alist at index idx
Preconditions:
:param val: a value of any kind
:param idx: a valid index for the list
Post-conditions:
The list increases in size.
The new value is at index idx.
The values previously in the list at idx or later appear after the new value.
Return:
:return If the index is valid, insert_value_at_index returns True.
:return If the index is not valid, insert_value_at_index returns False.
"""
pass

############
def delete_item_at_index(self, idx):
"""
Purpose
Delete the value at given index .
Preconditions:
:param idx: a non-negative integer
Post-conditions:
The list decreases in size if the index is valid
The value at idx is no longer in the list.
Return:
:return True if index was valid, False otherwise
"""
pass

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

class node(object):
    def __init__(self, data, next=None):
    """
    Create a new node for the given data.
    Pre-conditions:
    data: Any data value to be stored in the node
    next: Another node (or None, by default)
    """
        self.data = data
        self.next = next
      
    # Note: use the attributes directly; no setters or getters!

class LList(object):
    
    def __init__(self):
    """
    Purpose
    creates an empty list
    """
        self._size = 0 # how many elements in the stack
        self._head = None # the node chain starts here; initially empty
        self._tail = None # the last node in the node chain; initially empty

    ############
    def retrieve_data_at_index(self,idx):
    """
    Purpose
    Return the value stored at the index idx
    Preconditions:
    :param idx: a non-negative integer
    Post-conditions:
    none
    Return:
    :return (True, val) if val is stored at index idx and idx is valid
    :return (False, None) if the idx is not valid for the list
    """
        if not 0<=idx<self._size:
            return (False, None)
        itr = self._head
        for i in range(idx):
            itr = itr.next
        val = itr.data
        return (True, val)

    ######
    def set_data_at_index(self, idx, val):
    """
    Purpose
    Store val into alist at the index idx
    Preconditions:
    :param val: a value of any kind
    :param idx: a non-negative integer
    Post-conditions:
    The value stored at index idx changes to val
    Return:
    :return True if the index was valid, False otherwise
    """
        if not 0<=id<self._size:
            return False
        itr = self._head
        for i in range(idx):
            itr = itr.next
        itr.data = val
        return True
    
    ############
    def insert_value_at_index(self, val, idx):
    """
    Purpose
    Insert val into alist at index idx
    Preconditions:
    :param val: a value of any kind
    :param idx: a valid index for the list
    Post-conditions:
    The list increases in size.
    The new value is at index idx.
    The values previously in the list at idx or later appear after the new value.
    Return:
    :return If the index is valid, insert_value_at_index returns True.
    :return If the index is not valid, insert_value_at_index returns False.
    """
        if not 0<=idx<self._size:
            return False
        if idx==0:
            tmp = self._head
            self._head = node(val, tmp)
            self._size += 1
            return True
        itr = self._head
        for i in range(idx-1):
            itr = itr.next
        tmp = node(val, itr.next)
        itr.next = tmp
        self._size += 1
        return True
    

    ############
    def delete_item_at_index(self, idx):
    """
    Purpose
    Delete the value at given index .
    Preconditions:
    :param idx: a non-negative integer
    Post-conditions:
    The list decreases in size if the index is valid
    The value at idx is no longer in the list.
    Return:
    :return True if index was valid, False otherwise
    """
        if not 0<=idx<self._size:
            return False
        if idx==0:
            tmp = self._head
            self._head = tmp.next
            del tmp
            self._size -= 1
            return True
        itr = self._head
        for i in range(idx-1):
            itr = itr.next
        tmp = itr.next
        itr.next = tmp.next
        del tmp
        if idx == self._size - 1:
            self._last = itr
        self._size -= 1
        return True
    
    def append_item(self, val): #i have added this method extra to make this class complete
        if self._head is None:
            self._head = node(val)
            self._last = self._head
        else:
            self._last = node(val)
        self._size += 1

Code Screenshots:

itr = self._head for i in range (idx-1): itr = itr.next tmp = node (val, itr.next) itr.next = tmp self. size += 1 return True

As per the given methods I've completed them but LList class isn't completed without append method because using insert we cannot insert elements into an empty list.

Thank you! Hit like if you like my work.

Add a comment
Know the answer?
Add Answer to:
Finish each function python 3 LList.py class node(object): """ A version of the Node class with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • """ Add_to_front(self, val) and add_to_back(self, val) functions can be used which are already done in python...

    """ Add_to_front(self, val) and add_to_back(self, val) functions can be used which are already done in python class. Name of list is LList class node(object): def __init__(self, data, next=None): self.data = data self.next = next # Note: use the attributes directly; no setters or getters! class LList(object): def __init__(self): self._size = 0 self._head = None self._tail = None """ def set_data_at_index(self, idx, val): """ The value stored at index idx changes to val return True if the index was valid otherwise...

  • i need to create methods that: append_element(self, val) This method should increase the size of the...

    i need to create methods that: append_element(self, val) This method should increase the size of the list by one, adding the specified value in the new tail position. This is the only way to add a value as the tail. insert_element_at(self, val, index) If the provided index identifies a valid zero-based position within the list, then insert the specified value at that position, increasing the length by one. This method can be used to insert at the head of a...

  • Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display,...

    Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display, remove methods. You may use default arguments and/or helper functions. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list """ if self.head is...

  • Python 3: Write a LinkedList method named contains, that takes a value as a parameter and...

    Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...

  • class Node(object): def __init__(self, data, next=None): self.data = data self.next = next class List(object):    def...

    class Node(object): def __init__(self, data, next=None): self.data = data self.next = next class List(object):    def __init__(self): self.head = None self.tail = None Implement the following functions for Linked List in Python and use the constructors above : Copy(lList) Builds and returns a copy of list ItemAt(List,i) Returns the data item at position i in list Pop(List,i=0) Remove item at position i in list. If i is not specified, it removes the first item in list Count(List,x) Returns the number...

  • from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked...

    from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked list. Note that this is considered a "private class", one which is only meant to be used in this module by the LinkedList class, but not by client code. === Attributes === item: The data stored in this node. next: The next node in the list, or None if there are no more nodes. """ item: Any next: Optional[_Node] def __init__(self, item: Any) ->...

  • PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None   

    PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None        self.__size = 0    # Return the head element in the list    def getFirst(self):        if self.__size == 0:            return None        else:            return self.__head.element        # Return the last element in the list    def getLast(self):        if self.__size == 0:            return None        else:            return self.__tail.element    # Add an element to the beginning of the list    def addFirst(self, e):        newNode = Node(e) # Create a new node        newNode.next = self.__head # link...

  • PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything...

    PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything a course uses in a grading scheme, # like a test or an assignment. It has a score, which is assessed by # an instructor, and a maximum value, set by the instructor, and a weight, # which defines how much the item counts towards a final grade. def __init__(self, weight, scored=None, out_of=None): """ Purpose: Initialize the GradeItem object. Preconditions: :param weight: the weight...

  • Question 3 (24 points) (0) Given Red-Black RBBST Node, implement insertNode ( ) function class RBBST...

    Question 3 (24 points) (0) Given Red-Black RBBST Node, implement insertNode ( ) function class RBBST Node: class RBBST: def init_(self, val, color): self.val val self.left None self.right None self.color color def init_(self) : self.root None def init_rbbst(self, val, color): self.root def insert(self, val): if (self.root is None): self.init_rbbst (val, RED) else: RBBST_Node(val, color) RED True BLACK False self. insertNode (self. root, val) def insertNode(self, current, val): (ii) def rotate_left (self, current) : (ii) def flip_colors (self, current) :

  • Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of...

    Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of this assignment page on eClass). In this file, you have been given the complete code for a LinkedList class. Familiarize yourself with this class, noticing that it uses the Node class from Task 1 and is almost identical to the SLinkedList class given in the lectures. However, it also has a complete insert(pos, item) method (which should be similar to the insert method you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT