Question

In: Computer Science

Given an unordered list (defined using the UnorderedList class above), transform it into a Python list.When...

Given an unordered list (defined using the UnorderedList class above), transform it into a Python list.When the input list is empty, the output Python list should be empty as well.Example: When the input unordered list is [1,2,3], the output Python list should be [1,2,3] .

Use the following codes:

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

def getData(self):
return self.data

def getNext(self):
return self.next

def setData(self, data):
self.data = data

def setNext(self, node):
self.next = node

class UnorderedList:
def __init__(self):
self.head = None

def add(self, data):
newNode = Node(data)
newNode.setNext(self.head)
self.head = newNode

def length(self):
cur = self.head
count = 0
while cur:
cur = cur.getNext()
count += 1
return count

def isEmpty(self):
return self.head == None

def search(self, data):
cur = self.head
while cur:
if cur.getData() == data:
return True
cur = cur.getNext()
return False

def remove(self, data):
if self.head.getData() == data:
self.head = self.head.getNext()
else:
cur = self.head
while cur.getNext() and cur.getNext().getData() != data:
cur = cur.getNext()
if cur.getNext():
cur.setNext(cur.getNext().getNext())

--------------------------------------------------------------------------------

"""
2. transform(lst)
Transform an unordered list into a Python list
Input: an (possibly empty) unordered list
Output: a Python list
"""
def transform(lst):
# Write your code here

Solutions

Expert Solution

Program

Text Format:

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

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self, data):
        self.data = data

    def setNext(self, node):
        self.next = node


class UnorderedList:
    def __init__(self):
        self.head = None

    def add(self, data):
        newNode = Node(data)
        newNode.setNext(self.head)
        self.head = newNode

    def length(self):
        cur = self.head
        count = 0
        while cur:
            cur = cur.getNext()
            count += 1
        return count

    def isEmpty(self):
        return self.head == None

    def search(self, data):
        cur = self.head
        while cur:
            if cur.getData() == data:
                return True
            cur = cur.getNext()
        return False

    def remove(self, data):
        if self.head.getData() == data:
            self.head = self.head.getNext()
        else:
            cur = self.head
            while cur.getNext() and cur.getNext().getData() != data:
                cur = cur.getNext()
                if cur.getNext():
                    cur.setNext(cur.getNext().getNext())


# --------------------------------------------------------------------------------

"""
2. transform(lst)
Transform an unordered list into a Python list
Input: an (possibly empty) unordered list
Output: a Python list
"""


def transform(lst):
    olist = []
    node = lst.head
    for i in range(lst.length()):
        tm = node.getData()
        olist.append(tm)
        node = node.getNext()
    return olist


lst = UnorderedList()
lst.add(7)
lst.add(5)
lst.add(3)
olist = transform(lst)
print(olist)


Output:

[3, 5, 7]

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.

Thank you.


Related Solutions

Given: You are given a Python Class template. In this class there is a class variable...
Given: You are given a Python Class template. In this class there is a class variable vector, is a list of N non-negative integers and are stored (in positions 0, 1, 2, ... (N-1)), where at least one integer is 0. Task: Write a recursive function "findAllPaths" to find all possible path through V starting at position 0, and ending at the location of 0, in accordance with the Rule below. If no such path exists, "paths" should be an...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
In python- Create a class defined for Regression. Class attributes are data points for x, y,...
In python- Create a class defined for Regression. Class attributes are data points for x, y, the slope and the intercept for the regression line. Define an instance method to find the regression line parameters (slope and intercept). Plot all data points on the graph. Plot the regression line on the same plot.
The Pool class of the multiprocessing Python module. There is a defined run method to perform...
The Pool class of the multiprocessing Python module. There is a defined run method to perform the tasks. Create a pool object of the Pool class of a specific number of CPUs your system has by passing a number of tasks you have. Start each task within the pool object by calling the map instance method, and pass the run function and the list of tasks as an argument.Hint: os.walk() generates the file names in a directory tree by walking...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
concatenate(lst1, lst2 ) Given two (possibly empty) unordered lists, concatenate them such that the first list...
concatenate(lst1, lst2 ) Given two (possibly empty) unordered lists, concatenate them such that the first list comes first.Example: When the first input unordered list lst1 is [1, 2, 3] and the second lst2 is [7,8,9], the outputunordered list is [1,2,3,7,8,9]. Use codes: class Node: def __init__(self, data): self.data = data self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self, data): self.data = data def setNext(self, node): self.next = node class UnorderedList: def __init__(self): self.head = None...
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the...
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the queue class create enqueue, dequeue, first, empty, len and resize methods. The class should support circular queue and have the ability to resize the queue.
Python Create a move function that is only defined in the base class called Objects. The...
Python Create a move function that is only defined in the base class called Objects. The move function will take two parameters x,y and will also return the updated x,y parameters.
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
Using the class Date that you defined in Exercise O3, write the definition of the class...
Using the class Date that you defined in Exercise O3, write the definition of the class named Employee with the following private instance variables: first name               -           class string last name                -           class string ID number             -           integer (the default ID number is 999999) birth day                -           class Date date hired               -           class Date base pay                 -           double precision (the default base pay is $0.00) The default constructor initializes the first name to “john”, the last name to “Doe”, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT