Page 1 of 3
how to use code to create a Pyramid
Posted: 16 Mar 2017 10:24
by fugitive
As picture shown, shows a Pyramid of letter A
(With batch processing, whose code will be the shortest )
——————————————————————————
this is my code,too long,haha
Code: Select all
@echo off
set "s= AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
for /l %%i in (1,1,40) do (
call,echo,%%s:~%%i,%%i%%
)
pause
——————————————————————————
You guys are really great.
The shortest is about 70 bytes.
I learned a lot from you, thanks!
2017.3.17
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 10:54
by Squashman
fugitive wrote:(With batch processing, whose code will be the shortest )
Maybe yours will be the shortest if you actually attempted to code it.
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 11:00
by Aacini
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "rows=17"
set "spaces="
for /L %%i in (1,1,%rows%) do set "spaces=!spaces! "
set "letters=A"
for /L %%i in (1,1,%rows%) do (
echo !spaces!!letters!
set "spaces=!spaces:~1!"
set "letters=!letters!AA!
)
Antonio
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 11:40
by Compo
I remember once coming accross this one, which I suppose is an extension of that!
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 12:29
by Aacini
This remembers me a program I wrote many years ago. I called it:
Pascal's Tree:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "rows=20"
set /A sp=rows*25/10, ad0=0
set "spaces="
for /L %%i in (1,1,%sp%) do set "spaces=!spaces! "
echo %spaces% 1
for /L %%i in (2,1,%rows%) do (
set /A "prev=1, n=%%i, line=1, adj=0"
for /L %%j in (1,1,%%i) do (
set /A "j=prev*n/%%j, prev=j, n-=1"
set "line=!line! !j!"
for /L %%d in (1,1,6) do set /A "j/=10, adj+=^!^!j"
)
set /A "mrg=(adj-ad0)/2+1, ad0=adj"
for /F %%j in ("!mrg!") do set "spaces=!spaces:~%%j!"
echo !spaces!!line!
)
Output:
Code: Select all
1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1
1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1
1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1
1 20 190 1140 4845 15504 38760 77520 125970 167960 184756 167960 125970 77520 38760 15504 4845 1140 190 20 1
Antonio
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 12:52
by misol101
@cmdgfx "poly 7 0 A 20,5,3,22,37,22"
...sorry about that, I'll show myself out
Here's an honest attempt (replace # with a backspace character):
Code: Select all
@echo off
set /a letters=1,spaces=17
:REP
for /l %%a in (1,1,%spaces%) do <nul set /p=".# "
for /l %%a in (1,1,%letters%) do <nul set /p=A
echo(
set /a letters+=2,spaces-=1
if %spaces% gtr 0 goto REP
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 14:51
by dbenham
This is pretty short:
Code: Select all
@set s= A
:l
@echo(%s%&if "%s:~0,1%"==" " set s=%s:~1%%s:~-1%%s:~-1%&goto :l
Dave Benham
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 14:58
by Aacini
Code: Select all
@echo off
if not defined spaces set "letters=A" & set "spaces= "
echo %spaces%%letters%
set "letters=%letters%AA" & set "spaces=%spaces:~1%
if defined spaces "%~F0"
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 15:04
by Aacini
Borrowed from Dave's code:
Code: Select all
@set "s= AA"
:l
@echo(%s:~,-1%&if "%s:~,1%"==" " set s=%s:~1%%s:~-2%&goto :l
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 15:14
by Thor
Based on Aacini's triangle method, I've come up with these 2 batch files:
"two_triangle.bat"
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "rows=17"
set "spaces="
set "letters=A"
for /L %%i in (1,1,%rows%) do set "spaces=!spaces! "
for /L %%i in (1,1,%rows%) do (
echo !spaces!!letters!!spaces!!spaces!!letters!
set "spaces=!spaces:~1!"
set "letters=!letters!AA"
)
"two_diamond.bat"
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "rows=17"
set "spaces="
set "letters=A"
for /L %%i in (1,1,%rows%) do set "spaces=!spaces! "
for /L %%i in (1,1,%rows%) do (
echo !spaces!!letters!!spaces!!spaces!!letters!
set "spaces=!spaces:~1!"
set "letters=!letters!AA"
)
set "rows=16"
set "spaces= "
set "letters=!letters:~4!"
for /L %%i in (1,1,%rows%) do (
echo !spaces!!letters!!spaces!!spaces!!letters!
set "spaces=!spaces! "
set "letters=!letters:~2!"
)
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 15:27
by dbenham
And here is a one liner that works from the command line as long as variable c is undefined:
Code: Select all
cmd/von/c"set l=A&set c=for/l %N in (1 1 17) do @&(%c%set l= !l!)&%c%echo !l!&set l=!l:~1!AA"
Dave Benham
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 15:35
by Aacini
This works, but I have no idea why!
Code: Select all
@set s= &set a=A
:l
@echo %s%%a%&set a=%a%AA&set s=%s:~1%&goto :l
I don't understand why the GOTO is break when the S string is empty... Windows 8.1 here.
Antonio
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 15:49
by dbenham
Oh hell. If I am going to use string literal constants, then I should be consistent.
79 byte batch file (if \n used instead of \r\n, and no \n on last line):
Code: Select all
@set s= A
:l
@echo(%s%&if "%s:~0,1%"==" " set s=%s:~1%AA&goto:l
85 byte command line one liner, assuming c is not defined:
Code: Select all
cmd/von/c"set l= A&for /l %N in (1 1 17) do @echo !l!&set l=!l:~1!AA"
Dave Benham
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 15:59
by dbenham
Aacini wrote:This works, but I have no idea why!
Code: Select all
@set s= &set a=A
:l
@echo %s%%a%&set a=%a%AA&set s=%s:~1%&goto :l
I don't understand why the GOTO is break when the S string is empty... Windows 8.1 here.
Both s and & are undefined,
so "obviously"
%s: expands to nothing, and
%&goto : expands to nothing, and you are left with:
Code: Select all
echo AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&set a=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&set s=~1l
But my most recent batch script beats yours by 1 byte, even after goto :l is shortened to goto:l
Dave Benham
Re: how to use code to create a Pyramid
Posted: 16 Mar 2017 16:14
by Aacini
@Dave, this is your "79 byte" batch file, but 4 bytes shorter:
Code: Select all
@set s= A
:l
@echo(%s%&if %s:~,1%A==A set s=%s:~1%AA&goto:l
Antonio