In: Computer Science
Use python programming to write this code and provide a screen short for the code.
2. Write a function that takes one argument (a string) and returns a string consisting of the single character from that string with the largest value. Your function should contain a for loop.
You can assume that the input to your function will always be a valid string consisting of at least one character. You can assume that the string will consist only of lower-case letters between 'a' and 'z', inclusive.
Hints
Example inputs and outputs
Input |
Expected output |
'python' |
'y' |
'antelope' |
't' |
Program: largestvalue.py
def largestvalue(s): // Function declaration
l=[0 for i in range(26)] // Declare a list for 26 characters;
for i in s: //checks the each character in a string
l[ord(i)-97]=1 //enter the value 1 in each character value
represented in list
for i in range(25,-1,-1)://checks the list from backward
if l[i]==1://whenever it found 1
return str(chr(i+97)) //it return the value as string
return 0
s=input() //Reads the input as string
print(largestvalue(s)) // Calls the function and print the return
value.
Output: