Many formulas in math involve nested radicals (square root signs inside other square roots signs). Your task here is to draw these using ascii art.
Specs
You will be given two integers, the number inside the radical, and the number of radicals. I think the best way to explain what you have to do is with an example. Here is the output for 2, 4:
____________________
\ / _______________
\ / \ / __________
\ / \ / \ / _____
\/ \/ \/ \/ 2
Here are some things to note:
- The height of each radical increases by one
- The length of the
_'s are always5times the height - There is a space after the last
/and before the next inner radical starts - The number inside will never be greater than 4 digits
- If the number of radicals is 0, just output the number
- Putting extra spaces to pad it into a rectangle is up to you
- This is code-golf, so shortest code in bytes wins!
Test Cases
2, 4:
____________________
\ / _______________
\ / \ / __________
\ / \ / \ / _____
\/ \/ \/ \/ 2
23, 0:
23
4, 1:
_____
\/ 4
1234, 3:
_______________
\ / __________
\ / \ / _____
\/ \/ \/ 1234
-
12\$\begingroup\$ I feel like this would be a slightly better challenge if the horizontal bars all had to end at the same point. \$\endgroup\$Greg Martin– Greg Martin2016年12月20日 00:13:44 +00:00Commented Dec 20, 2016 at 0:13
4 Answers 4
Python 3.5, (削除) 145 (削除ここまで) 137 bytes
def s(n,x):[([print(' '*j+'\\'+' '*i+'/ '+' '*j,end='')for j in range(x-i-1,-1,-1)],print(' '*i+i*'_____'or n))for i in range(x,-1,-1)]
Slightly ungolfed:
def s(n,x):
for i in range(x,-1,-1):
for j in range(x-i-1,-1,-1):
print(' '*j+'\\'+' '*i+'/ '+' '*j,end='')
print(' '*i+i*'_____' or n)
Output:
s(2,4)
____________________
\ / _______________
\ / \ / __________
\ / \ / \ / _____
\/ \/ \/ \/ 2
-
\$\begingroup\$
print(' '*i+'_____'*i or n)saves 7 bytes. EDITprint(' '*i+i*'_____'or n)saves 8. \$\endgroup\$Jonathan Allan– Jonathan Allan2016年12月20日 04:05:03 +00:00Commented Dec 20, 2016 at 4:05 -
\$\begingroup\$ Thank you. I did not know you could use 'or' like that. \$\endgroup\$James Hollis– James Hollis2016年12月20日 13:19:05 +00:00Commented Dec 20, 2016 at 13:19
JavaScript, (削除) 133 (削除ここまで) (削除) 132 (削除ここまで) 131 bytes
f=(n,r,q=r)=>~r?'1\0円/1 '[x='repeat'](d=q-r).replace(/\d/g,i=>' '[x](+i?d-=.5:r*2))+(r?' '[x](r*2)+'_'[x](5*r):n)+`
`+f(n,r-1,q):''
F=(n,r)=>console.log( f(n,r) )
F(2,4)
F(23,0)
F(4,1)
F(1234,3)
.as-console-wrapper{max-height:100%!important;top:0}
JavaScript (ES6), 124 bytes
f=(s,n,i=0,r=(n,c=` `)=>c.repeat(n))=>n?r(n+n)+r(n*5,`_`)+`
`+f(s,n-1).replace(/^/gm,_=>r(i)+`\\${r(n+~i<<1)}/`+r(++i)):s+``
<div oninput=o.textContent=f(s.value,+n.value)><input id=s><input id=n type=number min=0><pre id=o>
Save 3 bytes if the first parameter can be a string rather than a number.
PHP, 178 bytes
for($r=[" $argv[1]"];$i++<$argv[2];$r[]=$p("",2*$i).$p(_,5*$i,_))for($k=-1;++$k<$i;)$r[$k]=($p=str_pad)("\\".$p("",2*$k)."/",2*$i," ",2).$r[$k];echo join("\n",array_reverse($r));
bah that ́s awfully long.