10
0
Drunk Uncle (sorry M.E. and AUS for region lock) is a fictional character from Saturday Night Live. He often confuses words for others that sound like them. For this challenge you need to convert normal speech into drunkspeak.
Algorithm
Translating to drunkspeak requires swapping the order of words in the text. Swapping is based on the drunkenness similarity of two words. Drunkenness similarity is defined as the number of letters that two words have occuring at the same indices. However, two identical words have a drunkenness similarity of -1. For example, tree
and friend
have a drunkenness similarity of 2, because they both have an 'r' at index 1, and an 'e' at index 3.
All you have to do is find the two words in the text with the highest drunkenness similarity, then swap them. Once you swap two terms, they do not move any more. Then you look at the remaining swappable terms and swap the two that have the highest drunkenness similarity. You keep doing this until you can't swap any more. Then you output (or return, for a function) the updated text.
Specifics
- For simplicity, input is a list of words consisting of characters in [A-Za-z]
- Every input contains at least one word
- Letter matching is case-insensitive:
A
matches witha
(Note: Due to this ruleDog
anddog
are identical, and thus have a D.S. of -1) - If multiple pairs have the highest drunkenness similarity:
- Of the words, that can maximize drunkenness similarity, pick the one with the lowest index in the list
- Pair that word with the lowest-index word that maximizes drunkenness similarity
Examples
Drunk Uncle needs your help
(title)- S1: Drunk<=>your (DS: 1)
your Uncle needs Drunk help
- S2: needs<=>help (DS: 1)
your Uncle help Drunk needs
- Output:
your Uncle help Drunk needs
- S1: Drunk<=>your (DS: 1)
I love fidget spinners
(boring example)- S1: I<=>love (DS: 0)
love I fidget spinners
- S2: fidget<=>spinners (DS: 0)
love I spinners fidget
- S1: I<=>love (DS: 0)
dog eat dog ear
- S1: eat<=>ear (DS: 2)
dog ear dog eat
- S2: dog<=>dog (DS: -1)
dog ear dog eat
(this step just formality)
- S1: eat<=>ear (DS: 2)
Let me tell you a story
- S1: Let<=>me (DS: 1)
me Let tell you a story
- S2: tell<=>you (DS: 0)
me Let you tell a story
- S3: a<=>story (DS: 0)
me Let you tell story a
- S1: Let<=>me (DS: 1)
Too many money and purple people
- S1: purple<=>people (DS: 4)
Too many money and people purple
- S2: many<=>money (DS: 2)
Too money many and people purple
- S3: Too<=>and (DS: 0)
and money many Too people purple
- S1: purple<=>people (DS: 4)
Let me know if there are more examples you want me to cover.
8please change 'i love fid-get spinners' to 'i hate fid-get spinners' – Okx – 2017-07-18T17:45:35.910
1"natural language" – HyperNeutrino – 2017-07-18T20:08:53.543