Wenn Sie eine Liste mit mindestens zwei Wörtern (die nur aus Kleinbuchstaben bestehen) erstellt haben, können Sie eine ASCII-Rangliste der Wörter erstellen und anzeigen, indem Sie die Schreibrichtung zuerst nach rechts und dann nach links relativ zur Anfangsrichtung von links nach rechts ändern .
Wenn Sie mit dem Schreiben eines Wortes fertig sind, ändern Sie die Richtung und beginnen Sie erst dann mit dem Schreiben des nächsten Wortes.
Wenn Ihre Sprache keine Wortlisten unterstützt oder es für Sie bequemer ist, können Sie die Eingabe als eine Folge von Wörtern, die durch ein einzelnes Leerzeichen getrennt sind, verwenden.
Führende und nachfolgende Leerzeichen sind zulässig.
["hello", "world"]
oder "hello world"
hello
w
o
r
l
d
Hier beginnen wir mit dem Schreiben hello
und wenn wir zum nächsten Wort kommen (oder bei der Eingabe als Zeichenkette - ein Leerzeichen wird gefunden), ändern wir die relative Richtung nach rechts und schreiben weiterworld
Testfälle:
["another", "test", "string"] or "another test string" ->
another
t
e
s
tstring
["programming", "puzzles", "and", "code", "golf"] or "programming puzzles and code golf" ->
programming
p
u
z
z
l
e
sand
c
o
d
egolf
["a", "single", "a"] or "a single a" ->
a
s
i
n
g
l
ea
Gewinnkriterien
Der kürzeste Code in Bytes in jeder Sprache gewinnt. Lassen Sie sich von den Golfsprachen nicht entmutigen!
Antworten:
Kohle , 9 Bytes
Probieren Sie es online! Link ist eine ausführliche Version des Codes. Erläuterung: Zeichnet den Text rückwärts und transponiert die Zeichenfläche nach jedem Wort. 10 Bytes für die Zeichenketteneingabe:
Try it online! Link is to verbose version of code. Explanation: Draws the text backwards, transposing the canvas for spaces.
quelle
C (gcc),
947874 bytes-4 from Johan du Toit
Try it online!
Prints the ladder, one character (of the input) at a time. Takes a space-separated string of words.
quelle
*s==32
into*s<33
to save a byte.05AB1E,
1916 bytes-3 bytes thanks to @Emigna.
Try it online.
General explanation:
Just like @Emigna's 05AB1E answer (make sure to upvote him btw!!), I use the Canvas builtin
Λ
.The options I use are different however (which is why my answer is longer..):
b
(the strings to print): I leave the first string in the list unchanged, and add the trailing character to each next string in the list. For example["abc","def","ghi","jklmno"]
would become["abc","cdef","fghi","ijklmno"]
.a
(the sizes of the lines): This would be equal to these strings, so[3,4,4,7]
with the example above.c
(the direction to print in):[2,4]
, which would map to[→,↓,→,↓,→,↓,...]
So the example above would step-by-step do the following:
abc
in direction2
/→
.cdef
in direction4
/↓
(where the first character overlaps with the last character, which is why we had to modify the list like this)fghi
in direction2
/→
again (also with overlap of trailing/leading characters)ijklmno
in direction4
/↓
again (also with overlap)Code explanation:
quelle
€θ¨õšsøJ
.€θ¨õšsøJ
areõIvy«¤})
,õUεXì¤U}
andε¯Jθ줈}
(the last two require--no-lazy
). Unfortunately, those are all the same length. This would be much easier if one of the variables defaulted to""
...""
..." Are you looking forõ
, or you mean ifX
/Y
/®
would have been""
? Btw, nice 13 byter in the comment of Emigna's answer. Quite different than both mine and his tbh, with the directions[→,↙,↓,↗]
that you've used.õ
is not a variable. Yes, I mean a variable that defaults to""
. I literally doõU
at the start of one of the snippets, so if X (or any other variable) defaulted to""
, it would trivially save two bytes. Thanks! Yeah, ↙↗ is a bit new, but I got the idea of interspersing the true writes with length 2 dummy writes from Emigna's answer.05AB1E,
1413 bytesSaved 1 byte thanks to Grimy
Try it online!
Explanation
quelle
€Y¦
could be2.ý
(not that it would save any bytes here). And this is the first time where I've seen the new behavior of€
in comparison to the regular map being useful..ý
used before but I've never used myself so I didn't think of it.€
is the regular map to me and I've often used it, the other one is the "new" map ;)Canvas,
17121110 bytesTry it here!
Explanation:
quelle
JavaScript (ES8),
91 7977 bytesTakes input as an array of words.
Try it online!
Commented
quelle
p
for keeping track of line endings is very smart +1Python 2, 82 bytes
Try it online!
quelle
brainfuck, 57 bytes
Try it online!
Takes input as NUL separated strings. Note that this is using EOF as 0, and will stop working when the ladder exceeds 256 spaces.
Explanation:
quelle
.
char in line 3 (of the commented version)? I was trying to play with the input on TIO. On Mac, I switched keyboard to Unicode text input and tried creating new word boundaries by typingoption+0000
but it didn't work. Any idea why not?-
instead of.
for the explanation. For adding NUL bytes in TIO, I recommend using the console and running a command like$('#input').value = $('#input').value.replace(/\s/g,"\0");
. I don't know why your way didn't workJavaScript, 62 bytes
Try it online!
Thanks Rick Hitchcock, 2 bytes saved.
JavaScript, 65 bytes
Try it online!
quelle
t
witha
, then removingt=
Aheui (esotope),
490458455 bytesTry it online!
Slightly golfed by using full-width characters(2 bytes) instead Korean(3 bytes).
Explanation
Aheui is befunge-like esolang. Here is code with color: ?1 part checks if current character is space or not.
?2 parts check whether words had written right-to-left or top-to-down.
?3 part is break condition of loop which types spaces.
?4 parts check if current character is end of line(-1).
Red part is stack initialization. Aheui uses stacks (from
Nothing
toㅎ
: 28 stacks) to store value.Orange part takes input(
뱋
) and check if it is space, by subtracting with32
(ascii code of space).Green part add 1 to stack which stores value of length of space, if writing right-to-left.
Purple part is loop for printing spaces, if writing up-to-down.
Grey part check if current character is
-1
, by adding one to current character.Blue part prints current character, and prepare for next character.
quelle
Japt
-P
, 15 bytesTry it
quelle
bash, 119 chars
This uses ANSI control sequences to move the cursor - here I'm only using save
\e7
and restore\e8
; but the restore has to be prefixed with\n
to scroll the output if it's already at the bottom of the terminal. For some reason it doesn't work if you're not already at the bottom of the terminal. * shrug *The current character
$c
is isolated as a single-character substring from the input string$w
, using thefor
loop index$i
as the index into the string.The only real trick I'm using here is
[ -z $c ]
which will returntrue
, i.e. the string is blank, when$c
is a space, because it's unquoted. In correct usage of bash, you would quote the string being tested with-z
to avoid exactly this situation. This allows us flip the direction flag$d
between1
and0
, which is then used as an index into the ANSI control sequence array,X
on the next non-space value of$c
.I'd be interested to see something that uses
printf "%${x}s" $c
.Oh gosh let's add some whitespace. I can't see where I am...
quelle
Perl 6, 65 bytes
Try it online!
Anonymous code block that takes a list of words and prints straight to STDOUT.
Explanation
quelle
Charcoal, 19 bytes
Input as a list of strings
Try it online (verbose) or try it online (pure)
Explanation:
Loop in the range
[0, input-length)
:If the index is odd:
Print the string at index
i
in a downward direction:And then move the cursor once towards the top-right:
Else (the index is even):
Print the string at index
i
in a regular right direction:And then move the cursor once towards the bottom-left:
quelle
Python 2,
8988 bytesTry it online!
quelle
C# (Visual C# Interactive Compiler), 122 bytes
Try it online!
quelle
J,
474543 bytesTry it online!
I found a fun, different approach...
I started messing around with left pads and zips with cyclic gerunds and so on, but then I realized it would be easier to just calculate each letter's position (this boils down to a scan sum of the correctly chosen array) and apply amend
}
to a blank canvas on the razed input.The solution is handled almost entirely by Amend
}
:; ( single verb that does all the work ) ]
overall fork;
left part razes the input, ie, puts all the letters into a contiguous string]
right part is the input itself(stuff)}
we use the gerund form of amend}
, which consists of three partsv0`v1`v2
.v0
gives us the "new values", which is the raze (ie, all the characters of the input as one string), so we use[
.v2
gives us the starting value, which we are transforming. we simply want a blank canvas of spaces of the needed dimensions.([ ' '"0/ [)
gives us one of size(all chars)x(all chars)
.v1
selects which positions we will put our replacement characters in. This is the crux of the logic...0 0
in the upper left, we notice that each new character is either 1 to the right of the previous position (ie,prev + 0 1
) or one down (ie,prev + 1 0
). Indeed we do the former "len of word 1" times, then the latter "len of word 2" times, and so on, alternating. So we'll just create the correct sequence of these movements, then scan sum them, and we'll have our positions, which we then box because that's how Amend works. What follows is just the mechanics of this idea...([: <@(+/)\ #&> # # $ 1 - e.@0 1)
#:@1 2
creates the constant matrix0 1;1 0
.# $
then extends it so it has as many rows as the input. eg, if the input contains 3 words it will produce0 1;1 0;0 1
.#&> #
the left part of that is an array of the lengths of the input words and#
is copy, so it copies0 1
"len of word 1" times, then1 0
"len of word 2 times", etc.[: <@(+/)\
does the scan sum and box.quelle
T-SQL, 185 bytes
Try it online
quelle
Retina, 51 bytes
Try it online!
A rather straightforward approach that marks every other word and then applies the transformation directly.
Explanation
We mark every other word with a semicolon by matching each word, but only applying the replacement to the matches (which are zero indexed) starting from match 1 and then 3 and so on.
+(m
sets some properties for the following stages. The plus begins a "while this group of stages changes something" loop, and the open bracket denotes that the plus should apply to all of the following stages until there is a close bracket in front of a backtick (which is all of the stages in this case). Them
just tells the regex to treat^
as also matching from the beginning of lines instead of just the beginning of the string.The actual regex is pretty straightforward. We simply match the appropriate amount of stuff before the first semicolon and then use Retina's
*
replacement syntax to put in the correct number of spaces.This stage is applied after the last one to remove semicolons and spaces at the end of words that we changed to vertical.
quelle
Retina 0.8.2, 58 bytes
Try it online! Link includes test cases. Alternative solution, also 58 bytes:
Try it online! Link includes test cases.
I'm deliberately not using Retina 1 here, so I don't get operations on alternate words for free; instead I have two approaches. The first approach splits on all letters in alternate words by counting preceding spaces, while the second approach replaces alternate spaces with newlines and then uses the remaining spaces to help it split alternate words into letters. Each approach has to then join the last vertical letter with the next horizontal word, although the code is different because they split the words in different ways. The final stage of both approaches then pads each line until its first non-space character is aligned under the last character of the previous line.
Note that I don't assume that words are just letters because I don't have to.
quelle
PowerShell,
101 8983 bytes-12 bytes thanks to mazzy.
Try it online!
quelle
& $b @p
(each word as one argument), 3) use shorter form fornew line
constant. see 3,4 line at this examplefoo
. see the code.Given a list of at least two words...
PowerShell,
7465 bytesTry it online!
quelle
R, 126 bytes
Try it online!
quelle
T-SQL, 289 bytes
This runs on SQL Server 2016 and other versions.
@ holds the space-delimited list. @I tracks the index position in the string. @S tracks the total number of spaces to indent from the left. @B tracks which axis the string is aligned with at point @I.
The byte count includes the minimal example list. The script goes through the list, character by character, and changes the string so that it will display according to the requirements. When the end of the string is reached, the string is PRINTed.
quelle
JavaScript (Node.js), 75 bytes
Try it online!
Explanation and ungolfed
quelle
Stax, 12 bytes
Run and debug it
quelle
Jelly, 21 bytes
Try it online!
A full program taking the input as a list of strings and implicitly outputting to stdout the word ladder.
quelle
C (gcc),
9387 bytesThanks to gastropner for the suggestions.
This version takes an array of strings terminated by a NULL pointer.
Try it online!
quelle
Brain-Flak, 152 bytes
Try it online!
I suspect this can be shorter by combining the two loops for odd and even words.
quelle
J,
3533 bytesThis is a verb that takes the input as a single string with the words separated by spaces. For example, you could call it like this:
The output is a matrix of letters and spaces, which the interpreter outputs with newlines as required. Each line will be padded with spaces so they have the exact same length.
There's one slight problem with the code: it won't work if the input has more than 98 words. If you want to allow a longer input, replace the
_98
in the code by_998
to allow up to 998 words, etc.Let me explain how this works through some examples.
Suppose we have a matrix of letters and spaces that we imagine is a partial output for some words, starting with a horizontal word.
How could we prepend a new word before this, vertically? It's not hard: just turn the new word to a single-column matrix of letters with the verb
,.
, then append the output to that single-column matrix. (The verb,.
is convenient because it behaves as an identity function if you apply it to a matrix, which we use for golfing.)Now we can't just iterate this way of prepending a word as is, because then we'd only get vertical words. But if we transpose the output matrix between each step, then every other word will be horizontal.
So our first attempt for a solution is to put each word into a single-column matrix, then fold these by appending and transposing between them.
But there's a big problem with this. This puts the first letter of the next word before turning a right angle, but the specification requires turning before putting the first letter, so the output should be something like this instead:
The way we achieve this is to reverse the entire input string, as in
then use the above procedure to build the zig-zag but turning only after the first letter of each word:
Then flip the output:
But now we have yet another problem. If the input has an odd number of words, then the output will have the first word vertical, whereas the specification says that the first word must be horizontal. To fix this, my solution pads the list of words to exactly 98 words, appending empty words, since that doesn't change the output.
quelle