This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Page 1 6.189 –Homework ONLY Session 7 Administrivia Name: Instructions: 1. Err..complete the questions :). 2. When we ask for output, you DON'T have to write the spaces/newlines in. Program Text: print “X”, print “X”, Output: XX Day 4: More Loop Practice Problem 20: Each of the following function definitions takes a list as a parameter and solves a specific problem. Correctly fill the blanks in the following code to solve the problems. There is a way to solve each problem by only filling in the blanks. Don’t just add extra lines to force a solution. Also, there may even be more elegant solutions that don’t use all the blanks – feel free to use those too. Program Text: def swap_first_last(my_list): """This function swaps the first and last elements in a list. It has no return value.""" temp = _____________ _____________ = _____________ _____________ = _____________Page 2 Program Text: def second_biggest(my_list): """This function returns the second biggest element in my_list. It assumes that my_list contains distinct, positive integers.""" second_biggest = -5 biggest = -1 for i in my_list: if i > _____________: second_biggest = ______________ biggest = _____________________ elif i > second_biggest: second_biggest = ______________ return second_biggest Problem 13: You may recall the notion of a power series from Calculus. A power series is an infinite polynomial series that approximation a continuous function. For example, the power series of sin(x) is         3! 5! 7! The more terms you calculate, the closer your expression will be to sin(x) – hence the reason we call it an approximation. Write a function to calculate sin(x) using the above power series (well, fill in the blanks, at least.) Note: You've already seen the code for a function that can calculate the factorial of a number (Problem 11.) Assume the existance of a factorial(x) function that calculates the factorial of x.Page 3 Program Text: def calculate_sin(x, number_of_terms): "Calculates the value of sin(x) using the power series." number_of_terms = min(20, number_of_terms) #do at most 20 terms sin_value = 0 for i in range(number_of_terms): new_term = x ** _____________ new_term /= factorial(___________________) new_term *= (-1) ** ___________________ sin_value += new_term return


View Full Document

MIT 6 189 - Study Guide

Download Study Guide
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 Study Guide 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 Study Guide 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?