Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

first pair of the same symbols

Find the first (consecutive) pair of the same symbols using for-loop. It doesn’t matter if there are more than 2 same consecutive symbols.

Example:

"abBcccc" returns "cc"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What I have so far:

result = []
    for c in range(len(text) - 1):
        if text[c] == text[c + 1]:
            result.append(text[c])
    print("".join(result))

>Solution :

Using iterators with index seems to be the only way here:

string = "abBcccc"

for index in range(0, len(string)-1):
    if string[index] == string[index+1]:
        print(string[index:index+2])
        break

It’s needed to break to only get the first one.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading