Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views9 pages

Naincy Mod 5 Python

The document contains explanations of functions to check if balls are colliding, find mean, median and mode of a list, check if strings are nearly equal, find duplicate elements in a list, find unique elements in a list, calculate cumulative product of a list, and reverse a list without using the reverse function.

Uploaded by

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

Naincy Mod 5 Python

The document contains explanations of functions to check if balls are colliding, find mean, median and mode of a list, check if strings are nearly equal, find duplicate elements in a list, find unique elements in a list, calculate cumulative product of a list, and reverse a list without using the reverse function.

Uploaded by

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

Name :- Naincy Jaiswal

Roll Number :- 22CS011107 , Section :- B


Programming Practices II (YCS 4104)
FUNCTION

1) Write a function ball_collide that takes two balls as parameters &


computes if they are colliding. Your function should return a Boolean
representing whether or not the balls are colliding. Program :-
import math def

ball_collide(p):

x=False

a,b,c,d,e,f=p q=math.sqrt((d-

a)**2-(e-b)**2) y=c+f if

q<=y: x=True else:

x=False return x i=eval(input("Enter the 2 ball centers & radius in the

form of a tuple :- ")) if ball_collide(i):

print("The 2 balls collide") else:

print("The 2 balls don't collide")

Output :-
Enter the 2 ball centers & radius in the form of a tuple :- (3,5,7,13,15,19)
The 2 balls collide
Explanation :-
• The math module is imported at the beginning of the program.
• A function ball_collide() is defined which takes a tuple ‘p’ as its argument. • Within
the function, first a variable ‘x’ is initialized with False.
• The distance between the centre points of the 2 balls is calculated & the result is stored in
variable ‘q’. The sum of the 2 radius is also calculated in variable ‘y’.
• If the value of ‘q’ is less than equal to ‘y’, the ‘x’ is changed to True otherwise the value
of ‘x’ is reinitialized with False.
• The value of ‘x’ after the if-else condition is returned.
• Within the main part of the program, a tuple containing the centers of the 2 balls & their
radius is accepted from the user in variable ‘i’.
• The function ball_collide() is called & the tuple ‘i’ is passed onto it.
• If the function returns True, the balls are colliding else they aren’t colliding.

2) Find mean, median, mode for the given set of numbers in a list.
Program :-
import statistics def

compute(s):

x=statistics.mean(s) y=statistics.median(s)

z=statistics.mode(s) return x,y,z

l=eval(input("Enter data in a list

:- "))

a,b,c=compute(l) print("Mean

=",a) print("Median =",b)

print("Mode =",c)
Output :-
Enter data in a list :- [2,4,6,3,7,2,5]

Mean = 4.142857142857143

Median = 4

Mode = 2

Explanation :-
• The statistics module is imported at the beginning of the program. • A function
compute() is defined which takes a list ‘s’ as its argument.
• The mean, median, & mode of the input data is calculated using the mean(), median(), &
the mode() function in variables x,y,z. The values of these 3 variables are returned.
• Within the main part of the program, data is accepted from the user in the form of a list.
• The list entered by the user is passed onto the function compute().
• The values returned by the function are stored in variables a,b,c.
• The values of Mean, Median & Mode are displayed on the screen.

3) Write a function nearly_equal to test whether two strings are nearly


equal Program :-
def nearly_equal(x,y): if

len(x)!=len(y): return

False else: c=0

for i in range(len(x)):

if x[i]!=y[i]:

c+=1 if

c<2:

return True
else: return False

a=input("Enter 1st string :- ")

b=input("Enter 2nd string :- ") if

nearly_equal(a,b): print("Nearly

Equal Strings") else: print ("Not

Nearly Equal Strings")

Output :-
Enter 1st string :- fox Enter

2nd string :- vox

Nearly Equal Strings

Explanation :-
• A function nearly_equal() is defined which takes 2 strings ‘x’ & ‘y’ as its arguments.
• If the length of both the strings are unequal, then False is returned.
• Otherwise, firstly a variable ‘c’ is initialized with 0. A for loop is iterated throughout the
length of the string ‘x’. It is checked whether every character of the 1st & the 2nd string
matches or not. If not, then the value of ‘c’ is increased by 1.

• After the termination of the for loop, if the value of ‘c’ is less than 2, True is returned
otherwise False is returned. • Two strings ‘a’ & ‘b’ are accepted from the user in
the main part of the program.
• The nearly_equal() function is called & if it returns True, the strings are nearly equal
otherwise the strings are not nearly equal.

4) Write a function dups to find all duplicates in the list. Program


:- def

dups(x):

a=[] for i

in x: if i in

a:

print(i)

a.append(i) p=eval(input("Enter a list :-

")) print("Duplicate elements in the list

are :-") dups(p)

Output :-
Enter a list :- [4,6,2,6,1,4,7]

Duplicate elements in the list are :-

Explanation :-
• A function dups() is defined which takes a list ‘x’ as its argument.
• An empty list ‘a’ is declared.

• A for loop with index variable ‘i’ is iterated on the list ‘x’.
• If the element in ‘i’ is present in the list ‘a’, then it is displayed on the screen.
• The element in ‘i’ is added to the list ‘a’
• A list is accepted from the user in variable ‘p’.
• The dups function is called & the list ‘p’ is passed onto it.
5) Write a function unique to find all the unique elements of a list.
Program :-
def unique(x):

a=[] for i in x: if

x.count(i)==1 and i not in a:

print(i)

a.append(i) p=eval(input("Enter a list

:- ")) print("Unique elements in the list

are :-") unique(p)

Output :-
Enter a list :- [4,6,2,6,1,4,7]

Unique elements in the list are :-

7
Explanation :-

A function unique() is defined which takes a list ‘x’ as its argument. An


empty list ‘a’ is declared.
• A for loop with index variable ‘i’ is iterated on the list ‘x’.
• If the element ‘i’ is present in the list ‘x’ only once & if ‘i’ is not present in ‘a’, then the
element ‘i’ is displayed on the screen & it is added to list ‘a’ also.
• A list is accepted from the user in variable ‘p’.
• The unique function is called & the list ‘p’ is passed onto it.

6) Write a function cumulative_product to compute cumulative


product of a list of numbers. Program :-
def cumulative_product(x):

f=1 y=[]

for i in x:

f*=i

y.append(f) return y p=eval(input("Enter a list :-

")) print("Cumulative Product is :-

",cumulative_product(p)) Output :-
Enter a list :- [2,5,3,8,5,3,6]

Cumulative Product is :- [2, 10, 30, 240, 1200, 3600, 21600]

A function cumulative_product() is defined which takes a list ‘x’ as its argument. A


variable ‘f’ is initialized with 1. An empty list ‘y’ is also created.
• A for loop with index variable ‘i’ is iterated on the list ‘x’.
• The value of ‘f’ is multiplied with the value of ‘i’ & the same is updated.
Explanation :-

• The same is also added to the list ‘y’.


• After the termination of the for loop, the list ‘y’ is returned.
• A list is accepted from the user in variable ‘p’.
• The function cumulative_product() is called & the list ‘p’ is passed onto it. The list
returned by the function is displayed on the screen.

7) Write a function reverse to reverse a list. Without using the reverse


function. Program :-
def reverse(x): for i in

range(len(x)//2):

t=x[i] x[i]=x[len(x)-i-1]

x[len(x)-i-1]=t return x p=eval(input("Enter

a list :- ")) print("Reverse List

is :-",reverse(p))

Output :-
Enter a list :- [2,5,3,7,9,4,6]

Reverse List is :- [6, 4, 9, 7, 3, 5, 2]

A function reverse() is defined which takes a list ‘x’ as its argument.


A for loop with index variable ‘i’ is iterated throughout half the length of the list ‘x’.
• In every iteration, the ith element from the first is swapped with the ith element from the
last. This process continues till the for loop terminates.
• After the termination of the for loop, the list ‘x’ is returned.
• A list is accepted from the user in variable ‘p’.
Explanation :-

• The reverse() function is called & the list it returns is displayed on the screen.

You might also like