Knit me an ASCII-Sock
Mathematica, 104 bytes
""<>{t=Table;c=t[{"v"~t~#,">"~t~#2,"
"},#3]&;h=#/2;c[#,0,3h],c[#-i,3i,z={i,h}],c[h,6h+2Min[i-1,h-i],z]}&
Unnamed function taking a positive even integer as input and returning a string (with a trailing newline). Note that the newline between the two lines of code above is part of the code. The main work is done by the function c
, whose ungolfed definition
c[#1, #2, #3] = Table[{Table["v", #1], Table[">", #2], "\n"}, #3]
creates a table of lists, each one consisting of several "v"
s in a row followed by several ">"
s followed by a newline. Fortunately, the range #3
for the outer Table
can have a variable name, which can be referred to in the expressions #1
and #2
; this allows the function c
to be called with both constant and variable arguments. The rest is just computation, and the string-joining operator ""<>
flattens the nested lists that arise for free.
Python, 3.5 183 177 bytes
n=int(input())
h=n//2
z=h%2
l=['v'*h+'>'*(2*n-h+3*h+i*2)for i in range(1*z+n//4)]
*map(print,['v'*n]*(3*h)+['v'*i+'>'*(3*(n-i))for i in range(h,n)][::-1]+[l,l[:-1]][z]+l[::-1]),
Explanation incoming.
Try it Online!
Pyth - 93 Bytes
J/Q2K"V"P*s*Q1.5+*QKbVJp*+Q_+1NK*+3*3N">";InQ2VJp*JK*+s*2aaNc-J 1 2c-J 1 2+3*Q3">";IqQ2pK*6">
Explanation:
J/Q2 sets J to half of input
K"V" sets K to "V"
P prevents newline after main block of sock
* multiply the next 2 items
s*Q1.5 rounds up input*1.5 (columns in main block)
+ adds the next 2 item (this item gets multiplied by the previous item)
*QK multiplies "V" by input (resulting in each row)
b New line
VJ For all the numbers 0-((Q/2)-1) (as represented by N)
p print
* multiply the next 2 items
+Q_+1N input + -1*(1+N)
K "V"
*+3*3N">" 3N+3 many ">"'s
; ends for statement
InQ2 if input is not 2
VJ For all the numbers 0-((input/2)-1) (as represented by N)
p print
*JK input/2 many ">"'s
*+s*2aaNc-J 1 2c-J 1 2+3*Q3">" int(abs(abs(N-((input/2)-1)/2)-((input/2)-1)/2)))+(input*3)+3 many ">"'s
; end of for statement
IqQ2 if input is 2
pK*6"> print "V" and 6 ">"'s
You can try it here!