# Conjugation in Real Life

In @Adám's Dyalog APL Extended, the ⍢ (under) operator means conjugation: apply one function, then a second function, then the inverse of the first. It's fun to think of real-life actions in terms of conjugation:

A problem is transformed by g into another domain where it more readily solved by f , and then transformed back into the original domain. An example from real life is “under anesthetics”:

apply anesthetics
perform surgery
wake up from anesthetics

## Challenge

The inverse of a line g is "un" prepended to g, and vice versa. Define any line between s and its inverse, in that order, as being "under" s. For each line f in input in order:

• If f and its inverse both occur, do nothing
• If f is not "under" any other action, print f
• If f is "under" a line g, print f + " under " + g where + is concatenation.

### Input

A nonempty multiline string, or list of strings, etc., consisting of spaces and lowercase letters (you may instead use uppercase). Exactly one line will begin with "un"; and it will be the inverse of some other line. No line will be empty.

### Output

Output in the same format you took input in, or as allowed by Standard I/O.

### Test cases:

Input:
apply anesthetics
perform surgery
unapply anesthetics

Output:
perform surgery under apply anesthetics

Input:
unite asia
ite asia

Output:
establish the silk road under unite asia

Input:
desire a book
walk to store
take the book
pay for the book
unwalk to store

Output:
desire a book
take the book under walk to store
pay for the book under walk to store

Input:
drink excessively
undrink excessively

Output:
[empty string]

• I, too, have no output when I drink excessively – Stan Strum Dec 1 '18 at 21:06
• shouldn't it be "take the book and pay for the book under walk to store"? otherwise it's not clear that the transform is equivalent to the original... – Jonah Dec 2 '18 at 21:41
• @Jonah The idea is in an ideal world w="walk to store" and w^-1="unwalk to store" are inverses, so mathematically wfw^-1wg^-1 = wfgw^-1. – lirtosiast Dec 2 '18 at 22:33
• Ah fair enough, @lirtosiast. – Jonah Dec 2 '18 at 23:22

# Brachylog, 90 bytes

;Ṡ{hhH↰₂gB&hb~c[A,B,C]&tT;C↔↰U∧" under ",H,T;A↔↰,U|tT&hh,TgJ&hb;T↰Q∧J,Q|h}
~↰₃|↰₃
∧"un";?c


I made this while being under the impression that this could be recursive and multiple unders could be stacked. Probably not optimized. Also as this is bracylog, plumbing takes quite some bytes.

Try it online!

# Retina, 82 bytes

m{A^(un)?(.+)¶(?(1)|un)\2$^((un)?(.+)¶)(.+)¶((.+¶)*(?(2)|un)\3)$
$4 under$1$1$5


Try it online! Link includes test cases. Explanation:

m{


Run the whole program in multiline mode (so that ^ and $ match the beginning and end of individual lines) and repeat until there are no changes. A^(un)?(.+)¶(?(1)|un)\2$


Look for lines that might begin with un and are followed by a line that begins with un only if the previous line does not while the rest of the line is the same, and delete both lines. (This is a change of behaviour from Retina 0.8.2, which splits the lines before trying to match and therefore can never delete lines if the match needs to span more than one line at once.)

^((un)?(.+)¶)(.+)¶((.+¶)*(?(2)|un)\3)$ Look for lines that might begin with un, followed by at least one line, followed by a line that begins with un only if the original line does not while the rest of the line is the same. $4 under $1$1$5  Move the original line down one line, and also append it with under to the line just traversed. (Additional lines will be handled by the repeat.) # Python 2, 106 bytes s=input() x='' l=[] for i in s: if'un'==i[:2]or'un'+i in s:x=(' under '+i)*(not x) else:l+=[i+x] print l  Try it online! If the input can be a list from STDIN and the output be newline-separated, we then have this 94-byte solution: s=input() x='' for i in s: if'un'==i[:2]or'un'+i in s:x=(' under '+i)*(not x) else:print i+x  # JavaScript (Babel Node), 91 bytes Takes input as an array of strings in lowercase. Returns another array of strings. a=>a.flatMap(s=>s==r|'un'+s==r?(u=u?'':' under '+s,[]):s+u,u='',r=a.find(s=>/^un/.test(s)))  Try it online! ### Commented a => // a[] = input array a.flatMap(s => // for each string s in a[]: s == r | // if s matches the reference string 'un' + s == r ? ( // or its opposite: u = // update u: u ? // if u is not an empty string: '' // turn it to an empty string : // else: ' under ' + s, // set it to s with the ' under ' prefix [] // yield an empty array so that this entry is removed ) : // else: s + u, // yield s followed by u u = '', // initialize u to an empty string r = a.find(s => // initialize r ... /^un/.test(s) // ... to the string beginning with 'un' ) // ) // end of flatMap()  • Fail? – l4m2 Dec 1 '18 at 0:45 • @l4m2 Fail indeed. Now fixed. – Arnauld Dec 1 '18 at 1:01 # Clean, 147 bytes import StdEnv,Data.List a=inits;b=tails$l=hd[w++[e++[' under ':u]\\e<-z]++y\\i<-a l&t<-b l,w<-a i&u<-i,z<-a t&[x:y]<-b t|u==['un':x]||x==['un':u]]


Try it online!