From 39082ef8b3e858de42cf135f28bd7f8a8e1e5bfd Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 24 Aug 2019 19:02:52 -0500 Subject: [PATCH] icpc 2018 problem D solve --- .../D/input | 4 ++ .../D/main.py | 59 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 icpc/2018 ICPC East Central North America Regional Contest/D/input create mode 100644 icpc/2018 ICPC East Central North America Regional Contest/D/main.py diff --git a/icpc/2018 ICPC East Central North America Regional Contest/D/input b/icpc/2018 ICPC East Central North America Regional Contest/D/input new file mode 100644 index 0000000..b77188c --- /dev/null +++ b/icpc/2018 ICPC East Central North America Regional Contest/D/input @@ -0,0 +1,4 @@ +3 +Oh say can you see +I do not understand why you are so cranky just because Karel won +Formation \ No newline at end of file diff --git a/icpc/2018 ICPC East Central North America Regional Contest/D/main.py b/icpc/2018 ICPC East Central North America Regional Contest/D/main.py new file mode 100644 index 0000000..cb4ca47 --- /dev/null +++ b/icpc/2018 ICPC East Central North America Regional Contest/D/main.py @@ -0,0 +1,59 @@ +import os, sys + +mappings = { + '@' : ['at'], + '&' : ['and'], + '1' : ['one', 'won'], + '2' : ['to', 'too', 'two'], + '4' : ['for', 'four'], + 'b' : ['bea', 'be', 'bee'], + 'c' : ['sea', 'see'], + 'i' : ['eye'], + 'o' : ['oh', 'owe'], + 'r' : ['are'], + 'u' : ['you'], + 'y' : ['why'] +} + +# gets the largest and smallest possible subs +minSub = min([min(list(map(len, vals))) for vals in mappings.values()]) +maxSub = max([max(list(map(len, vals))) for vals in mappings.values()]) + +# Gets the token ('are' -> 'r') for a sub, case sensitive +def getToken(sub): + for key in mappings.keys(): + if sub in mappings[key]: + return key + +def processWord(word): + processed = [False] * len(word) + # For every character in the word + index = 0 + while index < len(word): + possible = [] + for length in range(minSub, maxSub + 1): + if index + length > len(word): + continue + sub = word[index:index + length] + token = getToken(sub.lower()) + if token: + # Sometimes mulitple tokens can appear for the same one, we just select the one that is longest + possible.append((token, len(sub))) + if possible: + select = max(possible, key=lambda item : item[1]) + # print(index, select, word[index:index + select[1]]) + word = word[:index] + select[0] + word[index + select[1]:] + # word[index:index + select[1]] = select[0] + index += 1 + return word + +def processLine(line): + return ' '.join([processWord(word) for word in line.split(' ')]) + +def main(): + path = os.path.join(sys.path[0], 'input') + data = open(path, 'r').read().split('\n')[1:] + print('\n'.join(map(processLine, data))) + +if __name__ == "__main__": + main() \ No newline at end of file