Laura really hates people using acronyms in her office and wants to force her colleagues to remove all acronyms before emailing her. She wants you to build a system that will edit out all known acronyms or else will notify the sender if unknown acronyms are present.

Any combination of three or more letters in upper case will be considered an acronym. Acronyms will not be combined with lowercase letters, such as in the case of 'KPIs'. They will be kept isolated as a word/words within a string.

For any string: 

All instances of 'KPI' must become "key performance indicators" 
All instances of 'EOD' must become "the end of the day" 
All instances of 'TBD' must become "to be decided"
All instances of 'WAH' must become "work at home"
All instances of 'IAM' must become "in a meeting"
All instances of 'OOO' must become "out of office"
All instances of 'NRN' must become "no reply necessary"
All instances of 'CTA' must become "call to action"
All instances of 'SWOT' must become "strengths, weaknesses, opportunities and threats"
If there are any unknown acronyms in the string, Laura wants you to return only the message:
'[acronym] is an acronym. I do not like acronyms. Please remove them from your email.'
So if the acronym in question was 'BRB', you would return the string:
'BRB is an acronym. I do not like acronyms. Please remove them from your email.'
If there is more than one unknown acronym in the string, return only the first in your answer.

If all acronyms can be replaced with full words according to the above, however, return only the altered string.

If this is the case, ensure that sentences still start with capital letters. '!' or '?' will not be used.

Difficulty: EASY
Tags: String algorithms

Starter code:
def acronym_buster(message):
	

Solutions:
import re
from functools import reduce
_ACRONYMS = {'KPI': 'key performance indicators', 'EOD': 'the end of the day', 'EOP': 'the end of the day', 'TBD': 'to be decided', 'WAH': 'work at home', 'IAM': 'in a meeting', 'OOO': 'out of office', 'NRN': 'no reply necessary', 'CTA': 'call to action', 'SWOT': 'strengths, weaknesses, opportunities and threats'}
_ACRONYM_PATTERN = re.compile('\\b[A-Z]{3,}\\b')
_CAPITAL_PATTERN = re.compile('(?:\\. |^)([a-z])')
_CAPITAL_FIX = lambda match: '{}'.format(match.group(0).upper())

def acronym_buster(message):
	message = reduce(lambda msg, item: msg.replace(*item), _ACRONYMS.items(), message)
	try:
		acro = next(_ACRONYM_PATTERN.finditer(message)).group(0)
		return '{} is an acronym. I do not like acronyms. Please remove them from your email.'.format(acro)
	except StopIteration:
		return _CAPITAL_PATTERN.sub(_CAPITAL_FIX, message)


import re
ACRONYMS = {'KPI': 'key performance indicators', 'EOD': 'the end of the day', 'TBD': 'to be decided', 'WAH': 'work at home', 'IAM': 'in a meeting', 'OOO': 'out of office', 'NRN': 'no reply necessary', 'CTA': 'call to action', 'SWOT': 'strengths, weaknesses, opportunities and threats'}
ACRONYM_PATTERN = re.compile('\\b[A-Z]{3,}\\b')

def acronym_buster(message):
	new = ACRONYM_PATTERN.sub(lambda m: ACRONYMS.get(m.group(), m.group()), message)
	matched = ACRONYM_PATTERN.search(new)
	if matched:
		return f'{matched.group()} is an acronym. I do not like acronyms. Please remove them from your email.'
	return re.sub('[^.\\s][^.]*', lambda m: m.group()[0].upper() + m.group()[1:], new)


import re
D = {'KPI': 'key performance indicators', 'EOD': 'the end of the day', 'TBD': 'to be decided', 'WAH': 'work at home', 'IAM': 'in a meeting', 'OOO': 'out of office', 'NRN': 'no reply necessary', 'CTA': 'call to action', 'SWOT': 'strengths, weaknesses, opportunities and threats'}
acronym = re.compile('(?:^|(?<=\\W))[A-Z]{3,}(?=\\W|$)')
capital = re.compile('(?:^|(?<=\\.\\s))[a-z]')

def acronym_buster(message):
	try:
		return capital.sub(lambda w: w.group().upper(), acronym.sub(lambda w: D[w.group()], message))
	except Exception as e:
		return f'{str(e)[1:-1]} is an acronym. I do not like acronyms. Please remove them from your email.'


def acronym_buster(message):
	acronyms = {'CTA': 'call to action', 'EOD': 'the end of the day', 'IAM': 'in a meeting', 'KPI': 'key performance indicators', 'NRN': 'no reply necessary', 'OOO': 'out of office', 'SWOT': 'strengths, weaknesses, opportunities and threats', 'TBD': 'to be decided', 'WAH': 'work at home'}
	result = []
	for sentence in message.split('.'):
		tmp = []
		for (i, word) in enumerate(sentence.split()):
			if word.isupper() and len(word) > 2:
				try:
					word = acronyms[word]
				except KeyError:
					return '{} is an acronym. I do not like acronyms. Please remove them from your email.'.format(word)
			tmp.append(word[0].upper() + word[1:] if i == 0 else word)
		result.append(' '.join(tmp))
	return '. '.join(result).rstrip()


