38
\$\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\$
10
  • \$\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\$ 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\$ Dec 8 '17 at 13:12
  • 1
    \$\begingroup\$ @totallyhuman Yes, as per OP. \$\endgroup\$
    – Adám
    Dec 9 '17 at 20:07

53 Answers 53

1
2
1
\$\begingroup\$

Swift, 90 85 82 72 bytes

-10 bytes thanks to @Mr.Xcoder

func f(a:[String]){print(a.reduce([]){$0.map{"\($0.reversed())"}+‌​[$1]})}
\$\endgroup\$
1
  • \$\begingroup\$ You can use print and drop the return type declaration: func f(a:[String]){print(a.reduce([]){$0.map{"\($0.reversed())"}+[$1]})} \$\endgroup\$
    – Mr. Xcoder
    Dec 8 '17 at 20:40
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

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

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

\$\endgroup\$
1
\$\begingroup\$

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

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

Clean, 59 bytes

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

Try it online!

\$\endgroup\$
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\$
1
  • \$\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\$
3
  • \$\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\$

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\$
1
\$\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
\$\begingroup\$

Pip, 16 bytes

Flg{Po?lRVlo!:o}

This loops over each line and reverses it based on the value of variable o(Predefined to 1), which is inverted each time the loop runs.

Try it online!

Fi,#gPi%2?RVg@ig@i

This one is based on the indices of each line instead.

\$\endgroup\$
1
\$\begingroup\$

Jelly, 3 bytes

UÐe

Try it online!

(or UÐo, reversing the other lines.)

U      (Vectorized) reverse
 Ðe    the even numbered lines.

Essentially Mr. Xcoder's 5-byter, but with what I assume is a newer addition to the language.

\$\endgroup\$
1
\$\begingroup\$

Brachylog, 4 bytes

i↔ⁱ⁾

Try it online!

Doesn't feel entirely honest to call this generator a 4-byter considering it costs 3 more bytes to actually use it.

i       Take an element from the input paired with its 0-index,
 ↔ ⁾    and reverse the element
  ⁱ     repeatedly
   ⁾    a number of times equal to the index.
\$\endgroup\$
1
\$\begingroup\$

Japt -m, 4 3 bytes

I/O as an array of lines.

zVÑ

Try it

zVÑ     :Implicit map of each string at 0-based index V in input array
z       :Rotate 90 degrees clockwise
 EÑ     :V*2 times
\$\endgroup\$
1
\$\begingroup\$

Red, 61 bytes

f: func[s][forall s[if odd? length? s[reverse s/1]print s/1]]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 89 80 bytes

R	X =1 - X
	N =INPUT	:F(END)
	OUTPUT =EQ(X) N :S(R)
	OUTPUT =REVERSE(N) :(R)
END

Try it online!

\$\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\$

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\$

Vyxal, 3 bytes

yRY

Try it Online!

Look ma, no Unicode!

y   # Uninterleave
 R  # Vectorised reverse
  Y # Interleave
\$\endgroup\$
0
\$\begingroup\$

Python 3, 46 bytes

lambda l:[u[::(-1)**i]for i,u in enumerate(l)]

Try it online!

\$\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.