I'm preparing for a job interview. So let's say I want to remove duplicate letters from a string, I came up with two functions to accomplish this:
# option 1:
def checkDuplicates(string):
postCheck = ""
previousChar = None
for currentChar in string:
if currentChar != previousChar:
postCheck += currentChar
previousChar = currentChar
return postCheck.title()
# option 2:
def checkDuplicates2(string):
scanned = set()
postCheck = ""
for currentChar in string:
if currentChar not in scanned:
scanned.add(currentChar)
postCheck += currentChar
return postCheck.title()
Testing:
>>> print(checkDuplicates("llaatteerr")
Later
>>> print(checkDuplicates2("llaatteerr")
Later
Now let's say I want to do the same thing but on a string of words where I have to loop over each word in a string. If there are duplicates in a word, remove the duplicate character. If the second word contains characters seen in the first, it doesn't matter - each word is its own case. So for instance:
>>> checkDup("llaatteerr tthhoouugghhtt")
Later Thought
So in order to do this, I created a second function:
def checkDupInWords(words):
postCheck = ""
for word in words.split():
postCheck += f"{checkDuplicates(word)} "
return postCheck
test:
>>>print(checkDupInWords("llaatteerr llaatteerr llaatteerr"))
Later Later Later
Now my question is, what if one word contained duplicate letters, and they are meaningful duplications (i.e. "Leonardo") Is this a task accomplishable and worth pursuing in short term? Or is it something that requires knowledge of linguistics and usage of external libraries?
Bonus question: would anyone say it's possible to encounter such task in an interview, or on a normal software developer job (I think it's possible if what you're working with is a project for language or something like that.) Any insight is appreciated!