Activity #5 – Applying Functions
The output of this activity will be the same as activity #3 but instead you are
required to use a function for the following algorithm:
1. A function for getting the length of each string
2. A function to get the shortest string
3. And a function to get the longest string
Assume that each string has its own unique length.
Sample Run:
Sample Run A
Enter value of X: 3
Enter string value: a
Enter string value: ab
Enter string value: abc
a - total length is 1
ab - total length is 2
abc - total length is 3
The shortest string is 1
The longest string is 3
Sample Run B
Enter value of X: 4
Enter string value: chico
Enter string value: strawberry
Enter string value: banana
Enter string value: guyabano
chico - total length is 5
strawberry - total length is 10
banana - total length is 6
guyabano - total length is 8
The shortest string is chico
The longest string is strawberry
x=int(input("Enter value of X: "))
num=[]
for n in range(x):
N=input("Enter strng value: ")
num.append(N)
r=len(num[0])
y=len(num[1])
z=len(num[2])
print("Length of ",num[0],"is",r)
print("Length of ",num[1],"is",y)
print("Length of ",num[2],"is",z)
def min(x,y,z):
if (x < y) and (x < z):
min = x
elif (y < x) and (y < z):
min = y
elif (z < y) and (z < x):
min = z
return(min)
def max(x,y,z):
if (x > y) and (x > z):
max = x
elif (y > x) and (y > z):
max = y
elif (z > y) and (z > x):
max = z
return(max)
#shortest strng
print("Shortest strng is", min(r,y,z))
#longest
print("Shortest strng is", max(r,y,z))
Comments
Leave a comment