Бустрофедон представляет собой текст , в котором каждая вторая линия письма переворачивается или обратить вспять, с перевернутым буквами.
В этой задаче мы просто перевернем все остальные строки, но оставим действительные символы без изменений. Вы можете выбрать, какие линии повернуть вспять, при условии, что это будут все остальные.
Вы можете взять текст в любом подходящем формате, если вы поддерживаете ноль или более строк печатного ASCII, каждая из которых содержит ноль или более символов.
Примеры:
["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"]
Ответы:
APL (Dyalog Classic) , 4 байта
Ввод представляет собой вектор символьных векторов.
⌽
это функция, которая обращает вектор (когда⌽
is applied monadically).⊢
is " dex " - функция, которая возвращает правильный аргумент. Когда в составе (∘
) с другой функцией ,f
что заставляет последнее монадическими , какA ⊢∘f B
эквивалентноA ⊢ (f B)
и , следовательноf B
.\
is the scan operator.g\A B C ...
is the vectorA (A g B) (A g (B g C)) ...
whereg
is applied dyadically (infix notation). Substituting⊢∘⌽
forg
it simplifies to:The reversals at even positions (or odd, depending on how you count) cancel out.
Try it online!
источник
]&|.&.>/\
for those who can read J.Haskell, 26 bytes
Try it online! Usage example:
zipWith($)l ["abc","def","ghi"]
yields["abc","fed","ghi"]
.Explanation:
l
is an infinite list of functions alternating between theid
entity function and thereverse
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"]
.источник
Husk, 4 bytes
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
In Husk, multiplying a string with a number repeats it that many times, also reversing it if the number is negative.
источник
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.
Test cases
Show code snippet
Or Try it online! (SpiderMonkey)
JavaScript (ES6), 45 bytes
Test cases
Show code snippet
источник
APL (Dyalog Unicode), 10 bytes
Works both ways:
Try it online! with
⎕IO←1
Try it online! with
⎕IO←0
How it works:
источник
Perl 5, 17 + 2 (-pl) = 19 bytes
odd lines reversed
even lines reversed
After @Martin's comment : input needs to have a trailing linefeed.
try it online
источник
Haskell, 27 bytes
Try it online!
Haskell, 30 bytes
Try it online!
Recursion FTW.
источник
05AB1E,
54 bytesTry it online!
Explanation
источник
K (oK),
1714 bytesSolution:
Try it online!
Example:
Explanation:
Apply
reverse
at odd indices of the input list:Notes:
&(#x)#0 1
for&2!!#x
to save 3 bytesисточник
APL (Dyalog), 12 bytes
Try it online!
источник
Python 2,
4036 bytes-4 bytes thanks to @Mr.Xcoder
Try it online!
Outputs by modifying the input list
Python 2, 43 bytes
Try it online!
источник
Alumin, 66 bytes
Try it online!
источник
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!
источник
R, 85 bytes
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.
источник
Jelly,
54 bytesTry it online!
Thanks HyperNeutrino for -1 bytes! (actually because I never knew how
ƭ
works due to lack of documentation, this time I got lucky)источник
¦
withm
(7 bytes).s2U2¦€;/
is also 7 bytes.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):
So to select and reverse alternating rows:
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 theORDER BY
, so if we had, say, 10,000 rows, we would definitely need this.источник
Perl 6, 44 bytes
Try it
источник
Comma delimited:
Stax, 12 bytes
Run and debug it
input: ABC,def,GHI,jkl,MNO,pqr,STU
Newline delimited:
Stax, 8 bytes
Run and debug it
input:
output for both:
источник
Actually, 7 bytes
Explanation:
Try it online!
источник
Alice, 13 bytes
Try it online!
Input via separate command-line arguments. Reverses the first line (and every other line after that).
Explanation
источник
Standard ML (MLton), 51 bytes
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 stringsa
andb
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
).источник
Retina, 18 bytes
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
;
.источник
Wolfram Language (Mathematica), 33 bytes
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.Fold
ing the input with respect to the above means we:Each line gets reversed one time fewer than the previous line, so the lines alternate direction.
источник
CJam, 11 bytes
Try it online! (CJam array literals use spaces to separate elements)
Explanation:
Explanation for the
Waf.%
"black magic" part:W
is a variable preinitialized to-1
.a
wraps an element in an array, soWa
is[-1]
.%
pops a numbern
and an arraya
and takes everyn
th element of the array. Whenn
is negative, it also reverses it, meaning thatW%
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 thatWa.%
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 thatWa.f%
reverses the first element of every element in the array.источник
V, 4 bytes
Try it online!
источник
Swift,
90858272 bytes-10 bytes thanks to @Mr.Xcoder
источник
print
and drop the return type declaration:func f(a:[String]){print(a.reduce([]){$0.map{"\($0.reversed())"}+[$1]})}
Ruby, 19 + 2 = 21 bytes
+2 bytes for
-nl
flags.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:
The
-p
option makes Ruby effectively wrap your script in a loop like this:The special variable
$_
contains the last line read bygets
, and$.
contains the line number.The
-l
enables automatic line ending processing, which automatically callschop!
on each input line, which removes the the\n
before we reverse it.источник
GNU sed, 31 + 1 = 32 bytes
+1 byte for
-r
flag.Try it online!
Explanation
источник
Charcoal, 9 bytes
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:
источник
Befunge-93, 48 bytes
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
источник