Practical506 Practical2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

3/1/23, 12:42 AM final506

Roll No: 22BCE506

Name: Jenish Gajera

Practical No: 2

A. Write a function areaTriangle that takes the lengths of three sides of the triangle as
input parameters and returns the area of the triangle as an output. Also, assert that the
sum of the length of any two sides is lesser than the third side. Write a main function
that accepts as command-line arguments and computes the area of a triangle using the
function areaTriangle.

In [ ]: import math


import sys

# User defined function areaTriangle to find the area of a triangle


def areaTriangle(s1, s2, s3):
maxx = max(s1,s2,s3)
s = (s1+s2+s3)/2
temp = s*(s-s1)*(s-s2)*(s-s3)
area = math.sqrt(temp)

# asserting that the sum of the length of any two sides is lesser than the t
if s1 < maxx:
if s2 < maxx:
if (s1+s2) < s3:
print("Sum of {} & {} is less then {}.".format(s1,s2,s3))
else:
print("Sum of {} & {} is not less then {}.".format(s1,s2,s3))
else:
if (s1+s3) < s2:
print("Sum of {0} & {2} is less then {1}.".format(s1,s2,s3))
else:
print("Sum of {0} & {2} is not less then {1}.".format(s1,s2,s3))
elif s2 < maxx:
if s3 < maxx:
if (s3+s2) < s1:
print("Sum of {2} & {1} is less then {0}.".format(s1,s2,s3))
else:
print("Sum of {2} & {1} is not less then {0}.".format(s1,s2,s3))
else:
if (s1+s2) < s3:
print("Sum of {0} & {1} is less then {2}.".format(s1,s2,s3))
else:
print("Sum of {0} & {1} is not less then {2}.".format(s1,s2,s3))
else:
if (s1+s2) < s3:
print("Sum of {} & {} is less then {}.".format(s1,s2,s3))
else:
print("Sum of {} & {} is not less then {}.".format(s1,s2,s3))
return area

def main():
if(len(sys.argv) > 1):
# storing the input from command line
t1 = sys.argv[1]

localhost:8888/nbconvert/html/final506.ipynb?download=false 1/2
3/1/23, 12:42 AM final506

t2 = sys.argv[2]
t3 = sys.argv[3]
# Calculating the area by calling the areaTriangle function
area = areaTriangle(int(t1),int(t2),int(t3))
print("Area : {:.3f}".format(area))
else:
print("Enter the three sides with the py... statement in cmd itself.")

main()

B. Write a function that takes a string as a parameter and returns a string with every
successive repetitive character replaced with a star(). For Example, ‘balloon’ is returned as
‘balo*n’.

In [1]: def f1(ss):


s1s=''
n = len(ss)
for i in range(n):
if ss[i] in s1s:
s1s = s1s+"*"
else:
s1s = s1s+ss[i]
return s1s
ss = input("enter string: ")
print(f1(ss))

enter string: balloon


bal*o*n

C. Write a function that takes a number as n input parameter and returns the
corresponding text in words; for example, on input 452, the function should return ‘Four
Five Two’. Use a dictionary for mapping to digits to their string representation.

In [2]: def numName(num):


# Dictionary having all the data we need
data = {
"0" : "Zero",
"1" : "One",
"2" : "Two",
"3" : "Three",
"4" : "Four",
"5" : "Five",
"6" : "Six",
"7" : "Seven",
"8" : "Eight",
"9" : "Nine",
}
print("Output : ",end="")
for i in range(len(n)):
# Accessing the dictionry with the user input index
print(data[n[i]], end=" ")

n = input("Enter any number : ")


numName(n)

Enter any number : 506


Output : Five Zero Six

localhost:8888/nbconvert/html/final506.ipynb?download=false 2/2

You might also like