logo Hurry, Grab up to 30% discount on the entire course
Order Now logo
660 Times Downloaded

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Bryce KimEnglish
(5/5)

719 Answers

Hire Me
expert
Ray BjorkCriminology
(5/5)

757 Answers

Hire Me
expert
Jesse RossResume writing
(5/5)

827 Answers

Hire Me
expert
Patriciaaa GloverrrScience
(5/5)

809 Answers

Hire Me
Python Programming
(5/5)

Returns the value of bn for any numeric b and any integer n including negative integers. Do not use any loops,

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

1. Within a Python file named recursion.py, write the following functions:

(a) (4 points) power(b, n)

Returns the value of bn for any numeric b and any integer n (including negative integers). Do not use any loops, and do not use any built-in ways of computing powers. This function must be recursive.

(b) (4 points)  count  positives(stuff)

Returns how many positive integers (i.e., > 0) are contained in stuff. You may assume that stuff is a list of integers. Do not use any loops, and do not use any built-in ways of searching a list. This function must be recursive.

(c) (4 points) all even(stuff)

Returns whether or not stuff contains only even integers. If stuff is empty, this should return True. You may assume that stuff is a list of integers. Do not  use  any loops,  and do not use any built-in ways of searching a list. This function must be recursive.

(d) (6 points) binary search(stuff,  target)

Returns the index of target within the  list  stuff if  it  exists,  or  -1  if  it  doesn’t  exist. You may assume that stuff is a sorted list of integers, and target is  an integer.  Do  not use any loops, and do not use any built-in ways of searching a list. This must be done recursively; you can write a recursive helper function if needed.

(e) (6 points) After your function definitions in recursion.py, write some tests for your func- tions. Be sure to test at least the following scenarios:

power - Negative, zero, and positive exponents

count positives - List contains all positive integers, list contains all non-positive integers, list contains a mix of positive/non-positive integers, list is empty

 

1

 

all even - List contains all even integers, list contains all odd integers, list contains a mix of even/odd integers, list is empty

binary search - Target is in the list, target is too low for the list, target is too high for the list, target is within the range of the list but not present, list is empty

2. As discussed in class, the quicksort algorithm starts by partitioning the list: a pivot value is selected, and the list is partitioned around that pivot. For this problem we’ll assume that

The pivot is always the element at index 0 of the original list (i.e., before any rearranging of elements is performed).

After partitioning, all the elements to the left of the pivot are the pivot, and all the elements to the right of the pivot are > the pivot.

In the following parts, assume that stuff is a list of integers.

(a) (7 points) Consider this naive algorithm for partitioning a list stuff:

1. Create two new low and high lists. These will be used to store the elements of stuff

that are ≤ or > the pivot, respectively.

2. Loop through all elements of stuff besides the pivot.  If the element is  the pivot, copy it into low. If the element is > the pivot, copy it into high.

3. Copy the elements of low back into stuff, then the pivot back into stuff, and finally the elements of high back into stuff.

Within a Python file named partitioning.py, write a function partition naive(stuff) that implements the above algorithm. The function should return the final index of the pivot, after partitioning is complete. Note that partition naive doesn’t need to return the partitioned list because the actions that it performs will affect the original list argu- ment. Make sure that your function works for a list of any length ≥ 1.

(b) (7 points) The partitioning algorithm from the previous part is not very efficient. Creating the two lists low and high requires extra time as well as memory. A better way is to perform the partition in-place, which means that no new lists are created. Instead, we just modify the elements of the original list directly.

Suppose we have a list a containing the elements [10, 5, 16, 14,  2,  10,  13].  As before, we want to use the first element (10) as the pivot. Here’s an algorithm to perform an in-place partition:

1. Create two indices, L and U. Start L from the beginning of the list; start U from the end.

 

 

(5/5)
Attachments:

Expert's Answer

660 Times Downloaded

Related Questions

. The fundamental operations of create, read, update, and delete (CRUD) in either Python or Java

CS 340 Milestone One Guidelines and Rubric  Overview: For this assignment, you will implement the fundamental operations of create, read, update,

. Develop a program to emulate a purchase transaction at a retail store. This  program will have two classes, a LineItem class and a Transaction class

Retail Transaction Programming Project  Project Requirements:  Develop a program to emulate a purchase transaction at a retail store. This

. The following program contains five errors. Identify the errors and fix them

7COM1028   Secure Systems Programming   Referral Coursework: Secure

. Accepts the following from a user: Item Name Item Quantity Item Price Allows the user to create a file to store the sales receipt contents

Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip

. The final project will encompass developing a web service using a software stack and implementing an industry-standard interface. Regardless of whether you choose to pursue application development goals as a pure developer or as a software engineer

CS 340 Final Project Guidelines and Rubric  Overview The final project will encompass developing a web service using a software stack and impleme