0% found this document useful (0 votes)
29 views3 pages

Tut 02

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

Tut 02

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
CS1010S, Semester I, 2021/2022—Tutorial 2 1 National University of Singapore School of Computing CS1010S: Programming Methodology Semester I, 2021/2022 Tutorial 2 Functional Abstraction, Recursion & Iteration 1, Define a function magnitude that takes in the coordinates of two points on a plane, (x1, yl) and (x2, y2), as arguments and returns the magnitude of the vector between them. def magnitude(xt, y1, x2, y2) # Returns the magnitude of the vector # between the points (x1, yl) and (x2, y2) >>> magnitude(2, 2, 5, 6) 5.0 2. A function can be viewed as a black box. All you need to know are the arguments it takes as input and what its output is. (a) One way of calculating the area of a triangle is using the formula area = } x base x height. Define a function area that calculates and returns the area of any given triangle using this formula. Decide what arguments it requires as input and what its return value should be. def area() # Return area of the triangle using the formula # area = 1/2 + base * height ) Another way of calculating the area of a triangle with sides 4, 8, ¢ is using the trigonometric ratio sine to get area = } x A x B x sin(AB), where AB is the included angle between sides & and 8. ‘The sin function is provided by the math package. You can call it by using sin after including the line from math import * at the top of your Python file. For information on how to use the math package, refer to http: //docs. python. org/3.6/library/nath. html Define a function area? that calculates and returns the area of any given trian- gle using this formula Decide what arguments the function requires as input and what its return, value should be. def area2() # Return area of the triangle using the formula # area = 1/2 * A * B * sin(AB) (c) Both functions calculate the same result. Can they be directly substituted for each other? Why? CS1010S, Semester I, 2021/2022—Tutorial 2 2 (@ We can also calculate the area of triangle using Heron's Formula, area = Jats = al(s—B){s— 6) where s = “+, Assume you are given a function herons_formula that takes 3 arguments a, b, cand returns the area ofa triangle with sides of length a, b,c. Define a function area3 that uses Heron’s formula to calculate and return the area of a given triangle given the x,y coordinates of the 3 points of the triangle. You may use the magnitude function defined in Question 1 def area3(x1, y1, x2, y2, x3, y3): # Return area of the triangle using Heron's formula. 8. For each of the questions below, what is printed when the expressions are evalu- ated? f@) def Fool) ize result = 0 while i < 10 result +2 i ited return result print (F001 ()) (b) def F002: return result print (F002()) (© def bariQ result = 0 for i in range(10) result +2 i return result print (bart ()) (@) def bar2() result = 0 for i in range(10) fan eo continue result +2 i return result print (bar2()) €S10108, Semester I, 2021/2022—Tutorial 2 3 4, Write a function sun_even_factorials that finds the sum of the factorials of the even numbers that are less than or equal ton, where n > 0. >>> sum_even_factorials(1) i >>> sum_even_factorials(3) 3 >>> sum_even_factorials(6) 747 5. Suppose we define the function: def f(g): return g(2) ‘Then we have def square(x) return x #« 2 >>> F(square) 4 >>> f(lambda 2: 2 * (2 + 19) 6 What happens if we (perversely) ask the interpreter to evaluate the combination £(F)? Explain

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy