Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

deleted 497 characters in body
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394

More interesting answer using the Canvas builtin (27(削除) 27 (削除ここまで) 25 bytes):
20(削除) 20 (削除ここまで) 19 bytes if trailing whitespaces were allowed: try it online try it online.

€g€D ̈2.ýIðIδ∍€gDø4.ιJŽDāSý ̃I€ûðoý374S.Λ¶¡ðδÜ

-2 bytes thanks to @emanresuA.

Input as a list of words; output as a list of lines.

Try it online. Try it online.

€gDø4.ý ̃I€ûðoý374S.Λ¶¡ðδÜ
€g # Get the length of each word of the (implicit) input-list
 €D # Create a copy ofConvert each length in the list,value keepingto a flattened list
   ̈ pair of this value #with Removea theDuplicate trailing+ itemZip/transpose
 24.ý # Intersperse itthis list of pairs with 24 as delimiter
I  ̃ # Push the input-list of words again
 ð Flatten #it Pushto a space character: "single "list
I I # Push the input-list yetof words again
 €û δ # Map overPalindromize each word in the input-list with the " " as argument:
 ∍ # Extend the space to a length equal to the word
 ðoý  # InterleaveJoin this list of spaces with the list of words
 J # Join" it to" a(2 singlespaces) stringdelimiter
ŽDā # Push compressed integer 3474374
 S # Convert it to a list of digits: [3,7,4]
.Λ # Use the modifiable Canvas builtin with these three arguments
 ¶¡ # Split it on newlines
 δ # Map over each inner line:
 ð Ü # Trim trailing spaces
 # (after which the list of lines is output implicitly)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why ŽDā is 3474.

Additional information about the Canvas builtin Λ/:

€g€D ̈2.ýIðIδ∍€gDø4.ιJŽDāSý ̃I€ûðoý374S creates the following Canvas arguments:

  • Lengths (€g€D ̈2€gDø4.ýý ̃): e.g. [11,2,11,24,7,2,7,24,3,2,3,24,4,24,4,24,4] for input ["programming","puzzles","and","code","golf"]
  • Characters (IðIδ∍.ιJI€ûðoý): e.g. "programming puzzles and code"programmingnimmargorp puzzleselzzup golfandna codedoc "golflog" for input ["programming","puzzles","and","code","golf"]
  • Directions (ŽDāS374S): [3,4,7,4], which translates to \$[↘,↓,↖,↓]\$\$[↘,↖,↓]\$

Step 2: Draw 211-1 characters (" ""nimmargorp") in direction 47 (\$↓\$\$↖\$):

p
 r
 o
 g
 r
 a
 m
 m
 i
 n
 g

Step 3: Draw 114-1 characters (" ") in direction 7 (\$↖\$):

p
 r
 o
 g
 r
 a
 m
 m
 i
 n
 g
 

Step 4: Draw 2-1 characters ("p"p") in direction 4 (\$↓\$):

p
 r
p o
 g
 r
 a
 m
 m
 i
 n
 g

Step 54: Draw 7-1 characters ("uzzles") in direction 3 (\$↘\$):

p
 r
p o
 u g
 z r
 z a
 l m
 e m
 s i
 n
 g

IðIδ∍€gDø4.ιJý ̃ could alternatively be εðy∍«}J or íD€gúíJ εgD4)} ̃ ̈ or €gDø4ドル¦ ̃, but I've been unable to find anything shorter.

More interesting answer using the Canvas builtin (27 bytes):
20 bytes if trailing whitespaces were allowed: try it online.

€g€D ̈2.ýIðIδ∍.ιJŽDāS.Λ¶¡ðδÜ

Input as a list of words; output as a list of lines.

Try it online.

€g # Get the length of each word of the (implicit) input-list
 €D # Create a copy of each length in the list, keeping a flattened list
   ̈  # Remove the trailing item
 2.ý # Intersperse it with 2 as delimiter
I # Push the input-list of words again
 ð  # Push a space character: " "
 I # Push the input-list yet again
 δ # Map over each word in the input-list with the " " as argument:
 ∍ # Extend the space to a length equal to the word
  # Interleave this list of spaces with the list of words
 J # Join it to a single string
ŽDā # Push compressed integer 3474
 S # Convert it to a list of digits
.Λ # Use the modifiable Canvas builtin with these three arguments
 ¶¡ # Split it on newlines
 δ # Map over each inner line:
 ð Ü # Trim trailing spaces
 # (after which the list of lines is output implicitly)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why ŽDā is 3474.

Additional information about the Canvas builtin Λ/:

€g€D ̈2.ýIðIδ∍.ιJŽDāS creates the following Canvas arguments:

  • Lengths (€g€D ̈2.ý): e.g. [11,2,11,2,7,2,7,2,3,2,3,2,4,2,4,2,4] for input ["programming","puzzles","and","code","golf"]
  • Characters (IðIδ∍.ιJ): e.g. "programming puzzles and code golf " for input ["programming","puzzles","and","code","golf"]
  • Directions (ŽDāS): [3,4,7,4], which translates to \$[↘,↓,↖,↓]\$

Step 2: Draw 2-1 characters (" ") in direction 4 (\$↓\$):

p
 r
 o
 g
 r
 a
 m
 m
 i
 n
 g

Step 3: Draw 11-1 characters (" ") in direction 7 (\$↖\$):

p
 r
 o
 g
 r
 a
 m
 m
 i
 n
 g
 

Step 4: Draw 2-1 characters ("p") in direction 4 (\$↓\$):

p
 r
p o
 g
 r
 a
 m
 m
 i
 n
 g

Step 5: Draw 7-1 characters ("uzzles") in direction 3 (\$↘\$):

p
 r
p o
 u g
 z r
 z a
 l m
 e m
 s i
 n
 g

IðIδ∍.ιJ could alternatively be εðy∍«}J or íD€gúíJ, but I've been unable to find anything shorter.

More interesting answer using the Canvas builtin ((削除) 27 (削除ここまで) 25 bytes):
(削除) 20 (削除ここまで) 19 bytes if trailing whitespaces were allowed: try it online.

€gDø4.ý ̃I€ûðoý374S.Λ¶¡ðδÜ

-2 bytes thanks to @emanresuA.

Input as a list of words; output as a list of lines.

Try it online.

€gDø4.ý ̃I€ûðoý374S.Λ¶¡ðδÜ
€g # Get the length of each word of the (implicit) input-list
  # Convert each value to a pair of this value with a Duplicate + Zip/transpose
 4.ý # Intersperse this list of pairs with 4 as delimiter
  ̃ # Flatten it to a single list
I # Push the input-list of words again
 €û # Palindromize each word
 ðoý # Join this list with " " (2 spaces) delimiter
ŽDā # Push 374
 S # Convert it to a list of digits: [3,7,4]
.Λ # Use the modifiable Canvas builtin with these three arguments
 ¶¡ # Split it on newlines
 δ # Map over each inner line:
 ð Ü # Trim trailing spaces
 # (after which the list of lines is output implicitly)

Additional information about the Canvas builtin Λ/:

€gDø4.ý ̃I€ûðoý374S creates the following Canvas arguments:

  • Lengths (€gDø4.ý ̃): e.g. [11,11,4,7,7,4,3,3,4,4,4,4,4,4] for input ["programming","puzzles","and","code","golf"]
  • Characters (I€ûðoý): e.g. "programmingnimmargorp puzzleselzzup andna codedoc golflog" for input ["programming","puzzles","and","code","golf"]
  • Directions (374S): [3,7,4], which translates to \$[↘,↖,↓]\$

Step 2: Draw 11-1 characters ("nimmargorp") in direction 7 (\$↖\$):

p
 r
 o
 g
 r
 a
 m
 m
 i
 n
 g

Step 3: Draw 4-1 characters (" p") in direction 4 (\$↓\$):

p
 r
p o
 g
 r
 a
 m
 m
 i
 n
 g

Step 4: Draw 7-1 characters ("uzzles") in direction 3 (\$↘\$):

p
 r
p o
 u g
 z r
 z a
 l m
 e m
 s i
 n
 g

€gDø4.ý ̃ could alternatively be εgD4)} ̃ ̈ or €gDø4ドル¦ ̃, but I've been unable to find anything shorter.

Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394

