Very Simple Triangles
Pyth, 44 42
ItQpdd*\_*4/Q2)jbms<*dQhQ,c" /\ "2,\/"__\\
The first line:
ItQpdd*\_*4/Q2)
ItQ ) If the input is not 1
pdd Print two spaces
*\_*4/Q2 Then groups of 4 underscores, repeated input/2 times.
The other two lines are generated by noticing that the second line consists of " /"
and "\ "
alternating input + 1 times, and the third line consists of "/"
and "__\"
alternated in the same fashion.
SQL, 182 175 173 187 bytes
Not that this'll ever be the shortest, but it's still amusing to try to minimize sql ;) lol I did this in Oracle 11, however, these should be basic SQL. [edit] as pointed out, I didn't apply the when input = 1 rule - only show 2 lines. can't think of a better way to do it, however, I did save a couple bytes by modifying the v logic ;) adding 2 ahead of time saves a couple bytes by not having to repeat it later [/edit]
select decode(&i,1,'',rpad(' ',v,'____')||z)||rpad(' /',v,'\ /')||decode(y,1,'\')||z||rpad('/',v-1,'__\/')||decode(y,1,'__\')from(select 2+floor(&i/2)*4v,mod(&i,2)y,chr(10)z from dual);
[edit1] removed some unnecessary spaces[/edit1] [edit2] changed &&i to just &i. It cuts down 2 chars, but forces user to input the # of triangles twice ... :P I realized my "good coding habits" using &&i were costing be 2 bytes!! The horror!! [/edit2]
Explanation (note: I use &&1 in this explanation so it only prompts once, the &1 above saves code space, but prompts multiple times ;) )
select -- line 1
decode(&&1,1,'', -- don't need line 1 if input is 1
rpad(' ',v,'____') || z ) || -- every pair of triangles
-- line 2
rpad(' /',v,'\ /') || -- every pair of triangles
decode(y,1,'\') || z || -- add the final triangle, input: 1,3,5 etc.
-- line 3
rpad('/',v-1,'__\/') || -- every pair of triangles
decode(y,1,'__\') -- add the final triangle, input: 1,3,5 etc.
from (select 2+floor(&&i/2)*4 v, -- common multiplier. 4 extra chars for every triangle pair
mod(&&i,2) y, -- Flag for the final triangle (odd inputs, 1,3,5, etc)
chr(10) z -- CR, here to save space.
from dual);
Output
SQL> accept i
1
SQL> /
/\
/__\
SQL> accept i
2
SQL> /
____
/\ /
/__\/
SQL> accept i
3
SQL> /
____
/\ /\
/__\/__\
SQL> accept i
12
SQL> /
________________________
/\ /\ /\ /\ /\ /\ /
/__\/__\/__\/__\/__\/__\/
SQL>
Python 2, 89 88 87 85 83 named / 81 unnamed
f=lambda n:1%n*(" "+n/2*4*"_"+"\n")+(" /\ "*n)[:2+2*n]+"\n"+("/__\\"*n)[:n-~n+n%2]
(Thanks to @orlp for a byte, and @xnor for another three)
This is a function which takes in an int n
and returns the triangles as a string using the row-by-row approach.
e.g. print f(10)
gives
____________________
/\ /\ /\ /\ /\ /
/__\/__\/__\/__\/__\/
For the first row, instead of (n>1)*
we use 1%n*
, since 1%n
is 0 if n == 1
and 1 if n > 1
.