0% found this document useful (0 votes)
11 views7 pages

2024 25 COL100 Lab 3 Function

This document outlines Lab 3 for COL100: Introduction to Computer Science, focusing on functions in programming. It includes examples such as calculating the perimeter of a triangular garden and swapping two numbers, along with instructions for a submission problem involving Euclidean distance and cosine similarity. Additionally, it presents practice problems that require implementing various functions to solve specific tasks.

Uploaded by

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

2024 25 COL100 Lab 3 Function

This document outlines Lab 3 for COL100: Introduction to Computer Science, focusing on functions in programming. It includes examples such as calculating the perimeter of a triangular garden and swapping two numbers, along with instructions for a submission problem involving Euclidean distance and cosine similarity. Additionally, it presents practice problems that require implementing various functions to solve specific tasks.

Uploaded by

chiragyadavarya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

COL100: Introduction to Computer Science

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.

1.1 Perimeter of a Triangular Garden


In this sample program, we will explore the usage of functions by creating a program that
calculates the perimeter of a triangular garden based on the coordinates of its vertices.
The program will use a user function called distance to compute the distance between
two points in the Cartesian plane.
Next, we provide the implementation or definition of the distance function.
1 def distance ( x1 , y1 , x2 , y2 ) :
2 dx = x2 - x1
3 dy = y2 - y1
4 dist = math . sqrt ( dx * dx + dy * dy )
5 return dist

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

3 # Function t o compute t h e d i s t a n c e between two p o i n t s


4 d e f d i s t a n c e ( x1 , y1 , x2 , y2 ) :
5 dx = x2 − x1
6 dy = y2 − y1
7 d i s t = math . s q r t ( dx ∗ dx + dy ∗ dy )
8 return dist
9
10 # Get c o o r d i n a t e s o f v e r t i c e s
11 x1 = f l o a t ( i n p u t ( ” Enter x−c o o r d i n a t e of vertex 1: ”) )
12 y1 = f l o a t ( i n p u t ( ” Enter y−c o o r d i n a t e of vertex 1: ”) )
13 x2 = f l o a t ( i n p u t ( ” Enter x−c o o r d i n a t e of vertex 2: ”) )
14 y2 = f l o a t ( i n p u t ( ” Enter y−c o o r d i n a t e of vertex 2: ”) )
15 x3 = f l o a t ( i n p u t ( ” Enter x−c o o r d i n a t e of vertex 3: ”) )
16 y3 = f l o a t ( i n p u t ( ” Enter y−c o o r d i n a t e of vertex 3: ”) )
17

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 }” )

1.2 Swap Two Numbers


This example illustrates the scope of parameters where the function parameters receive
copies of the actual parameters, and any modification made within the function does not
impact the original variables outside of it.
1 # Function t o swap two numbers
2 d e f swap numbers ( a , b ) :
3 # Swapping u s i n g a temporary v a r i a b l e
4 temp = a
5 a = b
6 b = temp
7 return a , b
8

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.

• Submit the program file in the assignment titled “Lab 3” on Gradescope.

• The Autograder on Gradescope will evaluate the submitted program on some test
cases.

2.2 Euclidean Distance between Two Vectors


Write a program to calculate the Euclidean distance between two vectors in 3D space
using a function. The Euclidean distance between two vectors A = (a1 , a2 , a3 ) and
B = (b1 , b2 , b3 ) is defined as:
p
d(A, B) = (a1 − b1 )2 + (a2 − b2 )2 + (a3 − b3 )2
Please implement your program that satisfies the following specifications.

• Your program should read two vectors, each representing a 3D vector.

• Your program should calculate and print the Euclidean distance (rounded up to
two decimal places) between the two vectors.

You can implement a function as follows.


1 e u c l i d e a n d i s t a n c e ( a1 , a2 , a3 , b1 , b2 , b3 ) ;

2.2.1 Sample Input 1

1 1 2 3
2 4 5 6

2.2.2 Sample Output 1

1 5.20

2.2.3 Sample Input 2

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

Please implement your program that satisfies the following specifications:


• Your program should read a positive integer n (which will be fixed at 3 for 3D
space).
• Your program should then read 3 floating-point numbers corresponding to the first
vector.
• Your program should then read 3 floating-point numbers corresponding to the
second vector.
• Your program should print the following four space-separated floating-point num-
bers:
– L2 Norm of the first vector
– L2 Norm of the second vector
– Dot product of the two vectors
– Cosine similarity between the two vectors
You can implement the following functions:
• l2 norm(vector)
• dot product(vector1, vector2)
• cosine similarity(vector1, vector2)
You can implement the following functions.
1 l2 norm ( v e c t o r , d i m e n s i o n ) ;
2 dot product ( vector1 , vector2 , dimension ) ;
3 c o s i n e s i m l a r i t y ( vector1 , vector2 , dimension ) ;

Page 5
3.1.1 Sample Input 1

1 2
2 1 0
3 0 1

3.1.2 Sample Output 1

1 1.00 1.00 0.00 0.00

3.1.3 Sample Input 2

1 2
2 1 1
3 1 1

3.1.4 Sample Output 2

1 1.41 1.41 2.00 1.00

3.2 Five Numbers and a Cipher


Write a program that will take five numbers which act like a cipher. To unlock the cipher,
you need to perform the following operations on the input (a,b,c,d,e) three times.
1 a = a - b
2 b = b - c
3 c = c - d
4 d = d - e
5 e = e - a

Please implement your program that satisfies the following specifications.

• Your program should read a space separated input of 5 integers.

• Your program should print the values in a single line with space separation for
a,b,c,d,e.

You can implement the following functions.


1 evaluate (a , b , c , d , e ) ;

Page 6
3.2.1 Sample Input 1

1 250 200 150 100 50

3.2.2 Sample Output 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

3.3 Characters and Numbers


You are given a 4 letter word as an input in a prescribed format, you are tasked with
finding the sum of the characters in this word. You can use the following functions:

• ord() converts a character to its corresponding Unicode code point

• chr() converts a Unicode code point back to its character.

3.3.1 Sample Input 1

1 C O D E

3.3.2 Sample Output 1

1 283

3.3.3 Sample Input 2

1 Play

3.3.4 Sample Output 2

1 406

Page 7

You might also like