0% found this document useful (0 votes)
31 views5 pages

Psrec

The document describes 7 problems involving recursive and iterative algorithms on lists and strings. Problem 1 asks to find two contiguous integers of different signs in a list. Problem 2 finds the closest element to a target number in a sorted list. Problem 3 defines a recursive function and its memoized and iterative versions. Problem 4 reverses a list recursively without built-in functions. Problem 5 finds the index of the first mismatch between two lists. Problem 6 counts the even integers in a list where evens precede odds. Problem 7 checks if a string is a palindrome recursively.

Uploaded by

Michel Kordahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Psrec

The document describes 7 problems involving recursive and iterative algorithms on lists and strings. Problem 1 asks to find two contiguous integers of different signs in a list. Problem 2 finds the closest element to a target number in a sorted list. Problem 3 defines a recursive function and its memoized and iterative versions. Problem 4 reverses a list recursively without built-in functions. Problem 5 finds the index of the first mismatch between two lists. Problem 6 counts the even integers in a list where evens precede odds. Problem 7 checks if a string is a palindrome recursively.

Uploaded by

Michel Kordahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Problem 1

We are given a list A of integers of such that:

i) len(A)≥2

ii) The first and last elements of A have different signs

iii) All the elements of A are non-zero

Given A, we would like to find any two contiguous integers x and y in A of different signs.
Note that the existence x and y is guaranteed by (i), (ii), and (iii). (The argument is by contradiction: If
all contiguous integers in A are of the same signs, then the first and second are of the same sign, and the
second and the third are of the same sign, etc. Thus, the first and last elements are of the same signs, which
contradicts (ii)).
Write a function signChange(A), which given a list of integers satisfying (i), (ii), and (iii), returns a tuple
(x,y), where x and y are contiguous integers A of different signs (see the below test cases).
You are not asked to check if Conditions (i), (ii), and (iii) are satisfied; assume that they hold. If there are
more than one pairs of contiguous integers of different signs (e.g., last test case below), any one of them is
a valid answer.
Faster algorithms are worth more point. Any correct solution is worth 20 point. To get full grade, do it
in O(logn) time.
(Hint: Recursion: implement signChange(A) as a wrapper function containing a recursive function).
Test Program:
print(signChange([ 2, -1]))
print(signChange([2, 3, -1]))
print(signChange([2, 3, -5, 2,-1]))
print(signChange([-2,-3, -5, 2,10]))
print(signChange([-2,-3,-5, -20, 2,-25,10]))

Output:
(2, -1)
(3, -1)
(3, -5)
(-5, 2)
(-20, 2)

1
Problem 2
Implement the function findClosest(L,x), which given a list of numbers L sorted in nondecreasing
order, and a number x, finds the element e in L which is closest to x. That is, the desired e is an element
of L such that |e − x| ≤ |e0 − x| for all elements e0 of L. If the list is empty, your function should raise an
assertion with an error message “Empty list!”.
For example, for x = 7 and L = [2,4,8,10], findClosest([2,4,8,10],7)=8 because: |2 − 7| = 5; |4 − 7| = 3;
|8 − 7| = 1; and |10 − 7| = 3. Hence, 8 is the closest element of L to 7.

Test program/output:

print(findClosest([1,5,7],4))

print(findClosest([3,9,10,11],10))

print(findClosest([3,8,14],11)) # Both 8 and 14 are valid answers

print(findClosest([-2,7,10,18],2))

print(findClosest([-2,7,10,18],20))

print(findClosest([-2,7,10,18],-3))

Output:

5 10 14 -2 18 -2

Problem 3

2
Problem 4

b) Memoized. The recursive function in Part (a) makes many unnecessary calls to solve repeated
problems. Implement a memoized recursive function fMemoized(n), which given n, returns fn. Follow the
approach we did in class to memoized Fibonacci.

c) Iterative. Now, implement a non-recursive function fIterative(n), which given n, returns fn. Use a list in
fIterative. Try it on the above test cases.

Example:

print("f(n) for n = 0...9:")

for i in range(10):

print(f(i), end=" ")


Ouput:

f(n) for n = 0...9:

1 1 1 4 7 13 25 46 85 160

Problem 5

Implement the function reverseList(L), which given a list L, modifies L by reversing the order of its
elements. The use of for loops, while loops, slicing operator, list.reverse method, list.copy method, and
copy module is strictly forbidden in this problem. You are asked to do it recursively. Namely, implement
reverseList as wrapper function which calls a recursive function reverseListRecursive. Any correct
solution is worth full grade.

L1 = [] L: []

L2 = [1] L reversed: []

L3 = [1, 2] L: [1]

L4 = [10,5,3,2,1] L reversed: [1]

for L in (L1,L2,L3,L4): L: [1, 2]

print("L:",L) L reversed: [2, 1]

reverseList(L) L: [10, 5, 3, 2, 1]

print("L reversed:",L) L reversed: [1, 2, 3, 5, 10]

3
Problem 6

Test program/output:

print(firstMismatch([1,7,4,13,2],[1,7,4,13,1]))

print(firstMismatch([1,7,4,13,2],[1,7,4,30,1]))

print(firstMismatch([1,7,4,13,2],[1,7,14,30,1]))

print(firstMismatch([1,7,4,13,2],[1,70,14,30,1]))

print(firstMismatch([20,7,4,13,2],[1,70,14,30,1]))

print(firstMismatch([20],[1]))

Problem 7

We are given a list L consisting of even integers (possibly none) followed by odd integers. Given L, we
would like to find the number of even integers. That is, write a function countEven(L), which given a list L
of integers satisfying the condition that all even integers in L appears before all odd integers in L, returns
the the number of even integers in L. You are not asked to check if the list L satisfies the condition that all
even integers in L appears before all odd integers in L; assume that this condition holds. Moreover, you
function is not expected to work properly if this condition is not satisfied by L. Faster algorithms are
worth more point. Any correct solution is worth 10 points. To get full grade, do it in O(log n) time.

Test program/Output:
L1 = [2,4,6,2,7,9,13,15,1,1,19]
L2 = [2,4,6,2,4,8,12,10,4,1,19]
L3 = [2,4,6,2,4,8,12,10,4]
L4 = [7,9,13,15,1,1,19]
L5 = []
L6 = [2]
L7 = [1]

4
L8 = [2,1]
for L in [L1,L2,L3,L4,L5,L6,L7,L8]:
print(countEven(L))
Output:
49900101
Problem 8
Check if a string is a palindrome recursively Output:
print(isPalindrome("anna")) True
print(isPalindrome("civic")) True
print(isPalindrome("a")) True
print(isPalindrome("tx1aa1xt")) True
print(isPalindrome("")) True
print(isPalindrome("Civic")) False
print(isPalindrome("ab")) False

You might also like