35
\$\begingroup\$

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"]
\$\endgroup\$
  • \$\begingroup\$ Can't understand if return and input need to be text separated lines or it can be a file or a list of lines. \$\endgroup\$ – sergiol Dec 8 '17 at 12:30
  • \$\begingroup\$ @sergiol Default PPCG I/O rules apply. \$\endgroup\$ – Adám Dec 8 '17 at 12:35
  • \$\begingroup\$ Can my code behave inconsistently, i.e. sometimes start reversing from the first line and sometimes from the second? \$\endgroup\$ – Erik the Outgolfer Dec 8 '17 at 13:02
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Martin Ender Dec 8 '17 at 13:12
  • 1
    \$\begingroup\$ @totallyhuman Yes, as per OP. \$\endgroup\$ – Adám Dec 9 '17 at 20:07

48 Answers 48

1
2
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ @LuisMendo Thanks (overlooked this comment for quite some time) \$\endgroup\$ – Sanchises Dec 21 '17 at 9:08
1
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ 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));} \$\endgroup\$ – gastropner Dec 9 '17 at 9:45
  • \$\begingroup\$ 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]));} \$\endgroup\$ – gastropner Dec 9 '17 at 10:04
  • \$\begingroup\$ @gastropner Thanks! I don't think using printf to print *l works, though, because the string might for example contain %d. \$\endgroup\$ – Steadybox Dec 10 '17 at 15:00
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Tcl, 61 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

Red, 68 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

PHP, 54 bytes

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

function works on argument (call by reference)

\$\endgroup\$
0
\$\begingroup\$

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 */
}
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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

a=>a.map(s=>[...s].sort(_=>a,a=!a).join``)
\$\endgroup\$
0
\$\begingroup\$

Clean, 59 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Julia 0.6, 34 bytes

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

Try it online!

Function modifying the input in place.

\$\endgroup\$
0
\$\begingroup\$

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`
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.