Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

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)

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92829665
  • Price:- $10

Priced at Now at $10, Verified Solution

Have any Question?


Related Questions in Computer Engineering

Answer the following question whats the difference between

Answer the following Question : What's the difference between public, protected, and private members? What's the difference between static binding and dynamic binding?

Assignment - review of article where physical security

Assignment - Review of Article Where Physical Security Failed • Search the Internet for an article where physical security failed • Propose a possible change in that organization's physical security that could have preve ...

Write a matlab code to solve thisit seems that the math

Write a matlab code to solve this It seems that the Math department at a rival university has once again dropped the ball, and forgotten the value of n. You are to write a script which consumes a number that specifies th ...

Answer the following question 1 suppose a program has a

Answer the following Question : 1. Suppose a program has a button with the caption "Quit." Suppose also that the Name property of this button is btnQuit. Write a btnQuit_Click event procedure that gives the user a second ...

On a certain banking machine customers arrive at an average

On a certain banking machine customers arrive at an average of 15 per hour. a) What is the probability that 12 customers will use the machine in the next hour? b) What is the probability that there will be fewer than 3 c ...

Listen to or read the transcript of this podcast

Listen to (or read the transcript of) this podcast (https://www.stlouisfed.org/education/economic-lowdown-podcast-series/episode-16-elasticity-of-demand) from the Federal Reserve Bank of St. Louis. describe your experien ...

Please explain which formula i should use to complete the

Please explain which formula I should use to complete the following probability question: The probability that a family will buy a vacation home in Miami, malibu, or newport is 0.25, 0.10 and 0.35. What is the probabilit ...

I really need help writing this c progam i have seen many

I really need help writing this C++ progam. I have seen many answers to this question on this site, but they are all deeply flawed. Design the following classes in C++. Person class is the base class for other classes. P ...

How does westpac manage foreign exchange risk how does it

How does Westpac manage foreign exchange risk? How does it differ to the other 4 big banks? (ANZ, CBA and NAB)

You were recently hired as a database administrator for

You were recently hired as a database administrator for CSU-Global Campus. During grade reporting, CSG-Global cannot afford to have any downtime. You have been asked to develop a plan for monitoring SQL Server databases. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As