[Solved] Python 3.x -> Find Last Occurrence of character in string by using recursion and loop


With iteration:

def find_last(line,ch):
    last_seen =-1
    for i in range(len(line)):
        if line[i] == ch:
            last_seen=i
    return last_seen

With recursion:

def find_last_rec(line,ch,count):
    if count==line(len)-1:
        return -1
    elif line[count] ==ch:
        return max(count, find_last_rec(line,ch,count+1))
    else:
        return find_last_rec(line,ch,count+1)


def find_last(line,ch):
    return find_last_rec(line,ch,0)

solved Python 3.x -> Find Last Occurrence of character in string by using recursion and loop