Handling Text Data
Explore a few questions on handling text data in Python, including text processing and extraction, starting from a smaller to a higher level of complexity.
Text data is everywhere—from product reviews to medical records—and knowing how to process it efficiently is a key skill in interviews. In this lesson, we’ll work through a series of challenges that build from basic string operations to regex-powered information extraction. Let’s get started.
String processing in Python
Let’s assume you’re building a text editor with a feature that helps writers spot repetitive words or letters in their drafts. To get started, you need a function that checks for the first repeated character in a line of text so that it can flag unnecessary repetition. If there are no repeating characters, it should simply return “None”.
This question is frequently asked in technical interviews for roles that require strong foundational programming skills.
Write a function that takes a string as input and returns the first character that repeats. If no character repeats, return “None”.
Example:
Input:
"pythoninterviewquestion"
Output:
"n"
from itertools import dropwhiledef find_first_recurring_char(s):#TODO - your implementation# Example usageinput_string = "pythoninterviewquestion"output = find_first_recurring_char(input_string)print(f"The first recurring character is: {output}")assert output == "n"
Sample answer
Let’s look at our efficient solution to implement this snippet.
We can use a set
to keep track of characters that have been seen. The set
...