10
\$\begingroup\$

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 with a (Note: Due to this rule Dog and dog are identical, and thus have a D.S. of -1)
  • If multiple pairs have the highest drunkenness similarity:
    1. Of the words, that can maximize drunkenness similarity, pick the one with the lowest index in the list
    2. Pair that word with the lowest-index word that maximizes drunkenness similarity

Examples

  1. 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
  2. I love fidget spinners (boring example)

    • S1: I<=>love (DS: 0) love I fidget spinners
    • S2: fidget<=>spinners (DS: 0) love I spinners fidget
  3. 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)
  4. 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
  5. 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

Let me know if there are more examples you want me to cover.

\$\endgroup\$
  • 8
    \$\begingroup\$ please change 'i love fid-get spinners' to 'i hate fid-get spinners' \$\endgroup\$ – Okx Jul 18 '17 at 17:45
  • 1
    \$\begingroup\$ "natural language" \$\endgroup\$ – HyperNeutrino Jul 18 '17 at 20:08
3
\$\begingroup\$

JavaScript - 286 279 bytes

f=(a,n=(a=a.split` `).length)=>{for(d=n>>1;w=-2,d--;){for(i=n;i--;)for(j=n;j--;)s=((a,f,v=0)=>{for(u=s=a!=f;(r=a[v])&&(t=f[v++]);s+=(p=a=>a.toLowerCase())(r)==p(t));return u*s-1})(a[i],a[j]),!(f[i]|f[j])&&s>=w&&(w=s,x=i,y=j);f[x]=f[y]=1,[a[x],a[y]]=[a[y],a[x]];}return a.join` `}

You can try it on JSFiddle.

\$\endgroup\$
  • \$\begingroup\$ Welcome (again) to PPCG :) Nice first submission! \$\endgroup\$ – HyperNeutrino Jul 18 '17 at 20:12
2
\$\begingroup\$

Python 3, 285 277 270 267 Bytes, not working

i=input().split();r=range;l=len;t=y=z=-1;n=str.lower
for p in r(l(i)):
 for q in r(p):
  b=min(l(i[p]),l(i[q]));x=0
  for s in r(b):
   if n(i[p][s])==n(i[q][s]):
    x=x+1
  if n(i[p])==n(i[q]):
   x=-1
  if x>t:
   t=x;y=p;z=q
i[y],i[z]=i[z],i[y]
print(" ".join(i))

I tried to make the output match the challenge, not the test cases, as a couple of the test cases contradict the challenge.

Edit: golfed the 'lower' thingy.

Edit: changed split(" ") to split()

Edit: I realised this doesn't actually complete all the thingys, and while I might come up with a full working answer, in the meantime I might as well add that this only completes one iteration.

\$\endgroup\$
  • 1
    \$\begingroup\$ You need to define either g = str.lower or g = lambda s: s.lower() which you then can use like this. \$\endgroup\$ – ბიმო Aug 9 '17 at 12:49
  • \$\begingroup\$ @Bruce Forte Thanks! This got rid of 8 bytes. (It only went down to 270 though because I also had to fix a thing to make it so that it would swap words even if the highest similarity was 0; this added a byte). \$\endgroup\$ – Harmless Aug 9 '17 at 13:07
  • 1
    \$\begingroup\$ No problem and welcome to PPCG! If you haven't seen already, these kind of posts are very helpful. Btw .split(' ') can be replaced by .split(). \$\endgroup\$ – ბიმო Aug 9 '17 at 13:17
  • \$\begingroup\$ Wait, which test cases did you change, and what was wrong with them? \$\endgroup\$ – geokavel Aug 9 '17 at 15:15
  • \$\begingroup\$ I don't have enough rep to change a post, but the apparent contradiction was for the first example - the given answer for 'drunk uncle needs your help' was 'your uncle help drunk needs' over 'your uncle needs drunk help'. Both of these answers swapped words with the same similarity score, and they both follow your given tie-breaker by using the first of the pair to decide which to swap, in the sense that they both start with the first word. (I realise now as I write this that the contradiction is not really that, but more an ambiguity). The given way to decide- running out of chars see next- \$\endgroup\$ – Harmless Aug 9 '17 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.