Unformatted text preview:

CS 302Elaine RichAlgorithms1. Consider the problem of making a peanut butter and jelly sandwich.a. Write an algorithm to accomplish the task.b. Mark the places where nontrivial knowledge is required and indicate what it is.2. In class, we showed the circuitry for binary addition. Of course, in real computers, we need to perform other arithmetic operations as well. Let’s considermultiplication. Notice the relationship between multiplication and addition:5 * 3 = 3 + 3 + 3 + 3 + 3Imagine that you have a machine with an addition circuit but you now want to do multiplication. You can do it in software. Fill in the required code in the following Python function. (Hint: actually type this up and run it. That way you can be sure you submit a correct answer.)def mult(n, m): """ Multiplies n by m, using only addition. Precondition: n and m are positive integers. """ # code to compute n * m and make it the value of the variable ans return(ans)3. Consider the following chunk of code that we used as an example of the if/elif/else construct:if x < 10:print("small")elif x > 100:print("big")else:print("so-so")This code won’t work in all situations. What must be true before it starts in order for it not to produce an error? In other words, what is its precondition? (Hint: Run it on various inputs. Think about things it isn’t meant for.)4. Recall this program that we looked at when we were learning Python. def vowels(st): new = "" for i in range(len(st)): if st[i]=="a" or st[i]=="e" or st[i]=="i" orst[i]=="o" or st[i]=="u" or st[i]=="y": new = new + "*" else: new = new + st[i] return(new)a. Describe in English what it does. b. Use big-O notation to describe how many steps it executes as a function of |st| (the length of the input string st). 5. What is the sum of the integers from 1 to 9876?6. The prime factorization of a positive integer n is the bag (think of a bag as a set but duplicates are allowed) of positive integers with two properties:- The product of the integers in the bag is n.- All the integers in the bag are prime. (1 is not a prime number, so cannot be in the bag.)So, for example, the prime factorization of 612 is {2, 2, 3, 3, 17}. The prime factorization of 17 is {17}. What is the prime factorization of 714? 7. Recall the code we wrote to implement Euclid’s method for determining the greatest common divisor (gcd) of two integers:def gcd_euclid(n,m):if m == 0: return(n) else: return gcd_euclid(m, n%m)Compute gcd(87, 6). Use gcd_euclid and show each of the calls to it starting with gcd_euclid(87,6).8. In class, I presented the function newton that computes square root using Newton’s method:def newton(n): guess = 1 while abs(guess**2 - n) > .00000001: guess = (guess + n/guess)/2 return(guess)Use it to find sqrt(1287658).9. Suppose that you are given the following sorted list of numbers:3, 7, 21, 26, 35, 41, 43, 45, 54, 58, 62, 67, 71, 77, 80, 82, 91, 95a. Show the order in which you will check the elements of this list if you are using sequential search to look for 80.b. Show the order in which you will check the elements of this list if you are using binary search to look for 80.c. Give an example of a number that you would find faster if you used sequentialsearch.10. Show the steps that bubble sort will perform if given the input list [21, 2, 4, 98, 5].11. Give two examples of everyday heuristics that you use.12. Facebook uses a heuristic search algorithm to find the things to show on your home page. Just from watching what you get, can you think of factors that contribute to the objective function that it uses? (If you’ve never gotten on Facebook, as some friends to help you on this.)13. Take your slider puzzle. Randomly make at least 20 moves so that you have the tiles mixed up. a. Show the puzzle at this point. You can take a picture of it or sketch it.b. Now solve it. How many moves did it take you to do so?14. In class I presented the following program for computing the 3n+1 function:def threen(value): # compute 3n+1 while value != 1: if value % 2 == 0: value = value//2 else: value = 3 * value + 1 print(int(value))Run it on 87. How many steps did it take to converge? 15. Consider the following nim game:Assume that you are to play next. Show at least one move that will guarantee a win for you (assuming you don’t blow it later). Answer: Take 2 from the rightmost pile.16. Consider the following graph. Does it contain an Eulerian circuit? Justify your answer.17. Consider the following instance of the Post Correspondence problem. Does it have a solution? If so, show one. i X Y1ab a2ab ba3aa baa18. Can the following tile set tile an arbitrary surface on the plane (following the rulesdescribed in class)? 1 2 319. In class I presented my simple, not very robust version of the average program:def average(numbers): sum = 0 for num in numbers: sum = sum + num return(sum/len(numbers))Type it in to IDLE. Play with it. Give an example of an input that caused an


View Full Document

UT CS 302 - Algorithms

Download Algorithms
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Algorithms and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Algorithms 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?