3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and no other characters) and prints 1) the "least" character in the string, where one character is less than another if it occurs earlier in the alphabet (thus 'a' is less than 'C' and both are less than 'y') and 2) the index of the first occurrence of that character. When comparing letters in this problem, ignore case - i.e. 'e' and 'E' should be treated as equal. However, your output should use the case of the least letter as it appears in the input. Thus, in the example below "The least char is 'b' ..." would be incorrect. Important: your function must contain exactly one loop and you must determine the index of the character within the loop (i.e. you may not use Python's min, index, or find, and you may not use any lists). You may, however, use Python's lower() function to help with string case.
For example, >>> leastChar("yRrcDefxBqubSlyjYelskd")
should yield:
The least char is 'B' and occurs at position 8.
Hi, please find my implementation.
Please let me know in case of any issue.
def leastChar(str):
i = 0
min_index = 0
while i < len(str):
if str[min_index].lower() >
str[i].lower():
min_index =
i;
i = i +1
return min_index
str = "yRrcDefxBqubSlyjYelskd"
min_index = leastChar(str)
print "The least char is %c and occurs at position %d.
"%(str[min_index], min_index)
Get Answers For Free
Most questions answered within 1 hours.