2024 25 COL100 Lab 3 Function
2024 25 COL100 Lab 3 Function
Lab 3: Functions
January 20, 2025
1 Functions
Functions are a fundamental building block of programming and play a crucial role in
organizing code, making it more modular, and enhancing reusability. In this lab, we will
explore the concept of functions by working through various sample programs.
The function definition includes its name distance and the parameter list x1, y1,
x2, and y2 representing the coordinates of two points in the Cartesian plane. Inside
the function, we calculate the distance between two points. Finally, the function returns
the computed distance.
We can prompt the user for input, make the function calls to calculate the perimeter
and display the result. Here is the complete program that calculates the perimeter of a
triangular garden given the coordinates of its vertices. Note that the distance function is
called three times, each time with a different set of coordinates. The calculated distances
are added to obtain the perimeter of the triangular garden.
1 import math
2
18 # Compute p e r i m e t e r
19 p e r i m e t e r = ( d i s t a n c e ( x1 , y1 , x2 , y2 ) +
20 d i s t a n c e ( x2 , y2 , x3 , y3 ) +
21 d i s t a n c e ( x3 , y3 , x1 , y1 ) )
22
23 # Display r e s u l t
24 p r i n t ( f ” Perimeter o f t r i a n g l e = { perimeter }” )
9 # Input from t h e u s e r
10 num1 = i n t ( i n p u t ( ” Enter t h e f i r s t number : ” ) )
11 num2 = i n t ( i n p u t ( ” Enter t h e s e c o n d number : ” ) )
12
13 # P r i n t b e f o r e swapping
14 p r i n t ( f ” B e f o r e swapping : F i r s t number = {num1} , Second number = {
num2}” )
15
16 # C a l l t h e f u n c t i o n t o swap numbers
17 num1 , num2 = swap numbers (num1 , num2)
18
19 # P r i n t a f t e r swapping
20 p r i n t ( f ” A f t e r swapping : F i r s t number = {num1} , Second number = {num2
}” )
Page 2
2 Submission Problem
2.1 Instructions
• Complete the code of the following problem in main.py program file.
• The Autograder on Gradescope will evaluate the submitted program on some test
cases.
• Your program should calculate and print the Euclidean distance (rounded up to
two decimal places) between the two vectors.
1 1 2 3
2 4 5 6
1 5.20
1 8 9 7
2 3 2 5
Page 3
2.2.4 Sample Output 2
1 8.83
Page 4
3 Practice Problems
3.1 Cosine Similarity
Write a program to calculate the cosine similarity between two 3D vectors using functions.
The cosine similarity is defined as:
A·B
Cosine Similarity(A, B) =
∥A∥∥B∥
The L2 (Euclidean) norm of a vector V is given by:
v
u 3
uX
∥V ∥ = t Vi2
i=1
Page 5
3.1.1 Sample Input 1
1 2
2 1 0
3 0 1
1 2
2 1 1
3 1 1
• Your program should print the values in a single line with space separation for
a,b,c,d,e.
Page 6
3.2.1 Sample Input 1
1 0 0 −50 50 0
Explanation:
The numbers after the first execution of the function are 50 50 50 50 0
The numbers after the second execution of the function are 0 0 0 50 0
The numbers after the third execution of the function are 0 0 -50 50 0
1 C O D E
1 283
1 Play
1 406
Page 7