Discussion
· Jan 20, 2023

Code Golf: Pyramid

New Year, new Code Golf!

You will receive a positive integer for the number of floors. Your challenge will build a pyramid with a "#" character. As usual, the shortest solution wins.

Input

3

Output

  #
 ###
#####

also a valid output

  #  
 ### 
#####

Note

Rules

  1. The signature of the contest entry MUST be:
Class codeGolf.Pyramid
{

ClassMethod Build(f As %Integer)
{
}

}
  1. It is forbidden to modify class/signature, including but not limited to:
  • Adding inheritance
  • Setting default argument values
  • Adding class elements (Parameters, Methods, Includes, etc).
  1. It is forbidden to refer to non-system code from your entry. For example, this is not a valid entry:
ClassMethod Build(f As %Integer)
{
  W ##class(myPackage.myClass).test(a)
}
  1. The use of $ZWPACK and $ZWBPACK is also discouraged.
Discussion (23)1
Log in or sign up to continue
Class codeGolf.Pyramid
{

ClassMethod BuildPython(f As %Integer) [ Language = python ]
{
for i in range(f):
    print(' ' * (f - i - 1) + '#' * (2 * i + 1))
}

/// Description: Build a pyramid of height f
ClassMethod Build(f As %Integer) As %Status
{
    Set sc = $$$OK
    For i = 1:1:f {
        set space = $tr($j("",f-i)," "," ")
        set hash = $tr($j("",2*i-1)," ","#")
        Write space_hash, !
    }
    Return sc
}

}

I use the suggested common method for length calculation

/// write ##class(Golf.Task2).length()
ClassMethod length(
	class = {$classname()},
	method = "Build") As %Integer
{
  #dim methodObj As %Dictionary.MethodDefinition
  set methodObj = ##class(%Dictionary.MethodDefinition).IDKEYOpen(class, method)
  quit methodObj.Implementation.Size
}

also in terminal:
USER>p
         s (b,h)="*" f L=f:-1:1  w ?L,b,! s b=b_h_h
         F i=1:1:f K s,c S ($P(s," ",f-i+1),$P(c,"#",i*2))="" W s,c,!
 
 
 

I'm looking at the output validation code, not Output, where there are no spaces at all on the right. It is a pity that there is confusion because of this!

 
Class codeGolf.test.Pyramid

I'm sorry to say, but this kind of information is an essential part of the task, and as such should be written before the recurring "As usual, the shortest ..."

Also, it would be nice, if the output example would show this trivia (the invisible spaces)!

I just saw "input = 3" and the output showed no trailing blanks!

And I saw the Notice with two links
- how to compute the size
- test examples
I wasn't interested in opening them, first, I know how to compute code size and second, I make the examples myself.
Hiding information in a side notice is a questionable practice.

ClassMethod Pyramide(n)
{
	s a="#" f i=1:1:n w ?n-i,a,?2*n-1,! s a=a_"##"
}

For a test use

w $c(27)_"[7m" d ##class(your.class).Pyramide(5) w $c(27)_"[0m"

 and you should get