Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Write a program or function that takes in a positive integer and prints or returns a stack of that many ASCII-art turtles, where each turtle is larger than the one above it.

Specifically, if the input is 1, the output should be:

 __
/,,\o

If the input is 2:

  __
o/,,\
 ____
/,__,\o

If the input is 3:

   __
  /,,\o
  ____
o/,__,\
 ______
/,____,\o

If the input is 4:

    __
  o/,,\
   ____
  /,__,\o
  ______
o/,____,\
 ________
/,______,\o

If the input is 5:

     __
    /,,\o
    ____
  o/,__,\
   ______
  /,____,\o
  ________
o/,______,\
 __________
/,________,\o

And so on in the same pattern for larger inputs.

Note that:

  • The head (the o) of the bottom turtle is always on the right. The heads of the turtles above then alternate back and forth.
  • No lines may have trailing spaces.
  • Superfluous leading spaces are not allowed. (i.e. the back of the bottom turtle should be at the start of the line.)
  • A single optional trailing newline is allowed.

The shortest code in bytes wins.

share|improve this question
6  
trichoplax, I'm expecting an answer that uses recursion. – El'endia Starman 7 hours ago
4  
,________, When someone says something that makes no sense. – R. Kap 6 hours ago

Batch, 256 bytes

@set i=echo 
@%i%off
set u=
for /l %%j in (2,2,%1)do call set i=%%i%%  
set/af=%1^&1
if %f%==1 %i% __&%i%/,,\o&set u=__
for /l %%j in (2,2,%1)do call:l
exit/b
:l
set i=%i:~0,-2%
%i%  _%u%_
%i%o/,%u%,\
%i% __%u%__
%i%/,_%u%_,\o
set u=__%u%__

Note that line 1 has a trailing space and line 4 has two trailing spaces. i therefore contains an echo command with the appropriate amount of indentation for each turtle. Meanwhile u contains the number of underscores in alternate turtles. A leading odd turtle is special-cased and then the rest of the turtles are output in pairs.

share|improve this answer
1  
+1 for being exactly 256 bytes. Do not golf it unless u can exactly half its length! – Rohan Jhunjhunwala 5 hours ago

Cheddar, 105 bytes

n->(|>n).map(i->(1-i%2)*"o"+"\\,"+(n-i-1)*"__"+",/"+i%2*"o"+i/2*"  "+"\n"+(n-i)*"__"+(i+1)*" ").vfuse.rev
share|improve this answer
    
+1 for using cheese. You can use literal newline to save bytes – Downgoat 3 hours ago

Ruby, 100 bytes

Recursive solution. Try it online!

f=->n,i=1{f[n-1,i+1]if n>1;puts' '*i+?_*n*2,"%#{i-1}s/,#{?_*2*~-n},\\"%(i<2?'':'o '[i%2])+' o'[i%2]}
share|improve this answer

V, 57 53 bytes

bdwi __
/,,\o@"ñHyjç,/f,2á_k2A_
ëPhjI ñLòkk$x^PXkk

Since this contains unprintable characters, here is a hexdump:

00000000: 1862 6477 6920 5f5f 0a2f 2c2c 5c6f 1b40  .bdwi __./,,\o.@
00000010: 22f1 4879 6ae7 2c2f 662c 32e1 5f6b 3241  ".Hyj.,/f,2._k2A
00000020: 5f0a eb50 1668 6a49 20f1 4cf2 6b6b 2478  _..P.hjI .L.kk$x
00000030: 5e50 586b 6b                             ^PXkk

Try it online!

This should be 56 bytes, since there is H is supposed to be mapped to gg, which worked in previous versions, but magically disappeared from this version. :/

Explanation:

<C-x>                       "Decrement the input
     bdw                    "Move back and delete the input into register '"'
        i __\n/,,\o<esc>    "Insert the first turtle

@"ñ                         "Input - 1 times:
   H                        "  Move to the beginning of the file
    yj                      "  Copy two lines
      ç,/                   "  On every line containing a comma: 
         f,                 "    Move the cursor to that comma
           2á_              "    Append two underscores after the comma
              k             "    Move up a line
               2A_          "    And append 2 underscores after this line

ë               "  Move to the beginning of the file
 P              "  Paste the two lines we previously copied
  <C_v>hjI      "  And indent these two lines with a space
           ñ    "End loop

L               "Move to the end of the file
 ò              "  Recursively:
  kk            "  Move up two lines
    $x          "  Delete the last character on this line (an 'o')
      ^P        "  And paste this 'o' at the beginning of the line
        X       "  Remove one space
         kk     "  Move up two lines again
share|improve this answer
    
Interesting outputs for inputs 0 and below. – R. Kap 4 hours ago
    
This code also doesn't work for input > 10. On a side note, I accidentally broke it completely with the input 0 41c14. Not sure if I broke the code, or the runner. – Brandon Anzaldi 4 hours ago
1  
@R.Kap Yeah, I think I know why it does that. V is barely able to comprehend integers, so it just sees -1 as a string that it can't pretend is a number. Thankfully, I don't have to handle those. – Dr Green Eggs and Iron Man 4 hours ago
1  
@BrandonAnzaldi Ah, I see why that doesn't work. I'll fix that in a minute. Also, doing anything other than decimal number is bound to cause some weird problems. – Dr Green Eggs and Iron Man 4 hours ago
    
Yup! Cool solution. Figured it'd probably be somewhat simple to fix. I was just very, very fascinated with the output of the aforementioned accidental keyboard mash. Leading spaces also produce some fun output. Seems like you wrangled V quite nicely! – Brandon Anzaldi 4 hours ago

C, 328 bytes:

main(m,e,O,y,p,F,g,z,s){scanf("%d",&m);e=m;O=2*e;for(y=2;y<=2*m;y+=2){p=O%4==0;char x[y-2],d[e],l[e-1-p],u[y];x[y-2]=d[e]=l[e-1-p]=u[y]='\0';for(F=0;F<e-1-p;++F)l[F]=' ';for(g=0;g<y;++g)u[g]='_';for(z=0;z<(y-2);++z)x[z]='_';for(s=0;s<e;++s)d[s]=' ';printf("%s%s\n%s%s/,%s,\\%s\n",d,u,(O>4)? l:"",p ? "o":"",x,p ? "":"o");e--;O-=2;}}

A simple implementation using a lot of string formatting and for loops. However, another, hopefully shorter, recursive solution is coming soon. I will still try and golf this current one as much as I can, though.

C It Online! (Ideone)

share|improve this answer

Python 2, 153 bytes

n=input()
s=' ';r=[];k=0;i=0
while i<n:a=i%2;b=1-a;r=[s*k+s*a+s+'_'*(n-i)*2+s,s*k+'o'*a+'/,'+'_'*(n*2-i*2-2)+',\\'+'o'*b]+r;k+=a*2;i+=1
print'\n'.join(r)

Try it online

share|improve this answer

Retina, 97 bytes

Byte count assumes ISO 8859-1 encoding.

.+
$*_
_(_*)
 /,$1$1,\o
+`^( *)(.)(/.+)__,.(.)
 $1$4$3,\$2¶$&
%`^( *)./,(_*)
$1  $2__¶$&
Rm`^ | $

Try it online!

share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.