Eduard Lebedyuk · Jan 20

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.





also a valid output




  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.
1 258
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


Class codeGolf.Pyramid

ClassMethod Build(As %Integer)
   i=1:1:s,($P(s," ",f-i+1),$P(c,"#",i*2))="" s,c,!


How exactly are you counting, Robert? to me it looks shorter than the previous one

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:
         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,!

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

Maybe there is a shorter solution, I don't know...

size = 50

PS: if you look at the class codeGolf.test.Pyramid, you can see that the format of the output data for f=3 should be as follows:

"  #  "
" ### "

So be careful.

Where did you see the enclosing quotes? 
they are invisible in my browser.

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  

Perhaps the author wanted to test us for attentiveness ? wink

Just forget my contribution.
I'm not interested in digging for secrets!
I leave that to Indiana Jones    
my code is deleted ! it's not my type of fun

I hope that Eduard will be formulate the conditions of the task more carefully in the future.

PS: Robert, you shouldn't have rushed to remove your solution.

In any case, your solution was the best. yes

it still exists:

ClassMethod Build(f As %Integer)
 s a="#" f i=F:-1:1 w ?i,a,! s a=a_"##"

You are absolutely right.
There should be no discrepancies in the description of the task and in the code for checking the solution.

Somehow every solution is creative.

Only the pyramid characters count, having (or not having) white spaces at the end is not important.

Test cases do have them, but if you shorten the solution and it outputs the pyramid without white spaces after the hashtag that's great too.

Maybe the next time we get a better description which includes all details (unhidden, if possible).
Bye the way, what's the very difference between the two example pyramides ("Output" and "also a valid output")? I don't see any visible difference - but who knows, maybe I need new glasses...

If you select the text with the mouse, you will see spaces on the right in the second "Output".
In general, I think that such details should be enclosed in quotation marks.

Ok. If the nuances of formatting spaces and line feed are unimportant, then can yet shorten Robert's code:

size = 40