import re
know_acronyms = {'KPI': 'key performance indicators', 'EOD': 'the end of the day', 'TBD': 'to be decided', 'WAH': 'work at home', 'IAM': 'in a meeting', 'OOO': 'out of office', 'NRN': 'no reply necessary', 'CTA': 'call to action', 'SWOT': 'strengths, weaknesses, opportunities and threats'}

def acronym_buster(message):
	unknow_acronyms = []
	replace_or_store = lambda a: a in know_acronyms and know_acronyms[a] or unknow_acronyms.append(a)
	cleaned = re.sub('\\b[A-Z]{3,}\\b', lambda m: replace_or_store(m.group()), message)
	if unknow_acronyms:
		return '%s is an acronym. I do not like acronyms. Please remove them from your email.' % unknow_acronyms[0]
	else:
		return re.sub('(^|[.!?]) *\\w', lambda m: m.group().upper(), cleaned)


def acronym_buster(message):
	acronyms = {'KPI': 'key performance indicators', 'EOD': 'the end of the day', 'EOP': 'the end of the day', 'TBD': 'to be decided', 'WAH': 'work at home', 'IAM': 'in a meeting', 'OOO': 'out of office', 'NRN': 'no reply necessary', 'CTA': 'call to action', 'SWOT': 'strengths, weaknesses, opportunities and threats'}
	phrases = message.split('.')
	word_lists = []
	for phrase in phrases:
		word_lists.append(phrase.split(' '))
	for word_list in word_lists:
		for word in word_list:
			if len(word) >= 3 and word.isupper():
				if word in acronyms.keys():
					word_list[word_list.index(word)] = acronyms[word]
				else:
					return '{} is an acronym. I do not like acronyms. Please remove them from your email.'.format(word)
	phrases = []
	for word_list in word_lists:
		phrase = ' '.join(word_list)
		if len(phrase) > 0 and phrase[0].isalpha() and phrase[0].islower():
			phrase = phrase[0].upper() + phrase[1:]
		elif len(phrase) > 0 and phrase[0] == ' ' and phrase[1].islower():
			phrase = phrase[0] + phrase[1].upper() + phrase[2:]
		phrases.append(phrase)
	message = '.'.join(phrases)
	return message


import re

def acronym_buster(s):
	(d, temp) = ({'KPI': 'key performance indicators', 'EOD': 'the end of the day', 'TBD': 'to be decided', 'WAH': 'work at home', 'IAM': 'in a meeting', 'OOO': 'out of office', 'NRN': 'no reply necessary', 'CTA': 'call to action', 'SWOT': 'strengths, weaknesses, opportunities and threats'}, [])
	while re.search('\\b[A-Z]{3,}\\b', s):
		r = re.search('\\b[A-Z]{3,}\\b', s)
		arn = r.group()
		(start, end) = r.span()
		if arn in d:
			s = s[:start] + d[arn] + s[end:]
		else:
			return f'{arn} is an acronym. I do not like acronyms. Please remove them from your email.'
	return '. '.join([i[0].upper() + i[1:] for i in s.split('. ')])


import re
d = {'KPI': 'key performance indicators', 'EOD': 'the end of the day', 'TBD': 'to be decided', 'WAH': 'work at home', 'IAM': 'in a meeting', 'OOO': 'out of office', 'NRN': 'no reply necessary', 'CTA': 'call to action', 'SWOT': 'strengths, weaknesses, opportunities and threats'}

def acronym_buster(s):
	a = []

	def f(x):
		x = x[0]
		if x.isupper() and len(x) > 2:
			if x not in d:
				a.append(x)
			x = d.get(x, '')
		return x
	s = re.sub('^.|[!?.] .', lambda x: x[0].upper(), re.sub('\\w+', f, s))
	return f'{a[0]} is an acronym. I do not like acronyms. Please remove them from your email.' if a else s


import re

def acronym_buster(message):
	new_msg = message
	subs = {'KPI': 'key performance indicators', 'EOD': 'the end of the day', 'TBD': 'to be decided', 'WAH': 'work at home', 'IAM': 'in a meeting', 'OOO': 'out of office', 'NRN': 'no reply necessary', 'CTA': 'call to action', 'SWOT': 'strengths, weaknesses, opportunities and threats'}
	for item in re.findall('(?:\\w)*[A-Z]{3,}(?:\\w)*', new_msg):
		if item in subs:
			new_msg = new_msg.replace(item, subs[item])
		elif item.isupper():
			return '{} is an acronym. I do not like acronyms. Please remove them from your email.'.format(item)
	sentences = new_msg.split('. ')
	return '. '.join((s[:1].upper() + s[1:] for s in sentences))