Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
29 views
3 pages
Tut 02
Uploaded by
KeePangChickenWings
AI-enhanced title
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
Download
Save
Save tut02 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
29 views
3 pages
Tut 02
Uploaded by
KeePangChickenWings
AI-enhanced title
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
Carousel Previous
Carousel Next
Download
Save
Save tut02 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save tut02 For Later
You are on page 1
/ 3
Search
Fullscreen
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
P04 - 4. Area of Rectangle, Square, Circle and Triangle
PDF
No ratings yet
P04 - 4. Area of Rectangle, Square, Circle and Triangle
3 pages
Practical506 Practical2
PDF
No ratings yet
Practical506 Practical2
3 pages
Python Codes Sem 5
PDF
No ratings yet
Python Codes Sem 5
221 pages
Python PDF
PDF
No ratings yet
Python PDF
55 pages
Working With Functions Xii Output Questions
PDF
50% (2)
Working With Functions Xii Output Questions
7 pages
Yr10 CS 23-24 T2 W6 L1 Functions
PDF
No ratings yet
Yr10 CS 23-24 T2 W6 L1 Functions
30 pages
Lecture TWP Python A03 1a General Features of Python
PDF
No ratings yet
Lecture TWP Python A03 1a General Features of Python
37 pages
Functions in Python Class Notes
PDF
No ratings yet
Functions in Python Class Notes
9 pages
Practical Writeups Python
PDF
No ratings yet
Practical Writeups Python
12 pages
03 Slide
PDF
No ratings yet
03 Slide
40 pages
Lecture 3
PDF
No ratings yet
Lecture 3
46 pages
BSC Physics Python Practical
PDF
No ratings yet
BSC Physics Python Practical
9 pages
Experiment 1.3
PDF
No ratings yet
Experiment 1.3
45 pages
Python - Java Script
PDF
No ratings yet
Python - Java Script
15 pages
Unit 4
PDF
No ratings yet
Unit 4
50 pages
Navdeep 781940
PDF
No ratings yet
Navdeep 781940
15 pages
Lecture 4
PDF
No ratings yet
Lecture 4
17 pages
Maths Ans
PDF
No ratings yet
Maths Ans
33 pages
Unit-2 Functions (E-Next - In)
PDF
No ratings yet
Unit-2 Functions (E-Next - In)
40 pages
Python Lab Manual
PDF
No ratings yet
Python Lab Manual
43 pages
Programming Assignment Unit 3 by Zin Myo Tun
PDF
No ratings yet
Programming Assignment Unit 3 by Zin Myo Tun
6 pages
Python Programming Unit Ii: Prof - Ajay Pashankar Assistant Professor Department of Cs & It K.M.Agrawal College Kalyan
PDF
No ratings yet
Python Programming Unit Ii: Prof - Ajay Pashankar Assistant Professor Department of Cs & It K.M.Agrawal College Kalyan
58 pages
Ejercicios de Programación 03
PDF
No ratings yet
Ejercicios de Programación 03
11 pages
Physics
PDF
No ratings yet
Physics
11 pages
Python Ws 1.3
PDF
No ratings yet
Python Ws 1.3
6 pages
Python Ws 1.3 New Sai
PDF
No ratings yet
Python Ws 1.3 New Sai
6 pages
Python Practice Programs (Extended Version)
PDF
No ratings yet
Python Practice Programs (Extended Version)
23 pages
Lab 2 NA
PDF
No ratings yet
Lab 2 NA
9 pages
Lec File Debugging
PDF
No ratings yet
Lec File Debugging
5 pages
Python 2
PDF
No ratings yet
Python 2
15 pages
Exp - 2 - SC - 2024 (Sharon Suresh) - Jupyter Notebook
PDF
No ratings yet
Exp - 2 - SC - 2024 (Sharon Suresh) - Jupyter Notebook
4 pages
A6c 8
PDF
No ratings yet
A6c 8
5 pages
Write Code and Test The Code For The Following Functions
PDF
No ratings yet
Write Code and Test The Code For The Following Functions
1 page
Program 1
PDF
No ratings yet
Program 1
6 pages
Task1 BasicProgramming AbelHendrikMP TI23T
PDF
No ratings yet
Task1 BasicProgramming AbelHendrikMP TI23T
3 pages
EMS412 Python Practice Test Solutions
PDF
No ratings yet
EMS412 Python Practice Test Solutions
6 pages
11 CSWTOct 2022 RTAns Key
PDF
No ratings yet
11 CSWTOct 2022 RTAns Key
3 pages
2A Slides
PDF
No ratings yet
2A Slides
22 pages
ME1004 Homework 4
PDF
No ratings yet
ME1004 Homework 4
1 page
Case Study: Fig. 9.18 Area Under A Curve
PDF
No ratings yet
Case Study: Fig. 9.18 Area Under A Curve
4 pages
Calculate The Area of Diffrent Geomettric Shapes
PDF
No ratings yet
Calculate The Area of Diffrent Geomettric Shapes
3 pages
Functions With Questions Part 2
PDF
No ratings yet
Functions With Questions Part 2
11 pages
Algorithms - A Simple Introduction in Python: Part Two
PDF
No ratings yet
Algorithms - A Simple Introduction in Python: Part Two
2 pages
Chapter 2
PDF
No ratings yet
Chapter 2
2 pages
Prog 15 2 3 14
PDF
No ratings yet
Prog 15 2 3 14
7 pages
Python Coding For Mathematical Problems
PDF
No ratings yet
Python Coding For Mathematical Problems
3 pages
Python3 Programming Handons and Assessment
PDF
No ratings yet
Python3 Programming Handons and Assessment
31 pages
CHANDAN KUMAR Assignment - 01
PDF
No ratings yet
CHANDAN KUMAR Assignment - 01
10 pages
CL9 CH5 Functions in Python AS
PDF
No ratings yet
CL9 CH5 Functions in Python AS
4 pages
Minor Assignment2 (Functions)
PDF
No ratings yet
Minor Assignment2 (Functions)
4 pages
IT Math Functions
PDF
No ratings yet
IT Math Functions
10 pages
ELEC 1018Y - Computational Techniques: Getting Help For Functions
PDF
No ratings yet
ELEC 1018Y - Computational Techniques: Getting Help For Functions
14 pages
Chap 4: My - Thermo - Stat
PDF
No ratings yet
Chap 4: My - Thermo - Stat
12 pages
Exp1
PDF
No ratings yet
Exp1
10 pages
Computer Science & Engineering: Worksheet 1.3
PDF
No ratings yet
Computer Science & Engineering: Worksheet 1.3
3 pages
Workshop 2
PDF
No ratings yet
Workshop 2
5 pages
20BCS1975 - PYTHON Worksheet Exp 1.3
PDF
No ratings yet
20BCS1975 - PYTHON Worksheet Exp 1.3
3 pages
SEE1002 Introduction To Computing For Energy and Environment
PDF
No ratings yet
SEE1002 Introduction To Computing For Energy and Environment
5 pages
Python-Final Exam
PDF
No ratings yet
Python-Final Exam
2 pages