The internet is a very confounding place for some adults. Tom has just joined an online forum and is trying to fit in with all the teens and tweens. It seems like they're speaking in another language! Help Tom fit in by translating his well-formatted English into n00b language.

The following rules should be observed:

- "to" and "too" should be replaced by the number 2, even if they are only part of a word (E.g. today = 2day)
- Likewise, "for" and "fore" should be replaced by the number 4
- Any remaining double o's should be replaced with zeros (E.g. noob = n00b)
- "be", "are", "you", "please", "people", "really", "have", and "know" should be changed to "b", "r", "u", "plz", "ppl", "rly", "haz", and "no" respectively (even if they are only part of the word)
- When replacing words, always maintain case of the first letter unless another rule forces the word to all caps.
- The letter "s" should always be replaced by a "z", maintaining case
- "LOL" must be added to the beginning of any input string starting with a "w" or "W"
- "OMG" must be added to the beginning (after LOL, if applicable,) of a string 32 characters^(1) or longer
- All evenly numbered words^(2) must be in ALL CAPS (Example: ```Cake is very delicious.``` becomes ```Cake IZ very DELICIOUZ```)
- If the input string starts with "h" or "H", the entire output string should be in ALL CAPS
- Periods ( . ), commas ( , ), and apostrophes ( ' ) are to be removed
- ^(3)A question mark ( ? ) should have more question marks added to it, equal to the number of words^(2) in the sentence (Example: ```Are you a foo?``` has 4 words, so it would be converted to ```r U a F00????```)
- ^(3)Similarly, exclamation points ( ! ) should be replaced by a series of alternating exclamation points and the number 1, equal to the number of words^(2) in the sentence (Example: ```You are a foo!``` becomes ```u R a F00!1!1```)

^(1) Characters should be counted After: any word conversions, adding additional words, and removing punctuation. Excluding: All punctuation and any 1's added after exclamation marks ( ! ). Character count includes spaces.

^(2) For the sake of this kata, "words" are simply a space-delimited substring, regardless of its characters. Since the output may have a different number of words than the input, words should be counted based on the output string.

Example: ```whoa, you are my 123 <3``` becomes ```LOL WHOA u R my 123 <3``` = 7 words

^(3)The incoming string will be punctuated properly, so punctuation does not need to be validated.

Difficulty: introductory

Starter code:

def n00bify(text):
	

Solutions:
import re
base = "too?|fore?|oo|be|are|you|please|people|really|have|know|s|[.,']".split('|')
noob = "2|4|00|b|r|u|plz|ppl|rly|haz|no|z|".split('|')

def n00bify(text):
    for b, n in zip(base, noob):
        keep_casing = lambda m: n.upper() if m.group().isupper() else n
        text = re.sub(b, keep_casing, text, flags=re.I)
    if not text: return ''
    if text[0] in 'hH': text = text.upper()
    if text[0] in 'wW': text = 'LOL ' + text
    if len(re.sub('[!?]', '', text)) >= 32: text = re.sub('\A(LOL |)', r'\1OMG ', text)
    text = re.sub('([?!])', r'\1' * len(text.split()), text).replace('!!', '!1')
    return ' '.join(w.upper() if i%2 else w for i, w in enumerate(text.split()))

import re

reg = ((r"[.',]", ""), (r"too?", "2"), (r"fore?", "4"), ("oo", "00"), ("be", "b"),
    ("are", "r"), ("you", "u"), ("please", "plz"), ("people", "ppl"), ("really", "rly"),
    ("have", "haz"), ("know", "no"), ("s", "z"))

def n00bify(stg):
    caps, lol = stg[0] in "hH", stg[0] in "wW"
    words = ["LOL"] if lol else []
    for w in stg.split():
        first = w[0].isupper()
        for p, r in reg:
            w = re.sub(p, r, w, flags=re.I)
        words.append(f"{w[0].upper()}{w[1:]}" if first else w)
    if len(re.sub(r"[?!]", "", " ".join(words))) > 31:
        words.insert(lol, "OMG")
    num, out = len(words), " ".join(w if i % 2 else w.upper() for i, w in enumerate(words, 1))
    out = re.sub(r"([?!])", r"\1"*num, out).replace("!!", "!1")
    return out.upper() if caps else out

def remove_punc(words):
    if "'" in words:
        temp_2 = words.partition("'")
        words = temp_2[0] + temp_2[-1]  
        
        words = remove_punc(words)
        
    if "." in words:
        temp_2 = words.partition(".")
        words = temp_2[0] + temp_2[-1]  
        
        words = remove_punc(words)
        
    if "," in words:
        temp_2 = words.partition(",")
        words = temp_2[0] + temp_2[-1]
        
        words = remove_punc(words)

    return words
    
def transw(words, old, new):
    temp_words = words.lower().replace(old, " "*len(old))
    result = ""
    threshold = 0
    for i in range(0, len(temp_words)):
        if temp_words[i] != " ":
            result = result + words[i]
        else:
            threshold += 1
            if threshold == len(old):           
                result = result + new
                threshold = 0
    return result
    


def n00bify(text):
    temp = text.split()
    result = ""
    for i in temp:
        
        i = transw(i, 'too', '2')
        i = transw(i, 'to', '2')
        i = transw(i, 'fore','4')
        i = transw(i, 'for', '4')
        i = transw(i, 'oo', '00')
        i = transw(i, 'be', 'b')
        i = transw(i, 'you', 'u')
        i = transw(i, 'are', 'r')
        i = transw(i, 'please', 'plz')
        i = transw(i, 'people', 'ppl')
        i = transw(i, 'really', 'rly')
        i = transw(i, 'have', 'haz')
        i = transw(i, 'know', 'no')
        i = transw(i, 's', 'z')
            
        i = remove_punc(i)
        
        if len(result) == 0:
            result = result + i
        else:
            result = result + " " + i
    
    if result[0] == "w" or result[0] == "W":
        result = "LOL" + " " + result
    if result[0] == "h" or result[0] == "H":
        result = result.upper()
    
            
    t_result = result.split()   
    
    count = 0
    for m in t_result:
        for n in m:
            if n == "!":
                break
            
            elif n.isalnum():
                count += 1
    
    count = count + len(t_result) - 1
    
    if count >= 32:
        if t_result[0] == "LOL":
            t_result = ["OMG"] + t_result
            t_result[0] = "LOL"
            t_result[1] = "OMG"
        else:
            t_result = ["OMG"] + t_result
    
    
    for k in range(0, len(t_result)):
        if "?" in t_result[k]:
            t_result[k] = t_result[k][:t_result[k].index("?")] + "?"*(len(t_result)-1) + t_result[k][t_result[k].index("?"):]
        
        if "!" in t_result[k]:
            add_in = ""
            for l in range(0, len(t_result)):
                if l % 2 == 0:
                    add_in = add_in + "!"
                else:
                    add_in = add_in + "1"
            
            t_result[k] = t_result[k][:t_result[k].index("!")] + add_in + t_result[k][t_result[k].index("!")+1:]        
                
    
    for i in range(0, len(t_result)):
        if i % 2 != 0:
            t_result[i] = t_result[i].upper()
    
                
    return " ".join(t_result)


def n00bify(text):
    import re
      
    replaced = re.sub(r'too|to|Too|To', '2', text)
    replaced = re.sub(r'fore|for|Fore|For|FORE', '4', replaced)
    replaced = re.sub(r'oo|Oo', '00', replaced)
    replaced = re.sub(r'be|Be', 'b', replaced)
    replaced = re.sub(r'are|Are', 'r', replaced)
    replaced = re.sub(r'you|You', 'u', replaced)
    replaced = re.sub(r'please|Please', 'plz', replaced)
    replaced = re.sub(r'people|People', 'ppl', replaced)
    replaced = re.sub(r'really|Really', 'rly', replaced)
    replaced = re.sub(r'have|Have', 'haz', replaced)
    replaced = re.sub(r'know|Know', 'no', replaced)
    replaced = re.sub(r's', 'z', replaced)
    replaced = re.sub(r'S', 'Z', replaced)
    replaced = re.sub(r"[.,']", '', replaced)
    if text[0] in ['W', 'w']:
        if len(replaced.replace('!',''))>=28:
            replaced = 'LOL OMG ' + replaced
        else:
            replaced = 'LOL ' + replaced
    else:
        if len(replaced.replace('!',''))>=32:
            replaced = 'OMG ' + replaced
    if text[0] in ['H', 'h']:
        replaced = replaced.upper()
    replaced = ' '.join([val.upper() if (i+1)%2==0 else val for i,val in enumerate(replaced.split())])
    num = len(replaced.split())
    replaced = replaced.replace('?','?'*num)
    s = ''.join(['!' if i%2==0 else '1' for i in range(num)])
    replaced = replaced.replace('!',s)
    return replaced


import re

def n00bify(text):
    replacementArray = [
    (r'too', "2"),
    (r'to', "2"),
    (r'fore', "4"),
    (r'for', "4"),
    (r'oo', "00"),
    (r'be', "b"),
    (r'are', "r"),
    (r'you', "u"),
    (r'please', "plz"),
    (r'people', "ppl"),
    (r'really', "rly"),
    (r'have', "haz"),
    (r'know', "no"),
    (r'\.', ""),
    (r',', ""),
    (r'\'', "")]
    
    firstLetterW = re.match(r'^w', text, flags=re.IGNORECASE)
    firstLetterH = re.match(r'^h', text, flags=re.IGNORECASE)
    
    for x in replacementArray:
        text = re.sub(x[0], x[1], text, flags=re.IGNORECASE)
        
    text = text.replace("s", "z")
    text = text.replace("S", "Z")
    
    count = 0
    for c in text:
        if (c.isalpha() or c.isdigit()) or c == " ":
            count += 1
        
    if firstLetterW  and count >= 28:
        text = "LOL OMG " + text
    elif firstLetterW :
        text = "LOL " + text
    elif count >= 32:
        text = "OMG " + text
        
    wordList = text.split()
    for i in range(len(wordList)):
        if i % 2 == 1 or firstLetterH:
            wordList[i] = wordList[i].upper()
    text = " ".join(wordList)
    
    text = text.replace("?", ("?" * len(wordList)))
    text = text.replace("!", exclamation(len(wordList)))
    
    return text
    
def exclamation(n):
    exclams = "!1" * (n // 2)
    if n % 2 == 1:
        exclams = exclams + "!"
    return exclams

def n00bify(text):
    noobtext = marks(caps(add(replace(text))))
    
    return noobtext
    
def replace(text):
    text = text.replace(",", "").replace(".", "").replace("'", "") \
                .replace("too", "2").replace("Too", "2").replace("TOo", "2").replace("TOO", "2") \
                .replace("to", "2").replace("To", "2").replace("TO", "2") \
                .replace("fore", "4").replace("Fore", "4").replace("FOre", "4").replace("FORE", "4") \
                .replace("for", "4").replace("For", "4").replace("FOr", "4").replace("FOR", "4") \
                .replace("oo", "00").replace("Oo", "00").replace("OO", "00") \
                .replace("be", "b").replace("are", "r").replace("you", "u").replace("please", "plz") \
                .replace("people", "ppl").replace("really", "rly").replace("have", "haz").replace("know", "no") \
                .replace("Be", "B").replace("Are", "R").replace("You", "U").replace("Please", "Plz") \
                .replace("People", "Ppl").replace("Really", "Rly").replace("Have", "Haz").replace("Know", "No") \
                .replace("s", "z").replace("S", "Z")
    return text
    
def add(text):
    if text.startswith('w') or text.startswith('W'):
        text = "LOL " + text
        
    if (len(text) - text.count("?") - text.count("!")) >= 32:
        if text.startswith('LOL'):
            text = text.replace("LOL", "LOL OMG")
        else:
            text = "OMG " + text
    return text
  
def caps(text):
    if text.startswith('h') or text.startswith('H'):
        text = text.upper()
    else:
        words = text.split()
        for i in range(1, len(words), 2):
            words[i] = words[i].upper()
        text = ' '.join(words)
    return text
    
def marks(text):
    num_words = len(text.split()) 
    text = text.replace("?", "?" * num_words)
    
    einself=""
    for i in range(0, num_words):
        add = "!" if (i % 2 == 0) else "1"    
        einself = einself + add
    text = text.replace("!", einself)
    
    return text