Home > Software design >  first pair of the same symbols
first pair of the same symbols

Time:01-10

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"

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))

CodePudding user response:

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.

  • Related