# The President's phone is broken

He is not very happy.

The only letters still working are uppercase ```E```, ```F```, ```I```, ```R```, ```U```, ```Y```.

An angry tweet is sent to the department responsible for presidential phone maintenance.


# Kata Task

Decipher the tweet by looking for words with known meanings.


* ```FIRE``` = *"You are fired!"*
* ```FURY``` = *"I am furious."*

If no known words are found, or unexpected letters are encountered, then it must be a *"Fake tweet."*

# Notes

* The tweet reads left-to-right.
* Any letters not spelling words ```FIRE``` or ```FURY``` are just ignored
* If multiple of the same words are found in a row then plural rules apply -
 * ```FIRE``` x 1 = *"You are fired!"*
 * ```FIRE``` x 2 = *"You and you are fired!"*
 * ```FIRE``` x 3 = *"You and you and you are fired!"*
 * etc...
 * ```FURY``` x 1 = *"I am furious."*
 * ```FURY``` x 2 = *"I am really furious."*
 * ```FURY``` x 3 = *"I am really really furious."*
 * etc...

# Examples

* ex1. FURYYYFIREYYFIRE = *"I am furious. You and you are fired!"*
* ex2. FIREYYFURYYFURYYFURRYFIRE = *"You are fired! I am really furious. You are fired!"*
* ex3. FYRYFIRUFIRUFURE = *"Fake tweet."*


----
DM :-)

Difficulty: introductory

Starter code:

def fire_and_fury(tweet):
	

Solutions:
import re
from itertools import groupby

CONFIG = {'FURY':   " really",
          'FIRE':   " and you",
          'FAKE':   "Fake tweet.",
          'FURY_f': "I am{} furious.",
          'FIRE_f': "You{} are fired!"}

def fire_and_fury(tweet):
    if re.findall(r'[^FURYIE]', tweet): return CONFIG['FAKE']
    lst = []
    for k,g in groupby(re.findall(r'FURY|FIRE', tweet)):
        lst += [ CONFIG[k+"_f"].format(CONFIG[k] * (len(list(g)) - 1)) ]
    return ' '.join(lst) or CONFIG['FAKE']

def fire_and_fury(tweet):
    if not all(c in 'EFIRUY' for c in tweet): return 'Fake tweet.'
    s = [0]
    for i in range(0,len(tweet)-3):
        if tweet[i:i+4] == 'FIRE':
            if s[-1] > 0: s[-1] += 1
            else: s.append(1)
        elif tweet[i:i+4] == 'FURY':
            if s[-1] < 0: s[-1] -= 1
            else: s.append(-1)
    return 'Fake tweet.' if len(s) == 1 else ' '.join(['You' + ' and you' * (s[i]-1) + ' are fired!' if s[i] > 0 else 'I am' + ' really' * (-s[i]-1) + ' furious.' for i in range(1,len(s))])

from itertools import groupby
import re


def fire_and_fury(tweet):
    if set(tweet) > set("EFIRUY") or ("FIRE" not in tweet and "FURY" not in tweet):
        return "Fake tweet."
    return " ".join(translate(group) for group in groupby(re.findall(r"FIRE|FURY", tweet)))


def translate(group):
    word, repeat = group[0], len(tuple(group[1])) - 1
    base, more = {"FURY": ("I am {}furious.", "really "), "FIRE": ("You {}are fired!", "and you ")}[word]
    return base.format(more * repeat)


from re import findall
from itertools import groupby


def fire_and_fury(tweet):
    s, match = [], findall(r"FIRE|FURY", tweet)
    if not match or not all([x in ["E", "F", "I", "R", "U", "Y"] for x in tweet]):
        return "Fake tweet."
    for i in groupby(match):
        s.append((i[0], len(list(i[1]))))
    s = translated(s)
    return " ".join(s)


def translated(s):
    string = []
    for i in s:
        if i[0] == "FIRE":
            string.append("You "+ "and you " * (i[1] - 1) + "are fired!")
        elif i[0] == "FURY":
            string.append("I am " + "really " * (i[1] - 1) + "furious.")
    return string

import itertools
import re

MEANINGS = {
    'FIRE': [lambda i: "You are fired!", lambda i: f"You{' and you' * i} are fired!"],
    'FURY': [lambda i: "I am furious.", lambda i: f"I am{' really' * i} furious."],
}

def fire_and_fury(tweet):
    if re.search('[^EFIRUY]', tweet):
        tweet = ''
    return ' '.join((
        MEANINGS[key][n > 1](n-1)
        for key, grp in itertools.groupby(re.findall('FIRE|FURY', tweet))
        for n in [sum(1 for _ in grp)]
    )) or 'Fake tweet.'

import re
from itertools import groupby

def fire_and_fury(tweet):
    text = ' '.join('I am{} furious.'.format(' really' * ~-len(list(g))) if k == 'FURY' else 'You{} are fired!'.format(' and you' * ~-len(list(g))) for k, g in groupby(re.findall('FIRE|FURY', tweet)))
    return text if re.fullmatch('[EFIRUY]+', tweet) and text else 'Fake tweet.'

import re
def fire_and_fury(tweet):
    if re.search(r'[^EFIRUY]',tweet):
        return 'Fake tweet.'
    tweet = "".join(re.findall(r'FIRE|FURY',tweet))
    return re.sub(r'(FIRE)+|(FURY)+',change,tweet).strip(" ") if tweet else 'Fake tweet.'
    
    
def change(m):
    m = m.group()
    if 'FURY' in m:
        return "I am "+"really "*(m.count("FURY")-1)+ "furious." + " "
    if 'FIRE' in m:
        return "You "+"and you "*(m.count("FIRE")-1) + "are fired!" + " "

from itertools import groupby
from re import findall

F = {'FIRE':lambda k: 'You' + ' and you' *(k-1) + ' are fired!', 
     'FURY':lambda k: 'I am' + ' really' *(k-1) + ' furious.'}
     
def fire_and_fury(tweet):
    r = [F[w](sum(1 for _ in g)) for w, g in groupby(findall('FIRE|FURY', tweet))]

    return ' '.join(r) if r and all(c in 'EFIRUY' for c in tweet)else 'Fake tweet.'    

from re import findall
from itertools import groupby

def fire_and_fury(tweet):
    m=findall('FIRE|FURY',tweet)
    if set('EFIRUY')<set(tweet):m=[]
    s=[]
    for x,c in groupby(m):
        l=len(list(c))
        if x=='FURY':
            s+=['I am '+'really '*(l-1)+'furious.']
        if x=='FIRE':
            s+=['You '+'and you '*(l-1)+'are fired!']
    r=' '.join(s)
    
    return r or "Fake tweet."

import re
def fire_and_fury(tweet):
    result = []
    if tweet.count('FIRE') == 0 and tweet.count('FURY') == 0 or any((x) not in 'FURYIE' for x in tweet):
        return "Fake tweet."
    tweet = re.findall('FIRE|FURY', tweet)

    i = 0
    while(i < len(tweet)):
        if tweet[i] == 'FIRE':
            count = 0
            while i < len(tweet) and tweet[i] == 'FIRE':
                count += 1
                i += 1
            i -= 1
            result.append('You' + ' and you' * (count-1) + ' are fired!')
            
        if tweet[i] == 'FURY':
            count = 0
            while i < len(tweet) and tweet[i] == 'FURY':
                count += 1
                i += 1
            i -= 1
            result.append('I am' + ' really' * (count-1) + ' furious.')
        i += 1
    return ' '.join(result)