In this kata you will have to modify a sentence so it meets the following rules:

convert every word backwards that is:

    longer than 6 characters

  OR

    has 2 or more 'T' or 't' in it

convert every word uppercase that is:

    exactly 2 characters long

  OR

    before a comma

convert every word to a "0" that is:

    exactly one character long
 

 NOTES: 

      Punctuation must not be touched. if a word is 6 characters long, and a "." is behind it,
      it counts as 6 characters so it must not be flipped, but if a word is 7 characters long,
      it must be flipped but the "." must stay at the end of the word.
      -----------------------------------------------------------------------------------------
      Only the first transformation applies to a given word, for example 'companions,'
      will be 'snoinapmoc,' and not 'SNOINAPMOC,'.
      -----------------------------------------------------------------------------------------
      As for special characters like apostrophes or dashes, they count as normal characters, 
      so e.g 'sand-colored' must be transformed to 'deroloc-dnas'.

Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code:
def spin_solve(sentence):
	

Solutions:
import re

def spiner(s, p):
	return (s[::-1] if len(s) > 6 or s.lower().count('t') > 1 else s.upper() if len(s) == 2 or p == ',' else '0' if len(s) == 1 else s) + p

def spin_solve(sentence):
	return re.sub("((?:\\w|['-])+)(\\W)?", lambda m: spiner(m.group(1), m.group(2) or ''), sentence)


from functools import partial
from re import compile
s = "[\\w'-]"
x = f'(?<!{s})'
y = f'(?!{s})'
conv1 = partial(compile(f'{s}{{7,}}|{s}*[tT]{s}*[tT]{s}*').sub, lambda x: x.group()[::-1])
conv2 = partial(compile(f'{x}({s}{s}{y}|{s}{{1,6}}(?=,))').sub, lambda x: x.group().upper())
conv3 = partial(compile(f'{x}\\w{y}(?!,)').sub, '0')

def spin_solve(sentence):
	return conv3(conv2(conv1(sentence)))


import re

def spin_solve(sentence):

	def f(m):
		(s, punc) = m.groups()
		return (s[::-1] if len(s) > 6 or s.lower().count('t') >= 2 else s.upper() if len(s) == 2 or punc == ',' else '0' if len(s) == 1 else s) + punc
	return re.sub("([-'a-zA-Z]+)([,.?!]?)", f, sentence)


spin_solve = lambda s: ' '.join([[[[i, '0'][len(i) == 1], i.upper()][len(i) - (i[-1].isalpha() ^ 1) == 2 or i[-1] == ','], i[:len(i) if i[-1].isalpha() else -1][::-1] + [i[-1], ''][i[-1].isalpha()]][len(i) - (i[-1].isalpha() ^ 1) > 6 or i.lower().count('t') > 1] for i in s.split()])


import re
W = lambda Q: ''.join(reversed(Q)) if 6 < len(Q) and ',' != Q[-1] or 1 < Q.upper().count('T') else '0' if 1 == len(Q) else Q.upper() if 2 == len(Q) or ',' == Q[-1] else Q
spin_solve = lambda Q: re.sub("[\\w'-]{1,6},|[\\w'-]+", lambda S: W(S.group()), Q)


def spin_solve(sentence):
	r = []
	for w in sentence.split(' '):
		l = len(w.rstrip(',.'))
		if l > 6 or w.lower().count('t') >= 2:
			s = w[::-1]
			if not s[0].isalpha():
				s = s[1:] + s[0]
			r.append(s)
		elif l == 2 or w[-1] == ',':
			r.append(w.upper())
		elif l == 1:
			r.append('0')
		else:
			r.append(w)
	return ' '.join(r)


def spin_solve(sentence):
	r = []
	for s in sentence.split():
		(p, c) = (s.endswith('.'), s.endswith(','))
		s = s[:-1] if p or c else s
		if len(s) > 6 or s.lower().count('t') > 1:
			r.append(s[::-1] + ('.' if p else ',' if c else ''))
		elif len(s) == 2 or c:
			r.append(s.upper() + ('.' if p else ',' if c else ''))
		elif len(s) == 1:
			r.append('0' + ('.' if p else ',' if c else ''))
		else:
			r.append(s + ('.' if p else ',' if c else ''))
	return ' '.join(r)


def spin_solve(sentence):
	s = ''
	w = ''
	for ot in sentence:
		if ot.isalpha() or ot in "'-'":
			w += ot
		else:
			if len(w) > 6 or w.upper().count('T') > 1:
				s += w[::-1]
			elif len(w) == 2 or ot == ',':
				s += w.upper()
			elif len(w) == 1:
				s += '0'
			else:
				s += w
			w = ''
			s += ot
	return s


def spin_solve(sentence):
	nu_list = []
	words = sentence[:-1].split()
	for x in words:
		if x.endswith(',') and len(x) > 7:
			x = x[:-1]
			nu_list.append(f'{x[::-1]},')
		elif x.endswith(',') and len(x) <= 7 or len(x) == 2:
			nu_list.append(x.upper())
		elif len(x) > 6 or x.count('t') >= 2:
			nu_list.append(x[::-1])
		elif len(x) == 1:
			nu_list.append('0')
		elif len(x) == 3 or len(x) <= 6:
			nu_list.append(x)
	return f"{' '.join(nu_list)}."


import re

def spin_solve(sentence):
	ans = sentence.split()
	for (idx, val) in enumerate(ans):
		t = re.sub('[^\\w-]', '', val)
		if len(t) > 6 or t.lower().count('t') > 1:
			ans[idx] = t[::-1] + val[len(t):]
		elif len(t) == 2 or val.endswith(','):
			ans[idx] = val.upper()
		elif len(t) == 1:
			ans[idx] = '0'
	return ' '.join(ans)