r/badcode • • Jul 06 '20

[deleted by user]

[removed]

175 Upvotes

56 comments sorted by

View all comments

2

u/healplease Jul 07 '20

Python, working perfectly of course!

import re

dictionary = {
	'.': '1', ',': '11', '?': '111', '!': '1111', '1': '11111',
	'a': '2', 'b': '22', 'c': '222', '2': '2222',
	'd': '3', 'e': '33', 'f': '333', '3': '3333',
	'g': '4', 'h': '44', 'i': '444', '4': '4444',
	'j': '5', 'k': '55', 'l': '555', '5': '5555',
	'm': '6', 'n': '66', 'o': '666', '6': '6666',
	'p': '7', 'q': '77', 'r': '777', 's': '7777', '7': '77777',
	't': '8', 'u': '88', 'v': '888', '8': '8888',
	'w': '9', 'x': '99', 'y': '999', 'z': '9999', '9': '99999',
	'\'': '*', '-': '**', '+': '***', '=': '****', '*': '*****',
	' ': '0', '0': '00', '#': '#',
}

def to_t9(s: str):
	capitals = re.findall('[A-Z]+', s)
	for capital in capitals:
		if len(capital) > 1:
			s = s.replace(capital, f'##{capital.lower()}#')
		if len(capital) == 1:
			s = s.replace(capital, f'#{capital.lower()}')

	result = '_'.join([dictionary[x] for x in s]).replace('#_#', "##")
	return result

print(to_t9('heLLO woRld'))  # => 44_33_##_555_555_666_#_0_9_666_#_777_555_3

1

u/pinguluk Jul 16 '20

That's really not bad