27
\$\begingroup\$

Background

When I was younger, I was taught a method of drawing a weird "S" shape, that I (along with my classmates) found fascinating. Today, I rediscovered it, and due to its formulaic approach to drawing it, thought it could lead to an interesting challenge :P

Drawing the "S"

The S can be drawn by following these simple steps:

First, draw 2 rows of three vertical lines like so

| | |

| | |

Next, connect the top left line with the bottom middle line and the top middle with the bottom right line to produce

| | |
 \ \
| | |

Finally, draw a top and bottom on the currently drawn image so that it ends up looking like

  ^
 / \
| | |
 \ \
| | |
 \ /
  v

As you can see, this results in an "S" shape. When extended however (drawing it with more than 2 rows), it produces a very interesting pattern. Your task is reproduce this interesting pattern.

Task

Given an integer where n >= 2, output The S with n rows to be made from it. Output may be returned from a function, and input may be taken in standard methods. Trailing/leading whitespace for both the overall image, as well as each line, is fine. However, leading line spaces must be consistent so that the " isn't broken. You may output as a list of lines.

Test cases

input
output
---

2

  ^
 / \
| | |
 \ \
| | |
 \ /
  v

---

8
  ^
 / \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ /
  v

---

10

  ^
 / \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ \
| | |
 \ /
  v

This is a so shortest code wins! Good luck,

\$\endgroup\$
  • 13
    \$\begingroup\$ Wikipedia calls the S the Cool S, and calls an infinite version of the an S Chain \$\endgroup\$ – Stephen Sep 20 '17 at 19:16
  • \$\begingroup\$ Can we output as a list of lines? \$\endgroup\$ – Mr. Xcoder Sep 20 '17 at 19:17
  • \$\begingroup\$ @Mr.Xcoder you may \$\endgroup\$ – caird coinheringaahing Sep 20 '17 at 19:17
  • \$\begingroup\$ @Stephen 10 minutes googling and this didn't come up. I'll edit the name of the question \$\endgroup\$ – caird coinheringaahing Sep 20 '17 at 19:18
  • \$\begingroup\$ @cairdcoinheringaahing I searched images, that might have been it :P \$\endgroup\$ – Stephen Sep 20 '17 at 19:19

39 Answers 39

0
\$\begingroup\$

SpecBAS - 74 bytes

1 INPUT n
2  ?"  ^"'" / \"'("| | |"#13" \ \"#13)*(n-1);"| | |"'" \ /"'"  v"

Apostrophe moves to next line, but doesn't work inside the string being repeated, so have to use #13 to insert line feeds in that part.

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

QBIC, 48 bytes

?@  ^`?@ / \ `[:|?@| | |`~a=b|?_fB|?@  v`\?@ \ \

Explanation

Note that every '?' inserts a linebreak
Also note that @...` creates a string literal and assigns that to A$, B$, ... Z$
?@  ^`      print ^ prefixed with 2 spaces
?@ / \ `    print the other start bit, pre- and postfixed with spaces
[:|         FOR a = 1 to (chain-length given as cmd line param, read as 'b')
?@| | |`    PRINT the pipes
~a=b|       IF we are in the last iteration THEN
?_fB|           print the reversed of B$
                    _f...| flips a string
                    B$ holds " / \ ", which reversed is " \ / "
?@  v`          and print "  v"
\           ELSE
?@ \ \          print the connector to the next section
The final string lit, the IF and the FOR are auto-closed at EOF
\$\endgroup\$
0
\$\begingroup\$

Batch, 109 bytes

I believe this isn't completely golfed yet.

@echo   ^^
@echo  / \
@for /l %%G in (2,1,%1)do @echo ^| ^| ^|&@echo  \ \
@echo ^| ^| ^|
@echo  \ /
@echo   v

This script just:

  • Output the top of the Cool S
  • Loop n times the chain body
  • Output the bottom of the Cool S
\$\endgroup\$
0
\$\begingroup\$

R, 90 bytes

function(n){cat('  ^
 / \\
| | |
')
for(i in 2:n-1)cat(' \\ \\
| | |
')
cat(' \\ /
  v
')}

Try it online!

pretty lame, but this is the best we've got to work with in R.

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

Retina, 37 bytes

.*
$*
1
\¶| | |¶ \ 
^
  ^¶ / 
$
/¶  v

Try it online!

I must be missing something, this is a super basic approach...

\$\endgroup\$
  • \$\begingroup\$ You did only outgolf Martin by 1 byte, so you probably aren't missing something \$\endgroup\$ – caird coinheringaahing Oct 4 '17 at 12:57
0
\$\begingroup\$

Excel VBA, 72 Bytes

Anonymous VBE immediate window function that takes input from cell A1 and outputs to the VBE immediate window

a="| | |":?"  ^":?" / \":For i=2To[A1]:?a:?" \ \":Next:?a:?" \ /":?"  v"
\$\endgroup\$
0
\$\begingroup\$

Ruby, 39 bytes

->n{"  ^
 / #{"\\
| | |
 \\ "*n}/
  v"}

Try it online!

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

J, 51 bytes

'  v',~' \ /',~'  ^',' / \',[:}:'| | | \ \ '$~5,~+:

Try it online!

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

PHP, 73 bytes

<?='  ^
 / \
'.join('
 \ \
',array_fill(0,$argv[1],'| | |')).'
 \ /
  v';

Try it online!

\$\endgroup\$

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.