Introduction

Hello there! Today, we’re going to delve into handling and manipulating strings in Python, a skill that is invaluable in many areas of programming. Specifically, we'll learn how to identify consecutive groups of equal characters in a string. Are you curious to know more and enhance your skills? Then let's get started!

Task Statement

In this lesson, our goal is to write a Python function that accepts a string as input and identifies all consecutive groups of identical characters within it. A group can be defined as a segment of the text where the same character is repeated consecutively.

Your function should return a list of tuples. Each tuple will consist of the repeating character and the length of its repetition. For instance, if the input string is "aaabbcccaae", your function should output: [('a', 3), ('b', 2), ('c', 3), ('a', 2), ('e', 1)].

It's important to note that while processing the input string, we are interested only in alphanumeric characters (i.e., alphabets and digits), whether they are upper or lower case. Any other characters present won't factor into the formation of these groups.

Are you ready to unravel how to accomplish this task? Let's dive in!

Solution Building, Step 1: Initialization

As is always the case when attempting to solve a problem, it's a good idea to take preliminary steps to establish our scope. First, we will initialize an empty list groups to store our results. We will also declare two variables, current_group_char and current_group_length, which will help us keep track of the current group's character and the length of its consecutive sequence.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal