DEV Community

Cover image for Python challenge_5
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on • Edited on

Python challenge_5

Double letters

level of challenge = 3/10

The goal of this challenge is to analyze a string to check if it
contains two of the same letter in a row.
For example, the string "hello" has l twice in a row,
while the string "nono" does not have two identical letters in a row.

Define a function named double_letters that takes a single parameter
The parameter is a string.
Your function must return True if there are two identical letters
in a row in the string, and False otherwise.

Hint

Use a for or while loop and keep track of the current index.
In each iteration, grab the letter at the current index,
And the letter after that, and compare the two.

My solution

def double_letters(letters):
    for letter in range(len(letters)-1):
        if letters[letter] == letters[letter+1]:
            return True
    return False

print(double_letters("letters"))
Enter fullscreen mode Exit fullscreen mode

Another solution

def double_letters(string):
    for i in range(len(string) - 1):
        letter1 = string[i]
        letter2 = string[i+1]
        if letter1 == letter2:
            return True
    return False
Enter fullscreen mode Exit fullscreen mode

shorter solution

def double_letters(string):
    return any([a == b for a, b in zip(string, string[1:])])
Enter fullscreen mode Exit fullscreen mode
def has_double_letters(letters):

    for letter in range(len(letters)-1):
        if letters[letter] == letters[letter+1]:
            print(f"There were tow {letters[letter]} at postion {str(letter)}")

print(double_letters("nono"))
Enter fullscreen mode Exit fullscreen mode

Add your solution in the comment :)

Top comments (2)

Collapse
 
shapeshapa profile image
shapa
import re

def double_letters(letters: str) -> bool:
    return bool(re.search(r'(.)\1', letters, re.IGNORECASE))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
agtoever profile image
agtoever • Edited

Generalized solution; strifes to make it “pythonic”:

def multiple_letters(string, n=2):
    return any(c * n in string for c in set(string))
Enter fullscreen mode Exit fullscreen mode

Try it online!