EXERCISE - 8 FUNCTIONS

A) Write a function ball_collide that takes two balls as parameters
and computes if they are colliding. Your function should return a Boolean
representing whether or not the balls are colliding. B) Find mean, median, mode for the given set of numbers in a list.
A) AIM: Write a function ball_collide that takes two balls as parameters and
computes if they are colliding. Your function should return a Boolean
representing whether or not the balls are colliding.
SOURCE CODE:
import math def ball_collide(ball_tuple1,ball_tuple2): d=math.sqrt((ball_tuple1[0]-ball_tuple2[0])**2 +
(ball_tuple1[1]-ball_tuple2[1])**2) if(d<=(ball_tuple1[2]+ball_tuple2[2])): return True else: return False ball_tuple1=(-2,-2,3) ball_tuple2=(1,1,3) collision=ball_collide(ball_tuple1,ball_tuple2) if(collision): print("Balls are collide.") else: print("Balls are not collide.")
OUTPUT:
Balls are collide.
B) AIM: Find mean, median, mode for the given set of numbers in a list.
SOURCE CODE:
def mean(numlist): no_of_eles=len(numlist) summ=sum(numlist) avg=summ/no_of_eles print("Mean for given set of numbers is : ",avg) return def median(numlist): numlist.sort() if(len(numlist)%2!=0): i=len(numlist)//2 print("Median for given set of numbers is : ",numlist[i]) else: i=len(numlist)//2 print("Median for given set of numbers is : ",
(numlist[i-1]+numlist[i])/2) return def mode(numlist): modedict={} mincount=1 for ele in numlist: maxcount=numlist.count(ele) if(maxcount>=mincount): mincount=maxcount modedict[ele]=mincount if(mincount==1): print("Mode for given set of numbers is : None") else: print("Mode for given set of numbers is : ",end='') for ele in modedict: if(modedict[ele]==maxcount): print(ele,end=' ') return numlist=[] n=int(input("Enter number of elements to be insert:")) for i in range(n): ele=int(input("Enter element:")) numlist.append(ele) mean(numlist) median(numlist) mode(numlist)
OUTPUT:
Enter number of elements to be insert:4 Enter element:9 Enter element:5 Enter element:7 Enter element:3 Mean for given set of numbers is : 6.0 Median for given set of numbers is : 6.0 Mode for given set of numbers is : None

No comments:

Post a Comment

Total Pageviews