how to use code to create a Pyramid

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
fugitive
Posts: 19
Joined: 09 Mar 2017 02:26

how to use code to create a Pyramid

#1 Post by fugitive » 16 Mar 2017 10:24

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
Attachments
QQ图片20170317001918.png
QQ图片20170317001918.png (5.46 KiB) Viewed 12308 times
Last edited by fugitive on 16 Mar 2017 21:18, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: how to use code to create a Pyramid

#2 Post by Squashman » 16 Mar 2017 10:54

fugitive wrote:(With batch processing, whose code will be the shortest )

Maybe yours will be the shortest if you actually attempted to code it.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to use code to create a Pyramid

#3 Post by Aacini » 16 Mar 2017 11:00

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

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: how to use code to create a Pyramid

#4 Post by Compo » 16 Mar 2017 11:40

I remember once coming accross this one, which I suppose is an extension of that!
Image

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to use code to create a Pyramid

#5 Post by Aacini » 16 Mar 2017 12:29

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

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: how to use code to create a Pyramid

#6 Post by misol101 » 16 Mar 2017 12:52

@cmdgfx "poly 7 0 A 20,5,3,22,37,22"

...sorry about that, I'll show myself out :mrgreen:


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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to use code to create a Pyramid

#7 Post by dbenham » 16 Mar 2017 14:51

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to use code to create a Pyramid

#8 Post by Aacini » 16 Mar 2017 14:58

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"

8)

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to use code to create a Pyramid

#9 Post by Aacini » 16 Mar 2017 15:04

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

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: how to use code to create a Pyramid

#10 Post by Thor » 16 Mar 2017 15:14

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!"
)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to use code to create a Pyramid

#11 Post by dbenham » 16 Mar 2017 15:27

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to use code to create a Pyramid

#12 Post by Aacini » 16 Mar 2017 15:35

This works, but I have no idea why! :shock:

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to use code to create a Pyramid

#13 Post by dbenham » 16 Mar 2017 15:49

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to use code to create a Pyramid

#14 Post by dbenham » 16 Mar 2017 15:59

Aacini wrote:This works, but I have no idea why! :shock:

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" :wink: %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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to use code to create a Pyramid

#15 Post by Aacini » 16 Mar 2017 16:14

@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

Post Reply