[Question]
  [
Whenever you make a move on a Rubik's Cube, there is a reverse move which undoes the first move. Because of this, every algorithm (set of moves) has a reverse algorithm which undoes the first algorithm.
The goal of this challenge is to find the reverse of a given algorithm.
# Specification:
The input consists of an array of individual moves. Each move is a string of length 1 or 2. Of course, you can use whatever input format makes the most sense in your language. Each move consists of the structure `X` or `X'` or `X2`, where `X` is an uppercase or lowercase letter.
To reverse `X`, simply replace it with `X'`. Likewise, `X'` becomes `X`. `X2` on the other hand does not get changed.
To create the output, reverse each move, and then reverse the array.
# Examples (strings separated by spaces):
`R` => `R'`
`D U'` => `U D'`
`S T A C K` => `K' C' A' T' S'`
`A2 B2` => `B2 A2`
# Scoring:
This is code-golf, so the fewest amount of bytes win. Standard loopholes are not allowed.
  
[Answer]
# [Python 2](https://docs.python.org/2/), ~~71~~ ~~57~~ ~~54~~ 53 bytes
*-15 bytes thanks to ovs! -3 bytes thanks to Rod.*
```
lambda l:[i.strip("'")+" '"[len(i):]for i in l[::-1]]
```
[Try it online!](https://tio.run/##RY49C8IwFEX3/IpHltdiFcwY6FB1c/Njim@o2NJATEOSxV8fkyL1DYfL4cJ97hOn2Yo0to9k@vfz1YORSu9C9NpVHHm94YBcmcFWupY0zh40aAtGSbndE6U4hBigBcUgn8ILUvOLJ2yA35Gv4poF3gq6gmPB@d/vRBEHgcSIsXVqWZBLx3ltI4z5lfQF "Python 2 – Try It Online")
## String I/O, 70 bytes
```
lambda s:' '.join(i.strip("'")+"'"*(len(i)<2)for i in s.split()[::-1])
```
[Try it online!](https://tio.run/##PY27DsIwFEP3fIWV5SY8KpExokOBjY3HVDqAaMVFJY2aLHx9iArCg2XZ0rF/x8fgTOrKS@qvr9v9imAJVDwHdoqLEEf2SpLU82wz1be51Wuju2EEgx1CEXzPUena2uWq0Sm2IQaUqAWy6ECLKcgdziS/mY44ocIW@99IlcHGkGiE@IMnjp1mP7KL6PJz@gA "Python 2 – Try It Online")
[Answer]
# [V](https://github.com/DJMcMayhem/V), ~~13~~ 10 bytes
```
æGÇä/á'Ó''
```
[Try it online!](https://tio.run/##K/v///Ay98Pth5foH16ofniyuvr//8FcIUZcjupczlze6gA "V – Try It Online")
3 bytes saved thanks to @nmjmcman pointing out my favorite feature. Explanation:
```
æG          " Revere the order of every line
  Ç         " On every line not containing...
   ä/       " a digit:
     á'     "   Append an '
       Ó    "   Remove every instance on this line
        ''  "     Of two single quotes
```
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), ~~27~~ 26 bytes
```
\w
$&'
''
'2'
2
O$^`.'?2?
```
[Try it online!](https://tio.run/##K0otycxL/K@q4Z7wP6acS0VNnUtdnYtL3Uidy4jLXyUuQU/d3sj@//8gLpdQda7gEEdnby5HIycjAA "Retina 0.8.2 – Try It Online") Link includes test cases. Explanation: The first stage adds an apostrophe after every alphanumeric. This results in double apostrophes (with or without an inclusive 2) which need to be removed. The final stage reverses the moves.
[Answer]
# JavaScript (ES6), 45 bytes
```
s=>s.map(([a,b])=>b?+b?a+b:a:a+"'").reverse()
```
Shortest solution is to take Array IO. Simple and appropriate use of argument destruction.
String output is +8 bytes for `.join` ``.
## String input, Array output: 69 bytes
```
(s,k=[])=>s.replace(/\S\S?/g,([a,b])=>k.unshift(b?+b?a+b:a:a+"'"))&&k
```
```
f=
(s,k=[])=>s.replace(/\S\S?/g,([a,b])=>k.unshift(b?+b?a+b:a:a+"'"))&&k
;
console.log(["R", "D U'", "S T A C K", "A2 B2"].map(e => `${e} => ${f(e)}`));
```
```
<textarea oninput="out.value=(f(this.value)||[]).join` `" placeholder="input here"></textarea>
<textarea id="out" readonly></textarea>
```
[Answer]
# JavaScript (ES6), 46 bytes
Takes input as an array of moves.
```
a=>a.map(m=>m[1]?+m[1]?m:m[0]:m+"'").reverse()
```
---
## Test it
Enter a comma separated list of moves.
```
o.innerText=(f=
a=>a.map(m=>m[1]?+m[1]?m:m[0]:m+"'").reverse()
)((i.value="S,T,A,C,K").split`,`);oninput=_=>o.innerText=f(i.value.split`,`)
```
```
<input id=i><pre id=o>
```
---
## Explanation
```
a=>
```
Anonymous function taking the array of moves as an argument via parameter `a`.
```
a.map(m=>                       )
```
Map over the array, passing each string through a function, where `m` is the current string.
```
 m[1]?
```
Check if the string contains a second second character (`"'"` or `"2"`).
```
+m[1]?
```
If it does try to cast that character string to an integer. If the string is `"2"`, it becomes `2`, which is truthy. If the string is `"'"`, it becomes `NaN`, which is falsey.
```
m
```
If the previous test is truthy, simply return `m`.
```
:m[0]
```
Otherwise, return the first character of `m`.
```
:m+"'"
```
If the string does not contain a second character then return `m` appended with a `'`.
```
.reverse()
```
Reverse the modified array.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ḟ;ċ?”'ḣ2)Ṛ
```
A monadic link taking an returning a list of lists of characters (an "array" of "strings").
**[Try it online!](https://tio.run/##y0rNyan8///hjvnWR7rtHzXMVX@4Y7GR5sOds/4fbvf@/z9aKUJJR0EpQh1MGinFAgA "Jelly – Try It Online")** (The footer avoids smashing the output, displaying the list split with spaces.)
### How?
```
ḟ;ċ?”'ḣ2)Ṛ - Link: list of lists of characters             e.g. ["F'", "D2" , "R"]
        )  - for each turn instruction:
    ”'     -   literal "'" character
   ?       -   if:
  ċ        -     count (number of "'" in the instruction) i.e.:  1   , 0    , 0
ḟ          -   then: filter out                                  "F"
 ;         -   else: concatenate                                       "D2'", "R'"
      ḣ2   -   head to index 2                                   "F" , "D2" , "R'"
         Ṛ - reverse                                            ["R'", "D2" , "F"]
```
[Answer]
# [Python](https://docs.python.org/), ~~51~~ 48 bytes
```
lambda a:[(v+"'")[:2-("'"in v)]for v in a[::-1]]
```
An unnamed function taking and returning lists of strings.
**[Try it online!](https://tio.run/##JcqxCsMgFEbhPU/x4@KVkkAdhax9gEIm62BppZZUJbkR@vQ2oduB75Qvv3LSLYy3NvvP/eHhjaV6ElIoa3RPe8SEqlzICyr29taY/uxc4@fKGCGumCQuWgxrmSOT6vLGZTso0PGoriwxMSTk8M4x0d9V@wE "Python 2 – Try It Online")**
Reverses the input list with `a[::-1]`; appends a `'` to *every* entry with `v+"'"`; heads each one to 1 or 2 characters depending on whether the original had a `'` in or not with `[:2-("'"in v)]`.
[Answer]
# [Python 3](https://docs.python.org/3/), ~~91~~ ~~89~~ ~~72~~ ~~70~~ ~~69~~ 65 bytes
```
lambda s:[i[0]+(len(i)-2and"'"or"2"*("2"==i[1]))for i in s[::-1]]
```
[Try it online! (With testcases)](https://tio.run/##RYvLCsIwEADP7VcsC9JEq9jqqdCDj5s3H6cYJNJUV2pakir49TWtBxdmmR3Y5tPea7PoyvzcVep5LRS4TJCYywmrtGHEp6kyBUZYW0xxzPzKcxKJ5LysLRCQASeybJpI2fXl0herzE2zJc/CgEyT67eqmJdXyzgPg8aSaVkkYORAgtVvbZ0ugBz8UjRiGAPOHjWZ/o3H/7Mcgp9O4B5lKHCLMZ6iQQ9ej56VZ@PZDXWVel2nKL8 "Python 3 – Try It Online")
Apparantly you don't need to take input and output as strings, so a 69 byte solution is possible
[Answer]
# [Haskell](https://www.haskell.org/), 43 bytes
```
map f.reverse
f[x]=x:"'"
f[x,c]=x:[c|c>'1']
```
[Try it online!](https://tio.run/##y0gszk7NyflfrauskJOYl16amJ6q4BwQoKCsW8uVbhvzPzexQCFNryi1LLWoOJUrLboi1rbCSkldCcTUSQZxopNrku3UDdVjgWoz8xRsFVLyuRQUCooy80oUVBTSFaKVgpRi0URclHSUQtUxhIOBwiFA7AjEzkDsjaHC0Qgo7GSkFPsfAA "Haskell – Try It Online") Declares an anonymous function `map f.reverse`. Bind to `g` and use as `g["S","T","A","C","K"]`.
[Answer]
# [PHP](https://php.net/), 81 bytes
```
<?foreach(array_reverse($_GET)as$v)$r[]=$v[1]?$v[1]<2?$v[0]:$v:"$v'";print_r($r);
```
[Try it online!](https://tio.run/##K8go@G9jX5BRwJWWX5SamJyhER2tFKQUqxOt5KKkoxSqDmYGA5khQOwIxM5A7A0WdTQCMp2MlGJjE4tV4t1dQzSrS/OKU0s0VIo0rf/DzEssKkqsjC9KLUstKk7VgKgDqi/TVCmKjrVVKYs2jLUHkzZGINog1kqlzEpJpUxdybqgKDOvJL4IYl7tfwA "PHP – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 13 bytes
```
RεÐ1èQ''si«ëK
```
[Try it online!](https://tio.run/##MzBNTDJM/f8/6NzWwxMMD68IVFcvzjy0@vBq7///o5VclHQUlELVQaSbkVIsAA "05AB1E – Try It Online")
### Explanation
```
RεÐ1èQ''si«ëK
R             # Reverse input array
 ε            # Map over each element...
  Ð1èQ         # Is the second char in the element the first one? (Uses the fact that in python indexing loops)
      ''       # Push '
        s      # Swap top items of stack
         i     # If the question above is true...
          «     # Concatenate
           ë   # Else
            K   # Push element without '  
```
[Answer]
# J, 25 bytes
J handles this one well, other than the unfortunate escape sequence needed to represent a single quote:
```
|.,&''''`}:@.(''''={:)&.>
```
We need to represent the list using boxed data, since it's a mix of one and two character items, hence:
* `&.>` - "under unbox", which means unbox each element, perform the operation that follows (ie, the symbols explained below) and then rebox when done
* `(''''={:)` "if the 2nd character is a single quote"....
* `@.` (J's agenda verb, a kind of generalized ternary statement, or a case statement) "then perform the 2nd item on the agenda list, otherwise perform the first"
* `}:` (the 2nd item on the agenda list), "remove the last character", ie, the single quote
* ` (J's tie verb) You can think of this as the agenda item separator
* `,&''''` (first item on agenda list) "add a single quote to the end"
* `|.` "reverse"
[Try it online!](https://tio.run/##y/pfbKtnY60Xb6igrhCq4KLgo66uEKGu4Oekp1CcmFuQk6qQkliS@D81OSNfoZgLTNXo6aipA0FCrZWDngaIZVttpammZ6dQ/P8/AA "J – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 10 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
Rε''«¤ºK2£
```
I/O as a list of moves.
This is a subset of [my answer for the *Expand a Rubik's Cube Commutator* challenge](https://codegolf.stackexchange.com/a/240973/52210).
[Try it online](https://tio.run/##yy9OTMpM/f8/6NxWdfVDqw8tObTL2@jQ4v//o5VclHSUQtWBRJCRUiwA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfeX/oHNb1dUPrT605NAub6NDi//X6vyPjlYKUorVUYhWclHSUQpVh7CDgewQIHYEYmcg9oYIOxoB2U5GSrGxAA).
**Explanation:**
```
R           # Reverse the (implicit) input-list
 ε          # Map over its strings:
  ''«       #  Append a "'" to each string
     ¤      #  Push the last character (the "'") without popping the string
      º     #  Mirror it to "''"
       K    #  Remove all "''" from the string
        2£  #  Only keep the first two characters of the string
            # (after which the result is output implicitly)
```
[Answer]
# [R](https://www.r-project.org/), 51 bytes
```
function(u)sub("((2)|')'","\\2",paste0(rev(u),"'"))
```
[Try it online!](https://tio.run/##K/qfpmCjq/A/rTQvuSQzP0@jVLO4NElDSUPDSLNGXVNdSUcpJsZISacgsbgk1UCjKLUMqEJHSV1JU/N/mkayhpKjko6Cko8RiHRSB5EuYDkA "R – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), 44 bytes
```
->a{a.reverse.map{|i|i[1]?i.tr(?',''):i+?'}}
```
[Try it online!](https://tio.run/##Xc3NCoJAFAXgfU9xmBZXyYRmGaiMunPnz8pcGChMUMmoQajPbgMRMl04u/Odq8bre229y3r066l2VfNqVN@497qbZjnL8lQF0h2UFZBDZJ/lIaBlWbtx6MFS/M7zsZ/asmQpqyr39pQPAi2wUrLZ7luOUdBfOWYOK8gUBeLNZMghECHZTKZNriN0Ip3E5AkhIghCTsi2IcERcvO54FqH3OS6JLjN1g8 "Ruby – Try It Online")
[Answer]
# Java 8, ~~141~~ ~~128~~ ~~126~~ 115 bytes
```
a->(new StringBuffer(a).reverse()+"").replaceAll(".","'$0").replace("'''","").replaceAll("('(2)'|('))(.)","$4$2$3")
```
Takes input as single `String` without spaces (i.e. `RUR'URU2R'U`).
[Try it online.](https://tio.run/##hY9BS8QwEIXv@yuGUMgE3SDR26Kw6k3cQ9aexEPMppI1m5YkrYj2t9esBgRBCknIvPlmeG@vBrVsO@P3u9dJOxUj3CvrPxYA1icTGqUNbI4lwDYF619AY/kotsr6mG8@MalkNWzAwyVManmF3ryVkeu@aUxAxXgwgwnRIDsh5Fh1Lu9fO4eEk1NCq7NfFQmlNIt/OKQoGP1EyhhylvvVRSWqc8Km1Y@Rrn922UjxM7R2B4ecqJh@fFKspHmPyRx42yfe5U5yHj3XSCRh37n@J25rOstsH9Y3d/ObhBSzkKwlrWUt8lvYcTFOXw)
**Explanation:**
```
a->                      // Method with String as both parameter and return-type
  (new StringBuffer(a).reverse()+"")
                         //1  Reverse the input
  .replaceAll(".","'$0") //2  Prepend an apostrophe before each character
  .replace("'''","")     //3  Remove all occurrences of three adjacent apostrophes
  .replaceAll("('(2)'|('))(.)","$4$2$3")
                         //4  Replace '2'C with C2 OR 'C with C' (C is any character)
```
Example of the result after each step, with `RUR'URU2R'U` as given input:
1. `U'R2URU'RUR`
2. `'U'''R'2'U'R'U'''R'U'R`
3. `'UR'2'U'R'UR'U'R`
4. `U'RU2R'U'RU'R'`
[Answer]
# [Scala](http://www.scala-lang.org/), 47 bytes
Golfed version. [Try it online!](https://tio.run/##VY8xawMxDIV3/wrhxTYNN2Q88MG13ZouTTO1oagXO1VwLsE2gRLy26@Wh3LR8NB7iI@nNGDA6fR9cEOGV6QRrkJcMIBv9YpS/ljnSON@a7u5M2Bh@mqiu7iYXHPEsybbkdfUBEzZWvWplCFwITmgBwlKmgmAudmlnFqotDlyW5DstYAydZNv0ixm9lkuQG7Ufbjm8J2lZ3liebk/6ZccPi6lKaERRXbOw7E8qzHuS5k@Rvz9/62FzUi51LlWRi3c@FN0OPxodmA7OJfbHEbta2IMo2/iNv0B)
```
_.reverse.map(i=>if(i.last=='\'')i else i+" '")
```
Ungolfed version. [Try it online!](https://tio.run/##VVCxbsIwFNzzFScvtlWUgREplVK60S4FppbhNbWpkQnItpCqim9P/QyqkuXsOz2/u3PsyNMwnD4Ppkt4JdfjtwK@jIVVfoEXF9P7OgXX73d6StGUUcDXwVxMiKY@0lk5NI9wFsrVnmJC00B@SKnhYHw0@XiAgBQ6v71WGS7kkUxM8b5@7MEmzFUxKjfxJvRsRJ/FDGIrp@KaxQ1Dy7BkWE1H2jmLT/MSRFf30sf8A4rCPodpQ6CfUfdt79J/5xK4tqdgqPtWzLj2Oc8m3ytbFK1vHa/D8Ac)
```
object Main {
  def f(l: List[String]): List[String] = {
    l.reverse.map(i => if (i.last == '\'') i else i + " '")
  }
  val tests: List[List[String]] = List(
    List("R"),
    List("D", "U'"),
    List("S", "T", "A", "C", "K"),
    List("A2", "B2")
  )
  def main(args: Array[String]): Unit = {
    tests.foreach(test => println(f(test)))
  }
}
```
]