Are you looking to rewrite the Max function present in Python to accommodate your requirement? In this article, I will show you how you can do that.
You can rewrite or create the max function of Python from scratch. It is possible to use the max() built-in method to return the largest item in an iterable, as well as the largest of two or more parameters.
Creating or Rewriting the Max Function in Python
You can create a new function named max in Python and pass on the variable whatever you defined in the function to return the desired result.
Let me create a max function that returns the character with high precedence. For example, if I give two input characters as ‘a’ and ‘b’, ‘b’ should be returned as the max value of the function.
#defining the max function def max(input1, input2): if input1 > input2: return input1; else: return input2; #Initializing the input variables input1 = 'a' input2 = 'b' #Printing the Maximum value returned from the Max Function print("The Maximum is: " + max(input1, input2))
The Maximum is: b
As shown in the above code, I have rewritten the max function of the Python from the scratch and returned the maximum character based on its precedence. Similarly, you can rewrite the same function to any type of comparison you want to make and return the maximum out of it.
I hope the above tutorial helped you to make your own max function from scratch. Let me know if you have any better code or you want to set a function or modify it to something else and are not able to do it. I will be happy to help you people.