Boustrophedonise

35

4

Related but very different.

A boustrophedon is a text where every other line of writing is flipped or reversed, with reversed letters.

In this challenge, we will just reverse every other line, but leave the actual characters used intact. You may chose which lines to reverse, as long as it is every other one.

You may take the text in any suitable format as long as you support zero or more lines of printable ASCII, each with zero or more characters.

Examples:

["Here are some lines","of text for you","to make a","boustrophedon"]:

["Here are some lines","uoy rof txet fo","to make a","nodehportsuob"] or ["senil emos era ereH","of text for you","a ekam ot","boustrophedon"]


["My boustrophedon"]:

["My boustrophedon"] or ["nodehportsuob yM"]

[]:  
[]

["Some text","","More text","","","Last bit of text"]:

["Some text","","More text","","","txet fo tib tsaL"] or ["txet emoS","","txet eroM","","","Last bit of text"]

Adám

Posted 2017-12-08T11:18:45.970

Reputation: 37 779

Can't understand if return and input need to be text separated lines or it can be a file or a list of lines. – sergiol – 2017-12-08T12:30:23.437

@sergiol Default PPCG I/O rules apply.

– Adám – 2017-12-08T12:35:14.310

Can my code behave inconsistently, i.e. sometimes start reversing from the first line and sometimes from the second? – Erik the Outgolfer – 2017-12-08T13:02:07.703

2@EriktheOutgolfer Yes, I asked about this earlier and the wording of "You may chose which lines to reverse, as long as it is every other one." was actually changed to what it is now to make it general enough for that behaviour. – Martin Ender – 2017-12-08T13:12:25.710

More answer than upvotes... – user202729 – 2017-12-08T13:36:01.003

Perhaps a good testcase: ["Some text","","More text","","","Last bit of text"] – Sanchises – 2017-12-08T16:50:40.290

1@totallyhuman Yes, as per OP. – Adám – 2017-12-09T20:07:10.220

How does the last example work? "Last bit of text" is never reversed and "More text" always is. – CJ Dennis – 2017-12-11T00:41:47.460

@CJDennis Fixed. – Adám – 2017-12-11T08:00:49.027

Related – user41805 – 2018-05-13T19:53:14.573

Answers

20

APL (Dyalog Classic), 4 bytes

⊢∘⌽\

The input is a vector of character vectors.

is a function that reverses a vector (when is applied monadically).

is "dex" - a function that returns its right argument. When composed () with another function f it forces the latter to be monadic as A ⊢∘f B is equivalent to A ⊢ (f B) and therefore f B.

\ is the scan operator. g\A B C ... is the vector A (A g B) (A g (B g C)) ... where g is applied dyadically (infix notation). Substituting ⊢∘⌽ for g it simplifies to:

A (A ⊢∘⌽ B) (A ⊢∘⌽ (B ⊢∘⌽ C)) ...
A (⌽B) (⌽⌽C) ....
A (⌽B) C ....

The reversals at even positions (or odd, depending on how you count) cancel out.

Try it online!

ngn

Posted 2017-12-08T11:18:45.970

Reputation: 11 449

4That’s literally ]&|.&.>/\ for those who can read J. – FrownyFrog – 2017-12-08T15:29:18.817

2This is really clever. – Erik the Outgolfer – 2017-12-08T18:21:51.120

13

Haskell, 26 bytes

zipWith($)l
l=id:reverse:l

Try it online! Usage example: zipWith($)l ["abc","def","ghi"] yields ["abc","fed","ghi"].

Explanation:

l is an infinite list of functions alternating between the identity function and the reverse function.

The main function zips l and the input list with the function application $, that is for an input ["abc", "def", "ghi"] we get [id$"abc", reverse$"def", id$"ghi"].

Laikoni

Posted 2017-12-08T11:18:45.970

Reputation: 23 676

11

Husk, 4 bytes

z*İ_

Takes and returns a list of strings (the interpreter implicitly joins the result by newlines before printing). The first string is reversed. Try it online!

Explanation

