| Bytes | Lang | Time | Link |
|---|---|---|---|
| 006 | Japt | 240903T165948Z | Shaggy |
| 028 | Ruby pl | 240903T163910Z | Jordan |
| 061 | Scala | 230508T012738Z | 138 Aspe |
| 039 | Arturo | 230507T054738Z | chunes |
| 003 | Vyxal | 220529T185111Z | naffetS |
| 097 | Java 10 | 171009T102039Z | Kevin Cr |
| 027 | Röda | 171008T173259Z | fergusq |
| 041 | Python 2 | 171006T170905Z | Rod |
| 050 | JavaScript ES6 | 171006T185507Z | Bál |
| nan | Perl 5 | 171006T200828Z | Xcali |
| 098 | C gcc | 171006T200249Z | Leaky Nu |
| 007 | Pyke | 171006T190222Z | Mr. Xcod |
| 001 | Pyth | 171006T170155Z | Mr. Xcod |
| 065 | R | 171006T172206Z | Giuseppe |
| 017 | Alice | 171006T173954Z | Martin E |
| 002 | 05AB1E | 171006T175305Z | Erik the |
| 006 | MATL | 171006T175136Z | Giuseppe |
| 007 | CJam | 171006T174610Z | Martin E |
| 005 | Jelly | 171006T170924Z | Erik the |
Japt, 6 bytes
£ÔgUbX
£ÔgUbX :Implicit input of string U
£ :Map each X
Ô : Reverse U
g : Get character at index
UbX : First index of X in U
Java 10, 100 99 97 bytes
s->{var a=new char[256];int l=s.length,i=l;for(;i>0;a[s[--i]]=s[l+~i]);for(;i<l;s[i]=a[s[i++]]);}
Port of @LeakyNun's C answer. I doubt it can be done shorter without doing something similar in Java.
-1 byte thanks to @ceilingcat.
Input as char[] (character-array), and modifies this input instead of returning a new one to save bytes.
Röda, 27 bytes
f x{x|[x[-1-indexOf(_,x)]]}
This takes a list of characters as input and returns a stream of characters.
Using string datatype (40 bytes):
f x{x=x/""x|[x[-1-indexOf(_,x)]]|concat}
JavaScript ES6 50 bytes
3 bytes saved thanks to Justin Mariner
s=>[...s].map(x=>s.slice(~s.indexOf(x))[0]).join``
Test it:
f=s=>s.split``.map(x=>s.slice(~s.indexOf(x))[0]).join``
let inp = document.getElementById("inp");
let out = document.getElementById("out");
function change() {
out.innerText = f(inp.value);
}
change();
<input id="inp" type="text" oninput="change()" value="Hello, World!" /><br>
<p id="out"></p>
Perl 5, 23 + 1 (-p) = 24 bytes
eval"y/$_/".reverse."/"
Thanks to @MartinEnder's Alice entry for the transliteration idea
C (gcc), 98 bytes
i,n,a[256];char*f(char*c){n=strlen(c);for(i=n;i>0;i--)a[c[i-1]]=c[n-i];for(;i<n;i++)c[i]=a[c[i]];}
Argument must be a modifiable string; string is modified in-place.
Pyke, 7 bytes
L@Q_M@s
L@ - Get the index of the first index of each character.
Q - Push the input to the stack.
_ - Reverse it.
M@ - Get the element at (each) position in ^^^ in ^.
s - Join.
Pyth, 1 byte
X
Pyth has wonderful built-ins :-)
Pyth, 8 7 bytes
sm@_QxQ
How it works
This is the more interesting non-built-in approach.
sm@_QxQ - Full program.
m - Map over the input.
@_Q - Get the index in the reversed input.
xQ - Of the first index of each character in the String.
s - Join the list.
R, 68 65 bytes
function(s)chartr(paste(rev(el(strsplit(s,''))),collapse=''),s,s)
Ports Erik's 05AB1E method for 3 bytes less. It wasn't the first, but it was the first one I saw.
old version:
function(s)paste(rev(s<-el(strsplit(s,'')))[match(s,s)],collapse='')
Verify all test cases -- prints out the vector with the input as names and the output in quotes below.
A fairly naive implementation, but I don't think this gets shorter in R (and I look forward to being wrong about that). It's essentially an R port of Rod's python answer but it was developed independently.
Ungolfed explanation:
function(s){
s <- el(strsplit(s,'')) # string to characters
r <- rev(s) # reverse s
idx <- match(s,s) # indices of first matches
r <- r[idx] # do the replacement of duplicates
paste(r, collapse = "") # characters to string
} # implicit return of last evaluated statement
Alice, 17 bytes
/?.R?y.@
\i.!yDo/
Explanation
/...@
\.../
This is just the usual template for linear code in Ordinal mode. If we unfold this, the actual program simply becomes:
i.!?D.?.Ryyo
The idea here is similar to that of my CJam answer. Since Alice has no easy way of indexing into strings with integers, it's easiest to replicate this behaviour with transliteration (y in Alice). However, Alice's transliteration semantics are a lot more general than CJam's, which means that Alice doesn't just disregard repeated mappings. For example, if we just wanted to transliterate Mmm, marshmallows to its reverse, this would represent the following list of mappings:
M -> s
m -> w
m -> o
, -> l
-> l
m -> a
a -> m
r -> h
s -> s
h -> r
m -> a
a -> m
l ->
l -> ,
o -> m
w -> m
s -> M
Note that we've got, for example, m -> w, m -> o, m -> a and m -> a. CJam would just discard all except the first mapping, but Alice would instead cycle through these. So the first m would be mapped to w, the second to o, the fifth again to w and so on. In this case that isn't helpful, because in general if we perform y on AAB (for some strings A and B) as we did in CJam, we'll always just get B in Alice.
So how do we compute a mapping that works for y (i.e. how do we discard the repeated mappings manually)? Of course, by using another transliteration. :)
The source of the mapping we want has to be the nub of the input (i.e. the deduplicated input). If we apply the above mapping to the nub, then each character only appears once, so we're only making use of the first of each of the repeated mappings. So by transliterating the nub with the input and its reverse, we're effectively simply discarding the duplicated mappings. We can then use the nub and this new result as a mapping for the original input. I'm sure that made sense to someone...
So the code:
i Read input. ["Mmm, marshmallows"]
.! Store a copy on the tape.
?D Push the nub of the input. ["Mmm, marshmallows" "Mm, arshlow"]
. Duplicate. ["Mmm, marshmallows" "Mm, arshlow" "Mm, arshlow"]
? Retrieve input. ["Mmm, marshmallows" "Mm, arshlow" "Mm, arshlow" "Mmm, marshmallows"]
.R Push its reverse. ["Mmm, marshmallows" "Mm, arshlow" "Mm, arshlow" "Mmm, marshmallows" "swollamhsram ,mmM"]
y Transliterate. ["Mmm, marshmallows" "Mm, arshlow" "swllmhsr mm"]]
y Transliterate. ["swwllwmhsrwm mms"]
o Output. []
05AB1E, 2 bytes
R‡
Explanation:
R‡
R Reverse input
‡ Transliterate the input with itself and its reverse (as above) (it only keeps first occurrences)
MATL, 6 bytes
PGt&m)
implicit input
P flip
Gt paste input back to stack and dup
&m alternate ismember, returns duplicated indices
) apply indices to the reversed char array
implicit output, print as char
CJam, 7 bytes
q__W%er
Explanation
q e# Read all input.
__ e# Make two copies.
W% e# Reverse the third copy.
er e# Transliterate the input, mapping the input to its own reverse. Due
e# to the way transliteration works in CJam, if a character in the source
e# of the mapping is repeated, all but the first occurrence of that
e# character are ignored.