Create a program that will take in a string as input and, if there are duplicates of more than two alphabetical characters in the string, returns the string with all the extra characters in a bracket.

For example, the input "aaaabbcdefffffffg" should return  "aa[aa]bbcdeff[fffff]g" 

Please also ensure that the input is a string, and return "Please enter a valid string" if it is not.

Difficulty: introductory

Starter code:

def string_parse(string):
	

Solutions:
import re
def string_parse(string):
    return re.sub(r'(.)\1(\1+)', r'\1\1[\2]', string) if isinstance(string, str) else 'Please enter a valid string'

import re
def string_parse(s):
    if not isinstance(s, str): return "Please enter a valid string"
    for l in set(s):
        s = re.sub(r"(%s%s)(%s+)" % (l, l, l), r"\1[\2]", s)
    return s

import re


def string_parse(s):
    if isinstance(s, str):
        return re.sub(r"(.)\1(\1+)", r"\1\1[\2]", s)
    return "Please enter a valid string"

from functools import partial
from re import compile

REGEX = partial(compile(r"(.)\1(\1+)").sub, r"\1\1[\2]")

def string_parse(string):
    return REGEX(string) if type(string) == str else "Please enter a valid string"

import re
def string_parse(string):
    try:
        def replace(match):
            return '{0}{0}[{1}]'.format(*match.group(1, 2))
        return re.sub(r'(.)\1(\1+)', replace, string)
    except TypeError:
        return 'Please enter a valid string'

from itertools import groupby

def string_parse(string):
    if isinstance(string,str):
        gs = groupby(string)
        f = lambda s : s if len(s) < 3 else s[:2] + '[' + s[2:] + ']'
        return ''.join([ f(''.join(list(g))) for _, g in groupby(string)])
    else:
        return 'Please enter a valid string'



from itertools import groupby

def string_parse(string):
  return ''.join(f'{x*2}[{x*(c-2)}]' if c > 2 else x * c for x, c in [(x, len(list(gp))) for x, gp in groupby(string)]) if isinstance(string, str) else 'Please enter a valid string'

# Attempt without regex, let's use classic generator function.

def string_parse_gen(string):
    counter, prev_char = 0, ''
    for char in string:
        if char == prev_char:
            counter += 1
            if counter == 2:
                yield '['
        else:
            if counter >= 2:
                yield ']'
            prev_char = char;
            counter = 0;
        yield char;
        
    if counter >= 2:
        yield ']'


def string_parse(string):
    return ''.join(list(string_parse_gen(string))) if isinstance(string, str) else "Please enter a valid string"

def string_parse(s):
    from itertools import groupby
    if not isinstance(s, str):
        return 'Please enter a valid string'
    
    x = [(i, sum([1 for j in group])) for i, group in groupby(s)]
    x = [i[0] * 2 + '[' + i[0] * (i[1] - 2) + ']' if i[1] >= 2 else i[0] for i in x]
    x = ''.join(x)
    x = x.replace('[]','')
    return x

import re
def string_parse(string):
    if not isinstance(string, str):
        return 'Please enter a valid string'
    def repl(Match):
        
        s = Match[0]
        n = len(s)
        c = s[0]
        
        return s if n <= 2 else c * 2 + f'[{c * (n - 2)}]'
    return re.sub(r'(\w)\1*', repl, string)

import re
def string_parse(s):
    try:
        return re.sub(r'(.)\1+', lambda m:  m.group()[0:2] + "[" +  m.group()[2:] + "]" if len(m.group())>2 else m.group()  , s)    
        #lambda x: True if x % 2 == 0 else False
    except TypeError:
        return "Please enter a valid string"

import re

def string_parse(s):
    return re.sub(r'((.)\2*)', lambda x: f'{2 * x[2]}[{x[1][:len(x[1]) - 2]}]' \
                          if len(x[1]) > 2 else x[1], s) if isinstance(s, str) \
                          else "Please enter a valid string"

def string_parse(string):
    if not isinstance(string, str):
        return 'Please enter a valid string'
    res = []
    count = 0
    last_char = None
    for c in string:
        if c != last_char:
            if count > 2:
                res.append(']')
            count = 0
        elif count == 2:
            res.append('[')
        count += 1
        last_char = c
        res.append(c)
    if count > 2:
        res.append(']')
    return "".join(res)


from itertools import groupby

def string_parse(str_in):
    if not isinstance(str_in, str):
        return "Please enter a valid string"
    str_out= []
    for _, group in groupby(str_in):
        s = ''.join(group)
        str_out.append(s if len(s) < 3 else f'{s[:2]}[{s[2:]}]')
    return ''.join(str_out)

def string_parse(string):
    if type(string)!=str:return "Please enter a valid string"
    try:
        out=string[:2]
        temp=''
        for i in string[2:]:
            if i==out[-1]==out[-2]:
                temp+=i
            else:
                if temp!='':out+='['+temp+']'
                out+=i
                temp=''
        if temp!='':out+='['+temp+']'
        return out
    except:
        return "Please enter a valid string"
        


def string_parse(string):
    if not isinstance(string, str):
        return 'Please enter a valid string'
    letters = ['0']
    for i in string:
        if i == letters[-1][-1]:
            letters[-1] += i
        else:
            letters.append(i)
    return ''.join([i if len(i) < 3 else f'{i[:2]}[{i[2:]}]' for i in letters][1:])

import re

def string_parse(string):
    if not type(string) == str:
        return "Please enter a valid string"
    
    return re.sub(r'(([a-zA-Z])\2)(\2+)', lambda x: x.group(1) + '[' + x.group(3) + ']', string)

def string_parse(string):
    #your code here
    if string == None or not isinstance(string,str)  : return "Please enter a valid string"
    if string == '' : return ''
    res = ''
    current_char = string[0]
    char_counter = 1
    for i in range(1, len(string)) :
        
        if current_char == string[i]:
            if char_counter + 1 <=2 :
                res += current_char
                char_counter +=1
                continue
            elif char_counter + 1 == 3 :
                res = res + current_char +  '[' 
                char_counter +=1
                continue
            elif char_counter + 1 > 3 :
                res += current_char
                char_counter +=1
        else :
            if char_counter > 2 :
                res = res + current_char + ']' 
            else :
                res += current_char
            current_char = string[i]    
            char_counter = 1
    res = res + current_char if char_counter <= 2 else res + current_char + ']'    
    return res

import re

def string_parse(string):
    if type(string) != str:
        return 'Please enter a valid string'
    for c in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
        string = re.sub(c + '{2}(' + c + '+)', c * 2 + '[\g<1>]', string) 
    return string

def string_parse(string):
    
    if not isinstance(string, str):
        return "Please enter a valid string"
    if len(string) ==0:
        return ""
    Di = {}
    text = ""
    f=0
    i = 0
    f2=0
    for i in range(len(string)-1):
        Di.setdefault(string[i], 0)
        text += string[i]
        Di[string[i]] +=1
        
        if not f and Di[string[i]] >=2 and string[i] == string[i+1]:
            text += "["
            f = 1
        if f and Di[string[i]] >=2 and string[i] != string[i+1]:
            text += "]"
            f = 0
            f2 =1
        if not f and string[i] != string[i+1]:
            Di[string[i]] =0
        
            
    text += string[-1]
    if f:
        text += "]"
    return text