z*İ_  Implicit input.
  İ_  The infinite list [-1,1,-1,1,-1,1..
z     Zip with input
 *    using multiplication.

In Husk, multiplying a string with a number repeats it that many times, also reversing it if the number is negative.

Zgarb

Posted 2017-12-08T11:18:45.970

Reputation: 39 083

6

JavaScript (ES6), Firefox, 43 bytes

This version abuses the sort algorithm of Firefox. It generates garbage on Chrome and doesn't alter the strings at all on Edge.

a=>a.map((s,i)=>[...s].sort(_=>i&1).join``)

Test cases

let f =

a=>a.map((s,i)=>[...s].sort(_=>i&1).join``)

console.log(f(["Here are some lines","of text for you","to make a","boustrophedon"]))
console.log(f(["My boustrophedon"]))
console.log(f([]))

Or Try it online! (SpiderMonkey)


JavaScript (ES6), 45 bytes

a=>a.map(s=>(a^=1)?s:[...s].reverse().join``)

Test cases

let f =

a=>a.map(s=>(a^=1)?s:[...s].reverse().join``)

console.log(f(["Here are some lines","of text for you","to make a","boustrophedon"]))
console.log(f(["My boustrophedon"]))
console.log(f([]))

Arnauld

Posted 2017-12-08T11:18:45.970

Reputation: 111 334

6

Perl 5, 17 + 2 (-pl) = 19 bytes

odd lines reversed

$_=reverse if$.%2

even lines reversed

$_=reverse if$|--

After @Martin's comment : input needs to have a trailing linefeed.

try it online

Nahuel Fouilleul

Posted 2017-12-08T11:18:45.970

Reputation: 5 582

6

APL (Dyalog Unicode), 10 bytes

⌽¨@{2|⍳≢⍵}

Works both ways:

Try it online! with ⎕IO←1

Try it online! with ⎕IO←0

How it works:

⌽¨@{2|⍳≢⍵} ⍝ tacit prefix fn
   {   ≢⍵} ⍝ Length of the input
      ⍳    ⍝ generate indexes from 1 (or 0 with ⎕IO←0)
    2|     ⍝ mod 2; this generates a boolean vector of 0s (falsy) and 1s (truthy)
  @        ⍝ apply to the truthy indexes...
⌽¨         ⍝ reverse each element

J. Sallé

Posted 2017-12-08T11:18:45.970

Reputation: 3 233

5

Haskell, 27 bytes

foldr((.map reverse).(:))[]

Try it online!

Haskell, 30 bytes

f(a:b:c)=a:reverse b:f c
f a=a

Try it online!

Recursion FTW.

totallyhuman

Posted 2017-12-08T11:18:45.970

Reputation: 15 378

4

05AB1E, 5 4 bytes

εNFR

Try it online!

Explanation

ε     # apply to each element (row)
 NF   # index times do:
   R  # reverse

Emigna

Posted 2017-12-08T11:18:45.970

Reputation: 50 798

3

K (oK), 17 14 bytes

Solution:

@[;&2!!#x;|]x:

Try it online!

Example:

@[;&2!!#x;|]x:("this is";"my example";"of the";"solution")
("this is"
"elpmaxe ym"
"of the"
"noitulos")

Explanation:

Apply reverse at odd indices of the input list:

@[;&2!!#x;|]x: / the solution
            x: / store input as variable x
@[;      ; ]   / apply @[variable;indices;function] (projection)
          |    / reverse
       #x      / count (length) of x, e.g. 4
      !        / til, !4 => 0 1 2 3
    2!         / mod 2, 0 1 2 3 => 0 1 0 1       
   &           / where true, 0 1 0 1 => 1 3

Notes:

  • switched out &(#x)#0 1 for &2!!#x to save 3 bytes

streetster

Posted 2017-12-08T11:18:45.970

Reputation: 3 635

3

APL (Dyalog), 12 bytes

⍳∘≢{⌽⍣⍺⊢⍵}¨⊢

Try it online!

Uriel

Posted 2017-12-08T11:18:45.970

Reputation: 11 708

3

Python 2, 40 36 bytes

-4 bytes thanks to @Mr.Xcoder

def f(k):k[::2]=map(reversed,k[::2])

Try it online!

Outputs by modifying the input list


Python 2, 43 bytes

f=lambda k,d=1:k and[k[0][::d]]+f(k[1:],-d)

Try it online!

ovs

Posted 2017-12-08T11:18:45.970

Reputation: 21 408

3

J, 9 bytes

(,|.&.>)/

Reduce from right to left, reversing all strings in the result and prepending the next string as is.

Try it online!

We can do 6 using ngn’s approach, but there will be extra spaces:

]&|./\

Try it online!

FrownyFrog

Posted 2017-12-08T11:18:45.970

Reputation: 3 112

3

Alumin, 66 bytes

hdqqkiddzhceyhhhhhdaeuzepkrlhcwdqkkrhzpkzerlhcwqopshhhhhdaosyhapzw

Try it online!

FLAG: h
hq
  CONSUME A LINE
  qk
  iddzhceyhhhhhdaeuze
  pk
  rlhcw
  REVERSE STACK CONDITIONALLY
  dqkkrhzpkzerlhcwqops

  OUTPUT A NEWLINE
  hhhhhdao
syhapzw

Conor O'Brien

Posted 2017-12-08T11:18:45.970

Reputation: 36 228

2@totallyhuman This is actually my language. – Conor O'Brien – 2017-12-08T16:18:37.787

2

R, 85 bytes

for(i in seq(l<-strsplit(readLines(),"")))cat("if"(i%%2,`(`,rev)(l[[i]]),"\n",sep="")

Try it online!

Input from stdin and output to stdout.

Each line must be terminated by a linefeed/carriage return/CRLF, and it prints with a corresponding newline. So, inputs need to have a trailing linefeed.

Giuseppe

Posted 2017-12-08T11:18:45.970

Reputation: 21 077

2

Jelly, 5 4 bytes

U¹ƭ€

Try it online!

Thanks HyperNeutrino for -1 bytes! (actually because I never knew how ƭ works due to lack of documentation, this time I got lucky)

user202729

Posted 2017-12-08T11:18:45.970

Reputation: 14 620

Tried ¦ with m (7 bytes). s2U2¦€;/ is also 7 bytes. – user202729 – 2017-12-08T13:30:15.070

2

T-SQL, 65 bytes

Our standard input rules allow SQL to input values from a pre-existing table, and since SQL is inherently unordered, the table must have row numbers to preserve the original text order.

I've defined the table with an identity column so we can simply insert lines of text sequentially (not counted toward byte total):

CREATE TABLE t 
    (i int identity(1,1)
    ,a varchar(999))

So to select and reverse alternating rows:

SELECT CASE WHEN i%2=0THEN a
ELSE reverse(a)END
FROM t
ORDER BY i

Note that I can save 11 bytes by excluding the ORDER BY i, and that is likely to return the list in the original order for any reasonable length (it certainly does for the 4-line example). But SQL only guarantees it if you include the ORDER BY, so if we had, say, 10,000 rows, we would definitely need this.

BradC

Posted 2017-12-08T11:18:45.970

Reputation: 6 099

2

Perl 6, 44 bytes

lines.map: ->\a,$b?{a.put;.flip.put with $b}

Try it

lines               # get the input as a list of lines
.map:
-> \a, $b? {        # $b is optional (needed if there is an odd number of lines)
  a.put;            # just print with trailing newline
  .flip.put with $b # if $b is defined, flip it and print with trailing newline
}

Brad Gilbert b2gills

Posted 2017-12-08T11:18:45.970

Reputation: 12 713

2

Comma delimited:

Stax, 12 bytes

ü«äì╠▒╕█╬pεû

Run and debug it

input: ABC,def,GHI,jkl,MNO,pqr,STU

Newline delimited:

Stax, 8 bytes

Çε÷┘)¼M@

Run and debug it

input:

ABC
def
GHI
jkl
MNO
pqr
STU

output for both:

CBA
def
IHG
jkl
ONM
pqr
UTS

182764125216

Posted 2017-12-08T11:18:45.970

Reputation: 499

1

Alice, 13 bytes

M%/RM\
d&\tO/

Try it online!

Input via separate command-line arguments. Reverses the first line (and every other line after that).

Explanation

       At the beginning of each loop iteration there will always be zero
       on top of the stack (potentially as a string, but it will be
       converted to an integer implicitly once we need it).
M      Push the number of remaining command-line arguments, M.
%      Take the zero on top of the stack modulo M. This just gives zero as
       long as there are arguments left, otherwise this terminates the
       program due to the division by zero.
/      Switch to Ordinal mode.
t      Tail. Implicitly converts the zero to a string and splits off the
       last character. The purpose of this is to put an empty string below
       the zero, which increases the stack depth by one.
M      Retrieve the next command-line argument and push it as a string.
/      Switch back to Cardinal mode.
d      Push the stack depth, D.
&\R    Switch back to Ordinal mode and reverse the current line D times.
O      Print the (possibly reversed) line with a trailing linefeed.
\      Switch back to Cardinal mode.
       The instruction pointer loops around and the program starts over
       from the beginning.

Martin Ender

Posted 2017-12-08T11:18:45.970

Reputation: 184 808

1

Actually, 7 bytes

;r'R*♀ƒ

Explanation:

;r'R*♀ƒ
;r       range(len(input))
  'R*    repeat "R" n times for n in range
     ♀ƒ  call each string as Actually code with the corresponding input element as input (reverse each input string a number of times equal to its index)

Try it online!

Mego

Posted 2017-12-08T11:18:45.970

Reputation: 32 998

1

Standard ML (MLton), 51 bytes

fun$(a::b::r)=a::implode(rev(explode b)):: $r| $e=e

Try it online! Usage example: $ ["abc","def","ghi"] yields ["abc","fed","ghi"].

Explanation:

$ is a function recursing over a list of strings. It takes two strings a and b from the list, keeps the first unchanged and reverses the second by transforming the string into a list of characters (explode), reversing the list (rev), and turning it back into a string (implode).

Laikoni

Posted 2017-12-08T11:18:45.970

Reputation: 23 676

+1, not enough ML solutions imo – jfh – 2017-12-08T22:36:07.683

1

Charcoal, 9 bytes

EN⎇﹪ι²⮌SS

Try it online! Link is to verbose version of code. Note: Charcoal doesn't know the length of the list, so I've added it as an extra element. Explanation:

 N          First value as a number
E           Map over implicit range
    ι       Current index
     ²      Literal 2
   ﹪        Modulo
  ⎇         Ternary
       S    Next string value
      ⮌     Reverse
        S   Next string value
            Implicitly print array, one element per line.

Neil

Posted 2017-12-08T11:18:45.970

Reputation: 95 035

1

Retina, 18 bytes

{O$^`\G.

*2G`
2A`

Try it online! Explanation: The first stage reverse the first line, then the second stage prints the first two lines, after which the third stage deletes them. The whole program then repeats until there is nothing left. One trailing newline could be removed at a cost of a leading ;.

Neil

Posted 2017-12-08T11:18:45.970

Reputation: 95 035

1

CJam, 11 bytes

{2/Waf.%:~}

Try it online! (CJam array literals use spaces to separate elements)

Explanation:

{              Begin block, stack: ["Here are some lines" "of text for you" "to make a" "boustrophedon"]
 2/            Group by 2:         [["Here are some lines" "of text for you"] ["to make a" "boustrophedon"]]
   W           Push -1:            [["Here are some lines" "of text for you"] ["to make a" "boustrophedon"]] -1
    a          Wrap in array:      [["Here are some lines" "of text for you"] ["to make a" "boustrophedon"]] [-1]
     f.%       Vectorized zipped array reverse (black magic):
                                   [["senil emos era ereH" "of text for you"] ["a ekam ot" "boustrophedon"]]
        :~     Flatten:            ["senil emos era ereH" "of text for you" "a ekam ot" "boustrophedon"]
          }

Explanation for the Waf.% "black magic" part:

  • W is a variable preinitialized to -1. a wraps an element in an array, so Wa is [-1].
  • % pops a number n and an array a and takes every nth element of the array. When n is negative, it also reverses it, meaning that W% reverses an array.
  • . followed by a binary operation applies that operation to corresponding elements of an array, so [1 2 3] [4 5 6] .+ is [5 7 9]. If one array is longer than the other, the elements are kept without modification, meaning that Wa.% reverses the first element of an array.
  • f followed by a binary operation will take an element from the stack and then act like {<that element> <that operation>}%, that is, go through each element in the array, push its element, push the element first popped from the stack, run the operation, and then collect the results back into an array. This means that Wa.f% reverses the first element of every element in the array.

Esolanging Fruit

Posted 2017-12-08T11:18:45.970

Reputation: 13 542

1

Wolfram Language (Mathematica), 33 bytes

Fold[StringReverse@*Append,{},#]&

Try it online!

How it works

StringReverse@*Append, when given a list of strings and another string as input, adds the string to the end of the list and then reverses all of the strings.

Folding the input with respect to the above means we:

  • Reverse the first line.
  • Add the second line to the end and reverse both of them.
  • Add the third line to the end and reverse all three.
  • Add the fourth line to the end and reverse all four.
  • And so on, until we run out of lines.

Each line gets reversed one time fewer than the previous line, so the lines alternate direction.

Misha Lavrov

Posted 2017-12-08T11:18:45.970

Reputation: 4 846

1

V, 4 bytes

òjæ$

Try it online!

ò      ' <M-r>ecursively (Until breaking)
 j     ' Move down (breaks when we can't move down any more)
  æ$   ' <M-f>lip the line to the end$

nmjcman101

Posted 2017-12-08T11:18:45.970

Reputation: 3 274

1

MATL, 7 bytes

"@gNo?P

Try it online!

Takes a cell array of strings, and leaves the result on the stack.

Explanation

"         % Loop over cell array
 @g     %   Push new string, and 'unwrap' it from its cell array.
     No?  %   Yes? Maybe so? (push stack size N, check if odd o, if so,...
        P %     Flip

As the implicit print function in MATL does not display an empty line for an empty item on the stack, we explicitly end the if-statement and loop and print the stack in the TIO footer:

]]X#

But this is not part of the program, as per the IO default

Sanchises

Posted 2017-12-08T11:18:45.970

Reputation: 8 530

@LuisMendo Thanks (overlooked this comment for quite some time) – Sanchises – 2017-12-21T09:08:43.753

1

C,  118   103  101 bytes

Thanks to @gastropner for saving 15 bytes and thanks to @ceilingcat for saving a byte!

i;f(l,n)char**l;{for(;n--;++l,n&1&&puts(""))for(i=strlen(*l);(n&1||!puts(*l))*i;putchar((*l)[--i]));}

Try it online!

C, 147 bytes

i,j;f(char*s){char t[strlen(s)];for(i=0;;t[j++]=*s++)if(!*s|*s==10){t[j]=0;i=!i;for(i&&(j=!puts(t));j;j||puts(""))putchar(t[--j]);if(!*s++)break;}}

Try it online!

Steadybox

Posted 2017-12-08T11:18:45.970

Reputation: 15 798

103 bytes if you go with clang or include string.h: f(l,n)char**l;{for(;n--;++l,puts(""))for(char*s=*l+strlen(*l);(n&1||printf(*l)*0)&s>*l;putchar(*--s));} – gastropner – 2017-12-09T09:45:42.030

100 bytes and GCC is happy again: i;f(l,n)char**l;{for(;n--;++l,puts(""))for(i=strlen(*l);(n&1||!printf(*l))&i>0;putchar((*l)[--i]));} – gastropner – 2017-12-09T10:04:53.873

@gastropner Thanks! I don't think using printf to print *l works, though, because the string might for example contain %d. – Steadybox – 2017-12-10T15:00:57.037

1

Swift, 90 85 82 72 bytes

-10 bytes thanks to @Mr.Xcoder

func f(a:[String]){print(a.reduce([]){$0.map{"\($0.reversed())"}+‌​[$1]})}

Herman L

Posted 2017-12-08T11:18:45.970

Reputation: 3 611

You can use print and drop the return type declaration: func f(a:[String]){print(a.reduce([]){$0.map{"\($0.reversed())"}+[$1]})} – Mr. Xcoder – 2017-12-08T20:40:20.193

1

Ruby, 19 + 2 = 21 bytes

+2 bytes for -nl flags.

$.%2<1&&$_.reverse!

Try it online!

Explanation

Practically identical to the Perl 5 answer, though I hadn’t seen that one when I wrote this.

With whitespace, the code looks like this:

$. % 2 < 1 && $_.reverse!

The -p option makes Ruby effectively wrap your script in a loop like this:

while gets
  # ...
  puts $_
end

The special variable $_ contains the last line read by gets, and $. contains the line number.

The -l enables automatic line ending processing, which automatically calls chop! on each input line, which removes the the \n before we reverse it.

Jordan

Posted 2017-12-08T11:18:45.970

Reputation: 5 001

1

GNU sed, 31 + 1 = 32 bytes

+1 byte for -r flag.

G
:
s/(.)(.*\n)/\2\1/
t
s/.//
N

Try it online!

Explanation

G                   # Append a newline and contents of the (empty) hold space
:
  s/(.)(.*\n)/\2\1/   # Move the first character to after the newline
  t                   # If we made the above substitution, branch to :
s/.//               # Delete the first character (now the newline)
N                   # Append a newline and the next line of input

Jordan

Posted 2017-12-08T11:18:45.970

Reputation: 5 001

1

Befunge-93, 48 bytes

 <~,#_|#*-+92:+1:
#^_@  >:#,_"#"40g!*40p91+,~:1+

Try It Online

Prints first line in reverse. Has a trailing newline.

Basically, it works by alternating between printing as it gets input and storing the input on the stack. When it reaches a newline or end of input, it prints out the stack, prints a newline, and modifies the character at 0,4 to be either a # or a no-op to change the mode. If it was the end of input, end the program

Jo King

Posted 2017-12-08T11:18:45.970

Reputation: 38 234

1

Japt, 4 bytes

ËzEÑ

Try it

ËzEÑ     :Implicit input of array
Ë        :Map each string at 0-based index E
 z       :  Rotate 90 degrees clockwise
  EÑ     :    E*2 times

Shaggy

Posted 2017-12-08T11:18:45.970

Reputation: 24 623

0

C#, 64 54 + 18 bytes

Try It Online!

a=>a.Select((x,i)=>i%2<1?x:string.Concat(x.Reverse())).ToArray()

Saved 10 bytes by returning IEnumerable

LiefdeWen

Posted 2017-12-08T11:18:45.970

Reputation: 3 381

0

Pyth, 6 bytes

.e@_Bb

Try it here!

  • .e ~ Enumerated map. k is the index, b is the current element.

  • _Bb ~ Bifurcate b with its reverse.

  • @ ~ Modular index in ^ with k.

Mr. Xcoder

Posted 2017-12-08T11:18:45.970

Reputation: 39 774

0

Python 2, 45 bytes

lambda k:[r[::i%-2|1]for i,r in enumerate(k)]

Try it online!

Python 2, 46 bytes

f=lambda k:k and[k[0][::len(k)%-2|1]]+f(k[1:])

Try it online!

Mr. Xcoder

Posted 2017-12-08T11:18:45.970

Reputation: 39 774

0

Tcl, 61 bytes

proc B L {lmap e $L {expr [incr i]%2?"$e":"[string rev $e]"}}

Try it online!

sergiol

Posted 2017-12-08T11:18:45.970

Reputation: 3 055

0

Stacked, 18 bytes

[{x i:x$revi*}map]

Try it online!

This simply reverses each string in the array according to its position.

Alternative, 24 bytes

[:#':>#,tr[...$rev*]map]

Same approach, but generates indices manually.

Conor O'Brien

Posted 2017-12-08T11:18:45.970

Reputation: 36 228

0

Red, 68 bytes

f: func[s][forall s[g: first s if odd? length? s[reverse g]print g]]

Try it online!

Galen Ivanov

Posted 2017-12-08T11:18:45.970

Reputation: 13 815

0

Jelly, 5 bytes

UJḤ$¦

Try it online!*

*Jelly outputs nested lists joined by spaces, and there is no native string type. Strings in Jelly are lists of characters, and that's why they're displayed that way. If you want them to be merged by spaces, Try this.

Mr. Xcoder

Posted 2017-12-08T11:18:45.970

Reputation: 39 774

0

PHP, 54 bytes

function(&$a){foreach($a as&$s)$i++&1&&$s=strrev($s);}

function works on argument (call by reference)

Titus

Posted 2017-12-08T11:18:45.970

Reputation: 13 814

0

Röda, 33 bytes

{enum|[_[::-1]]if[_%2=0]else[_1]}

Try it online!

Explanation:

{
 enum|     /* Zip elements in the stream with [0,1,...] */
           /* For each string _1 and index _2: */
 [_[::-1]] /*  Push _1 reversed */\
 if[_%2=0] /*   if the index is even */
 else[_1]  /*  else push _1 */
}

fergusq

Posted 2017-12-08T11:18:45.970

Reputation: 4 867

0

SNOBOL4 (CSNOBOL4), 89 bytes

R	X =X + 1
	N =INPUT	:F(END)
	OUTPUT =EQ(REMDR(X,2)) N :S(R)
	OUTPUT =REVERSE(N) :(R)
END

Try it online!

R	X =X + 1			;*Increment X
	N =INPUT	:F(END)		;*Read in input; when it's EOF, goto END
	OUTPUT =EQ(REMDR(X,2)) N :S(R)	;*if mod(X,2)==0, output N and goto R
	OUTPUT =REVERSE(N) :(R)		;*otherwise output reverse(n) and goto R
END

Giuseppe

Posted 2017-12-08T11:18:45.970

Reputation: 21 077

0

JavaScript (ES6), Firefox, 42 bytes, optimized from Arnauld's

a=>a.map(s=>[...s].sort(_=>a,a=!a).join``)

l4m2

Posted 2017-12-08T11:18:45.970

Reputation: 5 985

0

Clean, 59 bytes

import StdEnv
@t=[if(n rem 2<1)s(reverse s)\\s<-t&n<-[0..]]

Try it online!

Οurous

Posted 2017-12-08T11:18:45.970

Reputation: 7 916

0

Julia 0.6, 34 bytes

~x=x[2:2:end]=reverse.(x[2:2:end])

Try it online!

Function modifying the input in place.

LukeS

Posted 2017-12-08T11:18:45.970

Reputation: 421

0

Java 8, 53 bytes

a->{for(int i=1;i<a.length;i+=2)a[i]=a[i].reverse();}

Reverses every odd 0-indexed item.
Input as an array of StringBuffers. Modifies the input-array instead of returning a new one to save bytes.

Try it online.

Explanation:

a->{                       // Method with StringBuffer-array parameter and no return-type
  for(int i=1;i<a.length;  //  Loop `i` in the range `[1, input_length)
      i+=2)                //    And increase `i` by 2 after every iteration
    a[i]=a[i].reverse();}  //   Reverse the StringBuffer at index `i`

Kevin Cruijssen

Posted 2017-12-08T11:18:45.970

Reputation: 67 575

0

Zsh+coreutils, 31 bytes

for s;((i^=1))&&<<<$s||rev<<<$s

Try it online!

Repeated xoring will switch i between 0 and 1, so we alternate our output. Because the ternary chains our commands together, surrounding { } are unnecessary.

Zsh+coreutils, 23 bytes (almost correct)

for a b;<<<$a&&rev<<<$b

Try it online!

for sets b to the empty string if there are no more arguments. This unfortunately means that for an odd number of inputs, an extra empty line will be printed at the end.

GammaFunction

Posted 2017-12-08T11:18:45.970

Reputation: 2 838