SlideShare ist ein Scribd-Unternehmen logo
1 von 3
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 sin_value

Weitere ähnliche Inhalte

Was ist angesagt?

Python iteration
Python iterationPython iteration
Python iteration
dietbuddha
 
Sulpcegu5e ppt 2_1
Sulpcegu5e ppt 2_1Sulpcegu5e ppt 2_1
Sulpcegu5e ppt 2_1
silvia
 
53 inverse function (optional)
53 inverse function (optional)53 inverse function (optional)
53 inverse function (optional)
math126
 
1.4 Functions
1.4 Functions1.4 Functions
1.4 Functions
nicksuf
 
Composite functions
Composite functionsComposite functions
Composite functions
toni dimella
 
Polynomial Functions
Polynomial FunctionsPolynomial Functions
Polynomial Functions
nicole379865
 
Piecewise Functions
Piecewise FunctionsPiecewise Functions
Piecewise Functions
swartzje
 

Was ist angesagt? (20)

Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Types of functions
Types of functionsTypes of functions
Types of functions
 
Python iteration
Python iterationPython iteration
Python iteration
 
3.4 Polynomial Functions and Their Graphs
3.4 Polynomial Functions and Their Graphs3.4 Polynomial Functions and Their Graphs
3.4 Polynomial Functions and Their Graphs
 
Composite functions
Composite functionsComposite functions
Composite functions
 
Function and Its Types.
Function and Its Types.Function and Its Types.
Function and Its Types.
 
C語言基本資料型別與變數
C語言基本資料型別與變數C語言基本資料型別與變數
C語言基本資料型別與變數
 
Function Math 8
Function Math 8Function Math 8
Function Math 8
 
Recursion
RecursionRecursion
Recursion
 
Calc 5.3
Calc 5.3Calc 5.3
Calc 5.3
 
Sulpcegu5e ppt 2_1
Sulpcegu5e ppt 2_1Sulpcegu5e ppt 2_1
Sulpcegu5e ppt 2_1
 
Day03
Day03Day03
Day03
 
53 inverse function (optional)
53 inverse function (optional)53 inverse function (optional)
53 inverse function (optional)
 
1.4 Functions
1.4 Functions1.4 Functions
1.4 Functions
 
Composite functions
Composite functionsComposite functions
Composite functions
 
Polynomial Functions
Polynomial FunctionsPolynomial Functions
Polynomial Functions
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in Matlab
 
Piecewise Functions
Piecewise FunctionsPiecewise Functions
Piecewise Functions
 

Ähnlich wie Notes7

Lesson7 incdecoperators
Lesson7 incdecoperatorsLesson7 incdecoperators
Lesson7 incdecoperators
heartplusbrain
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
Devnology
 

Ähnlich wie Notes7 (20)

Matlab functions
Matlab functionsMatlab functions
Matlab functions
 
Python advance
Python advancePython advance
Python advance
 
Programming with effects - Graham Hutton
Programming with effects - Graham HuttonProgramming with effects - Graham Hutton
Programming with effects - Graham Hutton
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 
Lesson7 incdecoperators
Lesson7 incdecoperatorsLesson7 incdecoperators
Lesson7 incdecoperators
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning material
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
 
DataWeave 2.0 - MuleSoft CONNECT 2019
DataWeave 2.0 - MuleSoft CONNECT 2019DataWeave 2.0 - MuleSoft CONNECT 2019
DataWeave 2.0 - MuleSoft CONNECT 2019
 
A01
A01A01
A01
 
Create and analyse programs
Create and analyse programsCreate and analyse programs
Create and analyse programs
 
Tutorial2
Tutorial2Tutorial2
Tutorial2
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
MatlabIntro.ppt
MatlabIntro.pptMatlabIntro.ppt
MatlabIntro.ppt
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 

Mehr von Amba Research (20)

Case Econ08 Ppt 16
Case Econ08 Ppt 16Case Econ08 Ppt 16
Case Econ08 Ppt 16
 
Case Econ08 Ppt 08
Case Econ08 Ppt 08Case Econ08 Ppt 08
Case Econ08 Ppt 08
 
Case Econ08 Ppt 11
Case Econ08 Ppt 11Case Econ08 Ppt 11
Case Econ08 Ppt 11
 
Case Econ08 Ppt 14
Case Econ08 Ppt 14Case Econ08 Ppt 14
Case Econ08 Ppt 14
 
Case Econ08 Ppt 12
Case Econ08 Ppt 12Case Econ08 Ppt 12
Case Econ08 Ppt 12
 
Case Econ08 Ppt 10
Case Econ08 Ppt 10Case Econ08 Ppt 10
Case Econ08 Ppt 10
 
Case Econ08 Ppt 15
Case Econ08 Ppt 15Case Econ08 Ppt 15
Case Econ08 Ppt 15
 
Case Econ08 Ppt 13
Case Econ08 Ppt 13Case Econ08 Ppt 13
Case Econ08 Ppt 13
 
Case Econ08 Ppt 07
Case Econ08 Ppt 07Case Econ08 Ppt 07
Case Econ08 Ppt 07
 
Case Econ08 Ppt 06
Case Econ08 Ppt 06Case Econ08 Ppt 06
Case Econ08 Ppt 06
 
Case Econ08 Ppt 05
Case Econ08 Ppt 05Case Econ08 Ppt 05
Case Econ08 Ppt 05
 
Case Econ08 Ppt 04
Case Econ08 Ppt 04Case Econ08 Ppt 04
Case Econ08 Ppt 04
 
Case Econ08 Ppt 03
Case Econ08 Ppt 03Case Econ08 Ppt 03
Case Econ08 Ppt 03
 
Case Econ08 Ppt 02
Case Econ08 Ppt 02Case Econ08 Ppt 02
Case Econ08 Ppt 02
 
Case Econ08 Ppt 01
Case Econ08 Ppt 01Case Econ08 Ppt 01
Case Econ08 Ppt 01
 
Notes8
Notes8Notes8
Notes8
 
pyton Notes6
 pyton Notes6 pyton Notes6
pyton Notes6
 
pyton Notes1
pyton Notes1pyton Notes1
pyton Notes1
 
pyton Exam1 soln
 pyton Exam1 soln pyton Exam1 soln
pyton Exam1 soln
 
learn matlab for ease Lec5
learn matlab for ease Lec5learn matlab for ease Lec5
learn matlab for ease Lec5
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 

Notes7

  • 1. 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 = _____________ _____________ = _____________ _____________ = _____________
  • 2. 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.
  • 3. 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 sin_value