Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


ECS 32B: Summer 2022

Homework Assignment 3


Practice problem A. The sum ofthe first n terms ofthe Kleinfeldt sequence is defined as 1 + 1/4 + 1/9 + 1/16 + 1/25 + ... + 1/n2

We can rewrite this definition recursively, so that the sum ofthe first n terms ofthe Kleinfeldt sequence can be defined as:

 

=  1/n2    +  kleinfeldt   (n  -  1)  if  n  >  1

Using Python and the information provided above, construct the function kleinfeldt that      takes one argument, an integer n that is greater than 0, and returns the sum ofthe first n terms of the Kleinfeldt sequence as a real number.   Your solution must be a recursive solution.  Here are some examples:

>>>  kleinfeldt(1)

1

>>>  kleinfeldt(2)

1.25

>>>  kleinfeldt(100)

1.6349839001848923

Practice problem B. Assume you are climbing a ladder.  The ladder has n rungs.  You can only climb one or two rungs at a time.  How many different ways can you climb to the top?  For         example, if there are three rungs on the ladder, there are three different ways to climb to the top:

1 rung + 1 rung + 1 rung

2 rungs + 1 rung

Use Python and recursion to write a function called ladder that calculates the number of different ways you can get to the top.  Here are some examples:

>>>  ladder(3)

3

>>>  ladder(5)

8

>>>  ladder(10)

89

Your graded problems begin here.

1. (20 points) Write a recursive Python function that expects one argument, a Python list of           integers, and  returns the largest integer in the list. Thinking recursively, the largest integer is        either the first  integer in the list or the largest integer in the rest of the list, whichever is larger. If

the list has only one integer, then the largest integer is this single value.  You may assume that the list has at least one element.  Here are some examples:

>>>  findLargest([1,  7,  35,  12,  19,  106,  0])

106

>>>  findLargest([42])

42

(Helpful Python syntax: IfA is a list of integers, and you want to set the list B to all ofthe

 

For problems 2 and 3, assume that you have constructed a linked list using the Node() class from your textbook.  Here is one such example:

>>>  n1  =  Node(10)

 

2. (20 points) Use Python and recursion to write a function called findValue that determines    whether a given data value is in the linked list.  The function returns True ifthe value is present, and False otherwise.  Here are some examples:

>>>  findValue(10,n4)

True

>>>  findValue(40,n4)

True

>>>  findValue(50,n4)

False

>>>

Note that your function must work on lists other than the example above.  The list will not necessarily be sorted. You may assume that the list has at least one element.

Submit your solution via Canvas in one file named “hw3p2.py”.


3. (20 points) Use Python and recursion to write a function called findLastValue that returns the last value in a linked list.  Here is an example:

>>>  findLastValue(n4)

10

Note that your function must work on lists other than the example above.  You may assume that the list has at least one element.

Submit your solution via Canvas in one file named “hw3p3.py”.

4.  (20 points) In the PowerPoint slides for lecture 14, you will find a function that performs        binary search on a Python list using recursion.  Modify that binarySearchRec (alist,       item) function so that it performs ternary search on a Python list.  Your new function should     find two midpoints that divide the list into three (hence the name “ternary” …look it up)               approximately equal size segments.  Ifthe item being searched for is found at either ofthe           midpoints, the function should return True.  Ifthe item being searched for is less than the value   at the first midpoint, ternary search continues with the segment to the “left” of the first midpoint. Ifthe item being searched for is greater than the value at the second midpoint, ternary search       continues with the segment to the “right” of the second midpoint.  Otherwise, ternary search        continues with the segment between the first and second midpoints. Name your function             ternarySearchRec.

Submit your solution via Canvas in one file named “hw3p4.py”.