05AB1E, 17 bytes

12 bytes if trailing whitespaces were allowed: try it online.

ζε2úíJNúS}ζJðδÜõÜ

Port of @Shaggy's Japt answer.

Input as a list of list of characters; output as a list of lines.

Try it online.

More interesting answer using the Canvas builtin (27 bytes):
20 bytes if trailing whitespaces were allowed: try it online.

€g€D ̈2.ýIðIδ∍.ιJŽDāS.Λ¶¡ðδÜ

Input as a list of words; output as a list of lines.

Try it online.

Explanation:

ζ # Zip/transpose; swapping rows/columns of the (implicit) input-list of lists
 # of characters, with a space " " as filler for shorter lists
 ε # Map over each inner lists of characters:
 2ú # Pad each inner character with 2 leading spaces
 í # Reverse each inner string, so the spaces are trailing
 J # Join this list together to a single string
 Nú # Pad the 0-based map-index amount of leading spaces
 S # Convert the string back to a list of characters
 }ζ # After the map: Zip/transpose back again with " " filler
 J # Join each inner list together to a single string
 δ # Map over each inner line:
 ð Ü # Trim trailing spaces
 õÜ # Trim trailing empty strings from the list
 # (after which the list of lines is output implicitly)
€g # Get the length of each word of the (implicit) input-list
 €D # Create a copy of each length in the list, keeping a flattened list
 ̈ # Remove the trailing item
 2.ý # Intersperse it with 2 as delimiter
I # Push the input-list of words again
 ð # Push a space character: " "
 I # Push the input-list yet again
 δ # Map over each word in the input-list with the " " as argument:
 ∍ # Extend the space to a length equal to the word
 .ι # Interleave this list of spaces with the list of words
 J # Join it to a single string
ŽDā # Push compressed integer 3474
 S # Convert it to a list of digits
.Λ # Use the modifiable Canvas builtin with these three arguments
 ¶¡ # Split it on newlines
 δ # Map over each inner line:
 ð Ü # Trim trailing spaces
 # (after which the list of lines is output implicitly)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why ŽDā is 3474.

Additional information about the Canvas builtin Λ/:

It takes 3 arguments to draw an ASCII shape:

  1. Length of the lines we want to draw
  2. Character/string to draw
  3. The direction to draw in, where each digit represents a certain direction:
7 0 1
 ↖ ↑ ↗
6 ← X → 2
 ↙ ↓ ↘
5 4 3

€g€D ̈2.ýIðIδ∍.ιJŽDāS creates the following Canvas arguments:

  • Lengths (€g€D ̈2.ý): e.g. [11,2,11,2,7,2,7,2,3,2,3,2,4,2,4,2,4] for input ["programming","puzzles","and","code","golf"]
  • Characters (IðIδ∍.ιJ): e.g. "programming puzzles and code golf " for input ["programming","puzzles","and","code","golf"]
  • Directions (ŽDāS): [3,4,7,4], which translates to \$[↘,↓,↖,↓]\$

Because we have a list of lengths, those are leading here.

Step 1: Draw 11 characters ("programming") in direction 3 (\$↘\$):

p
 r
 o
 g
 r
 a
 m
 m
 i
 n
 g

Step 2: Draw 2-1 characters (" ") in direction 4 (\$↓\$):

p
 r
 o
 g
 r
 a
 m
 m
 i
 n
 g
 

Step 3: Draw 11-1 characters (" ") in direction 7 (\$↖\$):

p
 r
 o
 g
 r
 a
 m
 m
 i
 n
 g
 

Step 4: Draw 2-1 characters ("p") in direction 4 (\$↓\$):

p
 r
p o
 g
 r
 a
 m
 m
 i
 n
 g
 

Step 5: Draw 7-1 characters ("uzzles") in direction 3 (\$↘\$):

p
 r
p o
 u g
 z r
 z a
 l m
 e m
 s i
 n
 g
 

etc.

See this 05AB1E tip of mine for an in-depth explanation of the Canvas builtin.

IðIδ∍.ιJ could alternatively be εðy∍«}J or íD€gúíJ, but I've been unable to find anything shorter.

AltStyle によって変換されたページ (->オリジナル) /