Linked list insert element problem

Assignment Help Basic Computer Science
Reference no: EM131984013

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: EM131984013

Questions Cloud

Estimate the firms cost of common equity using capm : Calculate the firm's weighted average Cost of Capital. Use the CAPM cost of equity, your cost of debt capital, and the market value capital structure.
Receive and send rtp packets to b : 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
What will be in the account at the end of four years : What will be in the account at the end of four years if the interest rate is 12% compounded monthly?
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.
Determine the number of possible ror values and why : Given reinvestment rate of 15% per year for excess funds and 11 % per year for borrowing rate for extra funds, with 13 % MARR.
What is the purpose and mission of the who : What is the purpose and mission of the WHO? What are some of the areas of health and health care that they advocate for and advance?
Characteristic polynomial and its roots : Solve the recurrence equation An = An-1+4An-2 +2An-3, for A0 = 1, A1 = 1. A2 = 5 for n >= 3 Follow the steps below.
Designing and managing a database : Determine whether first normal form (1NF), second normal form (2NF), or third normal form (3NF) should be the goal when a DBA designs a database.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Accounts created in an operating system

Why is it important to have user and group accounts created in an operating system? List some advantages to both instances, providing examples for each?

  Outsourcing to difference vendors to handle it-is activities

Discuss the pros and cons of selecting (outsourcing to) three different vendors to handle three different IT/IS activities.

  What are the most important parts of this dashboard

Do not repeat examples from the textbook or that have been posted by other students. Please respond to the following:

  Find out the running time of program

What is the running time of your program? If M = 1, what is the running time of your program? How is the actual speed affected by the delete routine for large values  of N (N > 100,000)?

  Company: five guys burgers and fries

Identify the most important strengths and weaknesses of your organization including an assessment of the organization's resources.

  What bad things can happen if the message numbers do repeat

Modify the algorithms for the secure channel in this chapter to use the encrypt-then-authenticate order for encryption and authentication.

  Comp. literacy quiz

Need answers in 6 hours, no excuses please. 90% will earn 5 star rating.

  Price of two-bedroom condominiums

When the price of two-bedroom condominiums was $200,000 there were 10,000 units sold. Several years later when the price was $325,000

  Is it reasonable to believe the ads may be effective

Polls taken before and after the ad campaign show some increase in the proportion of voters who now recognize this candidate's name, with a P-value of 0.033. Is it reasonable to believe the ads may be effective?

  Measure the difference in space usage

Also measure the difference in space usage. Use a preprocessor conditional to disable range checking on access.

  Are your contrasts in part (a) orthogonal

Design contrasts to compare chewy with crispy, and expensive with inexpensive.

  Draw an er diagram for the database

Given the above description, draw an ER diagram for the database. State any assumptions you make for your ER-diagram to support your design. Include the following into your design:

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