Computer Science, asked by Anonymous, 3 days ago

Write a function in python (say fm) which will return greatest digit.

Such as

>>> fm(7,8,9,6)

Output will be 9

>>> fm(0.5,0.8,7.32,8.53)

output will be 8.53

[I am giving 25 points, expecting good and well explained answer]

Answers

Answered by MisterIncredible
5

Question :

A function in python which return the greatest value.

Answer :

Required to do :-

  • Find the greatest value

Constraints :-

  • Language - Python
  • function should be used
  • function should accept n arguments

Program :-

#let's define our function

def fm(*user_input):

data = list(user_input)

data.sort()

result = data[-1]

return result

#let's check out how our function works

print(fm(2.5,6.7,8.1,8,3.5,4))

Output : -

check the attachment !

Explanation : -

According to conditions mentioned that most important thing is that out function should be capable to take 'n' number of arguments.

So, inorder to make a function to take 'n' number of arguments .. we will use the concept of *args.

*args is used when we don't know exactly how many arguments will the user going to enter.

Firstly , the parameters which is entered in the place of *args will be stored in a form of turple.

Later, we will convert this turple into list and use the sort function to arrange all the values which are entering in an ascending order.

Ultimately, the greatest value will be at the last of the list and the index number for last value of a list is -1.

Hence, we use the -1 index to fetch the largest of all values which are entered.

Attachments:
Similar questions