Linked list insert element problem

Assignment Help Basic Computer Science
Reference no: EM131992014

Python 3 Linked List Insert Element Problem

Implement a method, insert_first which takes a linked list as a parameter and elem, and returns a new list, where elem was inserted as a first node. 

def insert_first(s, elem):

  '''

  >>> insert_first(link(3, link(2, link(1, "empty"))),100)

  [100, [3, [2, [1, 'empty']]]]

  '''

Implement a method, insert_last which takes a linked list as a parameter and elem, and returns a new list, where elem was inserted as a last node. 

def insert_last(s, elem):

  '''

  >>> lst = link(2, link(1, empty))

  >>> insert_last(lst,100)

  [2, [1, [100, 'empty']]]

  '''

Implement insert_at which takes a linked-list, an element elem and an insertion index idx and returns the list with the element inserted at the correct location.

def insert_at(s, elem, idx):

  '''

  >>> lst = link(3, link(2, link(1, empty)))

  >>> insert_at(lst, 4, 0)

  [4, [3, [2, [1, 'empty']]]]

  >>> insert_at(lst, 4, 2)

  [3, [2, [4, [1, 'empty']]]]

  '''

IMPORTANT Information:

empty = 'empty'

def is_link(s):

  """s is a linked list if it is empty or a (first, rest) pair."""

  return s == empty or (len(s) == 2 and is_link(s[1]))

def link(first, rest):

  """Construct a linked list from its first element and the rest."""

  assert is_link(rest), "rest must be a linked list."

  return [first, rest]

def first(s):

  """Return the first element of a linked list s."""

  assert is_link(s), "first only applies to linked lists."

  assert s != empty, "empty linked list has no first element."

  return s[0]

def rest(s):

  """Return the rest of the elements of a linked list s."""

  assert is_link(s), "rest only applies to linked lists."

  assert s != empty, "empty linked list has no rest."

  return s[1]

# define length

def len_link(s):

  """Return the length of linked list s."""

  length = 0

  while s != empty:

    s, length = rest(s), length + 1

  return length

# getitem

def getitem(s, i):

  """Return the element at index i of linked list s."""

  while i > 0:

    s, i = rest(s), i - 1

  return first(s)

Reference no: EM131992014

Questions Cloud

Find an article about the unethical behavior of an auditor : Using the Argosy University online library resources and the Internet, find an article about the unethical behavior of an auditor and its impact on the company.
Minimum number of udp sockets needed : What would be the minimum number of UDP sockets needed for "A" to receive and send RTP packets to B, C, and D. Briefly explain your answer.
Windows editions in terms of startup problems : Why is Windows 10 considered to be a more stable system than previous Windows editions in terms of startup problems.
Discuss how you may have used tvm in a recent investment : Discuss how you may have used TVM in a recent investment or loan decision and explain the TVM involved in your transaction.
Linked list insert element problem : Implement a method, insert_first which takes a linked list as a parameter and elem, and returns a new list, where elem was inserted as a first node.
Discuss different types of compensation plans they might use : If the overall stock market is sometimes volatile, Discuss the different types of compensation plans they might use.
How an activity-based costing system might benefit : Explain how Borealis Manufacturing could change its overhead application system to eliminate confusion over product costs. Describe how an activity-based.
Writting pseudocode for a collection manager program : The program that will help you manage a collection of recipes.
How saud bahwan group manage their corporate sustainability : Contemorary Management Issues - Demonstrate a systematic understanding of modern developments within business management (Knowledge & Understanding)

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Identifies the cost of computer

identifies the cost of computer components to configure a computer system (including all peripheral devices where needed) for use in one of the following four situations:

  Input devices

Compare how the gestures data is generated and represented for interpretation in each of the following input devices. In your comparison, consider the data formats (radio waves, electrical signal, sound, etc.), device drivers, operating systems suppo..

  Cores on computer systems

Assignment : Cores on Computer Systems:  Differentiate between multiprocessor systems and many-core systems in terms of power efficiency, cost benefit analysis, instructions processing efficiency, and packaging form factors.

  Prepare an annual budget in an excel spreadsheet

Prepare working solutions in Excel that will manage the annual budget

  Write a research paper in relation to a software design

Research paper in relation to a Software Design related topic

  Describe the forest, domain, ou, and trust configuration

Describe the forest, domain, OU, and trust configuration for Bluesky. Include a chart or diagram of the current configuration. Currently Bluesky has a single domain and default OU structure.

  Construct a truth table for the boolean expression

Construct a truth table for the Boolean expressions ABC + A'B'C' ABC + AB'C' + A'B'C' A(BC' + B'C)

  Evaluate the cost of materials

Evaluate the cost of materials

  The marie simulator

Depending on how comfortable you are with using the MARIE simulator after reading

  What is the main advantage of using master pages

What is the main advantage of using master pages. Explain the purpose and advantage of using styles.

  Describe the three fundamental models of distributed systems

Explain the two approaches to packet delivery by the network layer in Distributed Systems. Describe the three fundamental models of Distributed Systems

  Distinguish between caching and buffering

Distinguish between caching and buffering The failure model defines the ways in which failure may occur in order to provide an understanding of the effects of failure. Give one type of failure with a brief description of the failure

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd