28
\$\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\$
10
  • 14
    \$\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

47 Answers 47

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

Jelly, 32 bytes

Boring port of Lynn's Python solution.

“\¶| | |¶ \ ”ẋṭ“  ^¶ / ”;“/¶  v”

Try it online!

Explanation:

“\¶| | |¶ \ ”ẋṭ“  ^¶ / ”;“/¶  v”    Example input: 5
“\¶| | |¶ \ ”                       Literal string "\¶| | |¶ \ " (¶ = newline). Result: "\¶| | |¶ \ "
             ẋ                      Repeat as many times as the (implicit) input. Result: "\¶| | |¶ \ \¶| | |¶ \ \¶| | |¶ \ \¶| | |¶ \ \¶| | |¶ \ "
              ṭ                     Tack that on the end of...
               “  ^¶ / ”            ...the string "  ^¶ / ". Result: "  ^¶ / \¶| | |¶ \ \¶| | |¶ \ \¶| | |¶ \ \¶| | |¶ \ \¶| | |¶ \ "
                        ;           Append...
                         “/¶  v”    The string "/¶  v". Result: "  ^¶ / \¶| | |¶ \ \¶| | |¶ \ \¶| | |¶ \ \¶| | |¶ \ \¶| | |¶ \ /¶  v"
                                    Implicit print
\$\endgroup\$
1
\$\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\$
1
\$\begingroup\$

Retina, 37 bytes

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

Try it online!

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

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

Actually, 30 bytes

This works on the same principle as ETHproductions's Japt answer, where extra spaces are added later, and the whole string is split into rows of 5 for implicit printing. Try it online!

"\|||\"*" ^ /"+"/ v"@+#' j5@╪i

Ungolfing

            Implicit input.
"\|||\"*    Add the middle portion and multiply that by the input.
" ^ /"+     Append the top.
"/ v"@+     Append the bottom.
#           Convert into a list of strings
' j         Join with spaces.
5@╪         Split into a list of length-5 strings.
i           Flatten list onto the stack for implicit printing with newlines.
\$\endgroup\$
1
  • \$\begingroup\$ Nice job outgolfing Mego in his own language! \$\endgroup\$ – caird coinheringaahing Oct 4 '17 at 13:46
1
\$\begingroup\$

Jelly, 25 23 bytes

This works on the same principle as ETHproductions's Japt answer, where extra spaces are added later, and the whole string is split into strings of length 5 before printing. Try it online!

Edit: I knew there was a way to join the top and bottom of the S-chain in a golfier way. Thanks to Erik the Outgolfer for -2 bytes.

“\|||\”ẋ“ ^ /“/ v”jKs5Y

Ungolfing

                Left argument: n
“\|||\”ẋ        Repeat the middle portion n times.
“ ^ /“/ v”j     Append the top and bottom.
K               Join with spaces.
s5              Split into a list of length-5 strings.
Y               Print the strings with linefeeds.
\$\endgroup\$
1
  • \$\begingroup\$ “\|||\”ẋ“ ^ /“/ v”jKs5Y \$\endgroup\$ – Erik the Outgolfer Oct 4 '17 at 14:29
1
\$\begingroup\$

Charcoal, 25 bytes

↘^\¶/¶G→⁵↓⊕⊗N←⁵|¶ \↗¶\¶v/

Try it online! Link is to verbose version of code.

\$\endgroup\$
2
  • \$\begingroup\$ Funny how the other Charcoal answer is the same byte-count by using an completely different approach. I like yours more, though. The other is close to hard-coded, but yours is actually using Charcoal's strength. \$\endgroup\$ – Kevin Cruijssen Oct 27 '17 at 12:13
  • \$\begingroup\$ @KevinCruijssen You say strength, but that should really be printing diagonally; sadly the best I could do there was 29 bytes: ↘^\|¶/|\|¶|F⊖N↘\|\|¶|↘\|/¶|\v \$\endgroup\$ – Neil Oct 27 '17 at 13:48
1
\$\begingroup\$

PHP, 73 bytes

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

Try it online!

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

J, 51 50 48 bytes

'  v',~'  ^','/'(0 1;_1 3)}' \ \ | | |'$~5,~1++:

Try it online!

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

x86_16 machine code - 76 bytes

8B CB         MOV CX, BX
49            DEC CX
B4 09         MOV AH, 09H 
           ATAS:
BA 02 01      MOV DX, OFFSET RANTAI_S_1
CD 21         INT 21H
85 C9         TEST CX, CX
74 09         JE BAWAH   
          .LOOP:
BA 15 01      MOV DX, OFFSET RANTAI_LOOP
CD 21         INT 21H
E3 02         JCXZ BAWAH
E2 F7         LOOP .LOOP           
          BAWAH:
BA 23 01      MOV DX, OFFSET RANTAI_S_2
CD 21         INT 21H           
          EXIT:
B8 00 4C      MOV AX, 4C00H
CD 21         INT 21H 

RANTAI_S_1      DB 020H, 020H, 05EH, 00AH, 00DH, 020H, 02FH, 020H, 05CH
                DB 00AH, 00DH, 07CH, 020H, 07CH, 020H, 07CH, 00AH, 00DH
                DB 024H 

RANTAI_LOOP     DB 020H, 05CH, 020H, 05CH, 00AH, 00DH, 07CH, 020H, 07CH
                DB 020H, 07CH, 00AH, 00DH, 024H 

RANTAI_S_2      DB 020H, 05CH, 020H, 02FH, 00AH, 00DH, 020H, 020H, 076H
                DB 024H 

Tested on DOSBox

enter image description here

enter image description here

enter image description here

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

CSASM v2.1.2.3, 130 bytes

By far the shortest answer I'll submit with this language. (Probably)
Hooray for builtins!

func main:
in ""
conv i32
pop $a
push "  ^\n / \\\n| | |\n"
push " \\ \\\n| | |\n"
push $a
push 1
sub
mul
add
push " \\ /\n  v"
add
print
ret
end

Commented and ungolfed:

func main:
    ; Get the input, convert it to an integer and store it in the accumulator
    in ""
    conv i32
    pop $a

    ; Push the first three rows of the chain
    push "  ^\n / \\\n| | |\n"

    ; Push the next two rows of the chain
    push " \\ \\\n| | |\n"

    ; Duplicate those two rows ($a - 1) times
    push $a
    push 1
    sub
    mul

    ; Append the duplicated rows to the first three rows
    add

    ; Push the final 2 rows
    push " \\ /\n  v"

    ; Append them
    add

    ; Print the result string on the stack
    print
    ret
end
\$\endgroup\$
1
\$\begingroup\$

Pyth, 28 bytes

jcjd++" ^ /"*Q"\|||\\""/ v"5

Test suite

Translation of Sherlock9's Jelly answer.

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

Canvas, 22 bytes

| | |¶\ \*k^¶/ \:↕└∔∔r

Try it here!


Explanation:

Code                       # Explanation               # Stack visualization
#############################################################################################
                           # Stack starts with input   # <input>
| | |¶\ \                  # Push string literal       # <input>, | | |¶\ \
         *                # Repeat vertically n times # | | |¶\ \...| | |¶\ \
           k              # Remove last line          # | | |¶\ \...| | |
             ^¶/ \         # Push string literal       # | | |¶\ \...| | |, ^¶/ \
                  :       # Duplicate TOS             # | | |¶\ \...| | |, ^¶/ \, ^¶/ \
                    ↕      # Mirror vertically         # | | |¶\ \...| | |, ^¶/ \, \ /¶v
                     └     # Swap 2nd and 3rd in stack # ^¶/ \, | | |¶\ \...| | |, \ /¶v
                      ∔    # Add vertically            # ^¶/ \, | | |¶\ \...| | |¶\ /¶v
                       ∔   # Add vertically            # ^¶/ \¶| | |¶\ \...| | |¶\ /¶v
                        r # Center each line          #   ^¶ / \¶| | |¶ \ \...| | |¶ \ /¶  v
                           # Print TOS                 #

represents a newline in the stack visualization.

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

05AB1E, 22 bytes

…\||û×" ^ /ÿ/ v"Sðý5ô»

Try it online!

…\||û×" ^ /ÿ/ v"Sðý5ô»  # full program
                     »  # split...
      " ^ /ÿ/ v"        # literal...
                        # (implicit) with ÿ replaced by...
…\||                    # literal...
    û                   # concatenated with...
…\||                    # literal...
    û                   # reversed excluding the first character...
     ×                  # repeated...
                        # implicit input...
     ×                  # times...
                  ý     # with each...
                S       # character...
                  ý     # joined by...
                 ð      # spaces...
                     »  # into rows...
                    ô   # of length...
                   5    # literal
                        # implicit output
\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 71 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\$
1
\$\begingroup\$

GolfScript, 46 bytes

~['
| | |
']*' \\'2**' \\ /
  v'+
'  ^
 / \\'\

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.