| Bytes | Lang | Time | Link |
|---|---|---|---|
| 007 | Uiua SBCS | 240612T153444Z | chunes |
| 076 | R | 240612T150558Z | int 21h |
| 003 | Thunno 2 | 230816T120338Z | The Thon |
| 050 | Python 2 | 170527T132636Z | Rod |
| 003 | Vyxal | 210120T045516Z | lyxal |
| 007 | 05AB1E | 210120T072932Z | ovs |
| 006 | Japt | 170830T102204Z | Shaggy |
| 027 | Perl | 180201T185036Z | Ton Hosp |
| 084 | Jq 1.5 | 171014T005449Z | jq170727 |
| 026 | J | 171013T201738Z | Jonah |
| 018 | K oK | 171013T144542Z | mkst |
| 012 | 05AB1E | 170831T123425Z | scottine |
| 027 | q/kdb+ | 170529T095421Z | mkst |
| 009 | APL Dyalog | 170528T110847Z | Adá |
| 021 | 05AB1E | 170613T202545Z | Magic Oc |
| 013 | Japt | 170528T054435Z | Oliver |
| 082 | Mathematica | 170527T143917Z | DELETE_M |
| nan | Perl 5 | 170530T002518Z | Chris |
| 024 | GNU APL 1.2 | 170529T084444Z | Arc676 |
| 069 | C | 170528T190521Z | Quentin |
| 1713 | Retina | 170527T154501Z | FryAmThe |
| 063 | Haskell | 170528T142644Z | Laikoni |
| 024 | APL Dyalog | 170528T110008Z | user4180 |
| 062 | Mathematica | 170527T210514Z | ZaMoC |
| nan | 170527T191520Z | Brad Gil | |
| 007 | Jelly | 170527T152755Z | Dennis |
| 009 | Jelly | 170527T151214Z | hyper-ne |
| 070 | OCaml | 170527T153339Z | juloo65 |
| 093 | Java OpenJDK 8 | 170527T095540Z | user4180 |
| 067 | Python 2 | 170527T111926Z | ovs |
| 048 | JavaScript ES6 | 170527T104654Z | Neil |
| 058 | PHP>=7.1 | 170527T104024Z | Jör |
| 010 | Alice | 170527T103716Z | Leo |
| 034 | Röda | 170527T102405Z | user4180 |
| 008 | MATL | 170527T100453Z | Luis Men |
| 087 | Python 3 | 170527T100706Z | Trelzevi |
| 032 | Octave | 170527T100525Z | rahnema1 |
Uiua SBCS, 7 bytes
⍜▽⇌◿2°⊏
⍜▽⇌◿2°⊏
◿2°⊏ # mask of odd indices
⍜▽⇌ # reverse according to mask
R, 76 bytes
\(S)cat(substring(S,s<-ifelse((a=1:(n=nchar(S)))%%2,a,n-n%%2+2-a),s),sep="")
In this solution I have used a vector of the characters positions from 1 to the length of the input string. The even numbers in this vector are swapped with their counterparts, the odd positions are kept in their place.
This numeric vector is then fed to the substring function, which produces the vector of the corresponding characters in a single call.
Thunno 2, 3 bytes
^rI
Explanation
# Implicit input
^ # Uninterleave into two parts and push them separately to the stack
# "Hello, World!" -> "Hlo ol!", "el,Wrd"
# "ABCDEF" -> "ACE", "BDF"
# "P" -> "P", ""
r # Reverse the top of stack (second part from before)
# "Hello, World!" -> "Hlo ol!", "drW,le"
# "ABCDEF" -> "ACE", "FDB"
# "P" -> "P", ""
I # Interleave the two strings together
# "Hello, World!" -> "HdlroW ,olle!"
# "ABCDEF" -> "AFCDBE"
# "P" -> "P"
# Implicit output
Python 2, 52 50 bytes
-2 bytes (and a bug fix) thanks to pxeger
s=bytearray(input())
s[1::2]=s[1::2][::-1]
print s
Vyxal, s, 3 bytes
yṘY
Turns out by making my answer valid I saved a byte. Oh the irony
Explained
yṘY
y # un-interleave
Ṙ # reverse
Y # re-interleave
05AB1E, 7 bytes
ι and .ι have been added since this challenge was posted, both of them save bytes here.
Sι`R.ιJ
If we were allowed to take a list of string of length 1, ι`R.ι would work for 5 bytes.
S # split the string into a list of characters
ι # uninterleave: push [even indices, odd indices]
` # push both values seperately to the stack
R # reverse the odd indices
.ι # interleave the two lists
J # join into a string
Japt, 8 6 bytes
ó oÔrí
ó oÔrí :Implicit input of string
ó :Uninterleave
o :Modify last element
Ô : Reverse
r :Reduce by
í : Interleaving
Perl, 27 bytes
Includes +5 for -F lp
perl -F -lpe 's/.\K./$F[@F%-2-pos]/g' <<< "abcdefghi"
If you give STDIN without newline you can drop the l option
Jq 1.5, 84 bytes
[_nwise(2)|[_nwise(1)]]|transpose|map(map(values))|.[1]|=reverse|[transpose[][]]|add
Expanded
[_nwise(2)|[_nwise(1)]] # make list of two character lists
| transpose # convert to list of two lists of characters
| map(map(values)) # discard null
| .[1] |= reverse # reverse second list
| [transpose[][]] # transpose back to two lists and flatten
| add # convert back to string
J, 26 bytes
[:,@,./(0 1$~#)]`(|.@])/.]
ungolfed
[: ,@,./ (0 1 $~ #) ]`(|.@])/. ]
explanation
(0 1$~#)]`(|.@])/.]Use Key/.to split the input into the even/odd groups:(0 1$~#)creates the group definition, by repeating 0 and 1 cyclically to the length of the input. We use the gerundial form of Key for its main verb]`(|.@]), which applies the identity to the first group and reverses the second group:(|.@]).- Now that we have the two groups, the odd one reversed, we just zip them together and flatten:
,@,./
K (oK), 18 bytes
Solution:
{x[w:&2!!#x]:x@|w}
Examples:
> {x[w:&2!!#x]:x@|w}"Hello, World!"
"HdlroW ,olle!"
> {x[w:&2!!#x]:x@|w}"Hello World!"
"H!llooW rlde"
Explanation:
Interpretted mostly right-to-left, find the odd-indices characters, reverse them and put them back into the string
{x[w:&2!!#x]:x@|w} / solution
{ } / lambda function with implicit parameter x
#x / count x, #"Hello, World!" -> 13
! / til, !13 -> 0 1 2 3 4 5 6 7 8 9 10 11 12
2! / 2 modulo, 2!0 1 2 3 4 5 6 7 8 9 10 11 12 -> 0 1 0 1 0 1 0 1 0 1 0 1 0
& / where true, @0 1 0 1 0 1 0 1 0 1 0 1 0 -> 1 3 5 7 9 11
w: / store in variable w
|w / reverse w, |1 3 5 7 9 11 -> 11 9 7 5 3 1
x@ / index into x at these indices
x[ ]: / assign right to x at these indices
05AB1E, 12 bytes
RDgÈúøvyNÉè?
RDgÈúøvyNÉè? Implicit input: "abcdefghij"
R Reverse the string: "jihgfedcba"
DgÈú Put (length is even?1:0) spaces in front of it " jihgfedcba"
ø Zip (reinjects the input implicitly): ["a ", "bj", "ci", ...]
vy For each element of the list
NÉè Extract&push element[iteration is odd?1:0]
? Print without newline
q/kdb+, 70 56 47 38 35 29 27 bytes
Solution:
{x[w]:x(|)w:(&)#:[x]#0 1;x}
Example:
q){x[w]:x(|)w:(&)#:[x]#0 1;x}"Hello, World!"
"HdlroW ,olle!"
q){x[w]:x(|)w:(&)#:[x]#0 1;x}"Hello World!"
"H!llooW rlde"
Explanation:
Find the odd indices of the string, reverse this list, pull out elements at these points and then reassign them in-place to the original string.
{x[w]:x reverse w:where count[x]#0 1;x} / ungolfed
{ ; } / lambda function with two lines
0 1 / the list (0;1)
# / take
count[x] / length of input
where / indices where this is > 0
w: / save in variable w
reverse / reverse this list
x / index into x at these points
: / assignment
x[w] / assign x at indices with new values
x / return x
Edits:
-9 bytes; switching out
countfor(#:),tilfor(!),wherefor(&:)andreversefor(|:).-3 bytes; switching out
(#:)for(#),(&:)for(&)and(|:)for(|)-6 bytes; complete re-write
-2 bytes; using assignment rather than apply
APL (Dyalog), 9 bytes
Requires ⎕IO←0 (default on many systems) for proper definition of odd and even.
⌽@{2|⍳≢⍵}
⌽ reverse
@ at the elements filtered by the mask result from applying the
{ anonyomous function
2| the mod-2 of
⍳ the indices of
≢ the tally (length) of
⍵ the argument
} on the argument
05AB1E, 21 bytes
DgÉi¶«}2ô.BøRćR‚˜øJ¶K
I'm guessing the reason this wasn't done in 05AB1E yet is because it's gross...
Yet another time the zip function's auto-drop-last-element hurts instead of helps.
P.S. If you have improvement suggestions on my answer, post your own; it's likely enough of an improvement to warrant you getting the points. I am pretty ashamed of this answer.
Japt, 14 13 bytes
12 bytes of code, +1 for the -P flag.
Saved 1 byte thanks to @Shaggy
¬ë íU¬Åë w)c
Explanation:
¬ë íU¬Åë w)c
¬ Split the input into an array of chars
ë Get every other char, starting at index 0
í Pair with:
U¬ Input, split into a char array
Å .slice(1)
ë Get every other char
w Reverse
c Flatten
-P Join into a string
Mathematica, 82 bytes
""<>(f=Flatten)[{#&@@#,Reverse@Last@#}&@f[Characters@#~Partition~UpTo@2,{2}],{2}]&
Perl 5, 46 + 3 for -F flag = 49 bytes
while(++$x<@F){print$F[$x%2?$x-1:@F-$x-$#F%2]}
Uses the -F flag to auto split the input into an array of characters, @F. Loops through the array and outputs that element for an even index or that index (plus one for an odd length string) from the end for an odd input.
Takes input with a trailing newline. Without the trailing newline, can just change the pre-increment on $x to a post-increment.
A little more readable:
while(++$x<@F) { #While the index is less than the number of elements in the array. $x is 1-indexing the array despite the fact that perl is 0-indexed because it keeps us from having to use a proper for loop or a do-while loop
if($x%2) { #If $x is odd
print $F[$x-1] #Print the element
} else {
print $F[@F-$x-$#F%2] #Print from the end. $#F%2 fixes it for odd length strings
}
}
GNU APL 1.2, 24 bytes
R[X]←⌽R[X←2×⍳⌊.5×⍴R←⍞]◊R
APL works from right to left. ⍴R←⍞ assigns user input to R and then evaluates its length. Halve this by multiplying by .5 and apply ⌊ floor function. ⍳ returns all numbers from 1 to the argument.
APL operates on arrays, so 2× the array we just got from ⍳ doubles each element, giving us just the even indices (1-indexed, so relies on ⎕IO being 1).
When accessing multiple indices of a vector, APL gives the elements at those indices in a vector. R[X←2×⍳⌊.5×⍴R←⍞] gives just the even-indexed elements. ⌽ reverses the elements. Then, assign the reversed values back to the even indices (assigning these indices to X saves 6 bytes).
◊ is the statement separator. After the reversing is done, evaluate R to print the result.
C, 69 bytes
c,l;f(char*s){l=strlen(s);for(c=0;c<l;++c)putchar(s[c&1?l-l%2-c:c]);}
Pretty simple. Walks the string, printing either the current character or the opposite one.
Ungolfed and explained:
f(char *str) {
int len = strlen(str); // Get the total length
for(int c = 0; c<len; ++c) // Loop over the string
putchar(s[ // Print the char that is,
c & 1 // if c is odd,
? l - l % 2 - c // c chars from the end (adjusting odd lengths),
: c // and at index c otherwise
]);
}
Retina, 17 13 bytes
O^$`(?<=\G.).
Fixed an error thanks to Neil.
Saved 4 bytes thanks to Kobi.
Selects each letter preceded by an odd number of characters and reverses them. Does this by using \G which matches the end of the last match.
Haskell, 63 bytes
(_:r)!(a:s)=a:s!r
_!_=[]
f s=([' '|even$length s]++reverse s)!s
Try it online! Usage: f "some string".
For odd strings like abcdefghi, the function f passes the string and its reversal to the function !, which alternates taking chars from both strings. For even strings this does not work, and we need to append a dummy character first to get the offset right.
Mathematica, 62 bytes
takes as input a string
(s=Characters@#;Row@Riffle[s[[;; ;;2]],Reverse@s[[2;; ;;2]]])&
Perl 6, 63 58 55 bytes
{[~] .comb[{flat roundrobin (0,2...^*>=$_),[R,] 1,3...^*>=$_}]}
{[~] flat roundrobin .comb[{0,2...*},{$_-1-$_%2,*-2...*}]}
{[~] flat roundrobin .comb[{0,2...*},{[R,] 1,3...^$_}]}
{ # bare block lambda with implicit parameter 「$_」
[~] # reduce using string concatenation operator
flat # make the following a flat list
roundrobin # grab one from each of the following 2 lists,
# repeat until both are empty
.comb\ # split the input into graphemes (implicit method call)
[ # index into that sequence
{ 0, 2 ... * }, # code block producing every non-negative even number
{ # code block producing reversed odd numbers
# (「$_」 here contains the number of available elements)
[R,] # reduce with reversed comma operator
# (shorter than 「reverse」)
1, 3 ...^ $_ # odd numbers stopping before it gets
# to the number of input elements
}
]
}
I had to use roundrobin rather than zip, because zip stops as soon as one of the input lists is exhausted.
Jelly, 7 bytes
s2ZU2¦Z
This is a full program.
How it works
s2ZU2¦Z Main link. Argument: s (string)
s2 Split s into pairs.
Z Zip/tranpose, grouping characters by the parity of their indices.
¦ Sparse application:
U Upend; reverse both strings in the pair.
2 Replace the second string with the reversed string.
Z Zip/transpose, interleaving the two strings.
Jelly, 9 bytes
Ḋm2U
m2żÇ
Ḋm2U Helper Link -> Dequeue (return last len-1 elements), take every second element, reverse
m2żÇ Main Link -> Take every second element, then interleave with the result of the helper link
-1 byte thanks to Dennis
OCaml, 70 bytes
let f s=String.(mapi(fun i c->s.[(length s land-2-i-i)*(i mod 2)+i])s)
Java (OpenJDK 8), 108 96 94 93 bytes
Saved 1 byte by using @Neil's neat trick of using s[s.length+~i|1]
String f(char[]s){int a=s.length,b=0;String c="";for(;b<a;b++)c+=s[b%2<1?b:a+~b|1];return c;}
Python 2, 67 bytes
lambda s,j=''.join:j(map(j,zip(s[::2]+' ',s[1::2][::-1]+' ')))[:-1]
JavaScript (ES6), 48 bytes
f=
s=>s.replace(/./g,(c,i)=>i%2?s[s.length+~i|1]:c)
<input oninput=o.textContent=f(this.value)><pre id=o>
Alice, 10 bytes
/ZY
\IOR@/
Half of the bytes of this program are spent on correctly formatting the source, the actual commands are just IYRZO, because Alice has just the right builtins for this task.
Explanation
As I said, the mirrors (/\), the newline and @ are there just to make the ip move in the right direction and terminate the program at the end. The actual code, linearised, is the following:
IYRZO
I Input a line
Y Unzip it into its even positions and its odd ones
R Reverse the odd positions
Z Zip it back again
O Output
Quite straightforward, I'd say.
Röda, 34 bytes
f a{a/=""a[::2]<>reverse(a[1::2])}
Explanation
a/="" Convert the argument a into an array of length-1 strings
<> Interleave
a[::2] Every even element of a with
reverse(a[1::2]) Every odd element of a reversed
Here is an alternative solution at the same bytecount
36 34 bytes
{[_/""]|_[::2]<>reverse(_1[1::2])}
This is an anonymous function that takes input as a string from the input stream.
MATL, 8 bytes
t2L)P5M(
Try it online! Or verify all test cases.
Explanation
t % Implicit input. Duplicate
% STACK: 'abcdefghi', 'abcdefghi'
2L % Push [2, 2, 1j]. This represents 2:2:end when used as an index
% STACK: 'abcdefghi', 'abcdefghi', [2, 2, 1j]
) % Get entries at those indices
% STACK: 'abcdefghi', 'bdfh'
P % Flip
% STACK: 'abcdefghi', 'hfdb'
5M % Push [2, 2, 1j] again
% STACK: 'abcdefghi', 'hfdb', [2, 2, 1j]
( % Write entries at those indices. Implicit display
% STACK: 'ahcfedgbi'
Python 3, 93 87 bytes
lambda s:"".join("".join(t)for t in zip(s[::2],reversed(s[1::2])))+("",s[-1])[len(s)%2]