check_duplicate

Write a function in Python to check duplicate letters. It must accept a string, i.e., a sentence. The function should return True and the letters repeated if the sentence has any word with duplicate letters, else return False. 

Possible inputs/outputs: 'mississippi': 'True, i, s, p' , 'hello': 'l', 'bonjour': 'o', 'twinkle': 'False'


if anyone has a different solution or a slightly altered version, please comment!



Scroll down to view my solution...










_____________________


def check_duplicate(a: str):
global i, j
x = sorted(list(a))
duplicate = []
j = 'no'
for i in range(len(x) - 1):
if x[i] == x[i + 1]:
i += 1
j = 'yes'
elif j == 'yes':
duplicate.append(x[i])
j = 'no'
if j == 'yes':
duplicate.append(x[i])
if not duplicate:
return False
else:
return True, duplicate



Comments

Popular posts from this blog

Sieve_of_Erathosthenes