Exercise 83

Write a Python algorithm that takes as input a string 's' and which returns the first repeated character in the string s.
For instance: if s = "django framework", the algorithm returns the character 'a'.

Solution

# create a function to test if given character is repeated within a given string
def isRepeated(s,c):
    counter = 0
    for x in s:
        if x == c:
            counter = counter + 1
    if counter >= 2:
        return True
    else:
        return False
# function which determines the first repeated character
def firstRepeated(s):
    repeated = ''
    for x in s:
        if isRepeated(s,x):
            repeated = x
            break
    return repeated
          
# Example
s = "django framework"
print("The first repeated character is : " , firstRepeated(s))
# The output is: 'The first repeated character is :  a'




 

Younes Derfoufi
CRMEF OUJDA

 

Younes Derfoufi
my-courses.net

One thought on “Solution Exercise 83 : python algorithm that determines the first repeated character within a given string”

Leave a Reply