Written by

Senior Cloud Architect at InterSystems
Discussion Eduard Lebedyuk · 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.

Comments

Guillaume Rongier · Jan 20, 2023
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 fClassMethod Build(f As%Integer) As%Status
{
    Set sc = $$$OKFor i = 1:1:f {
        set space = $tr($j("",f-i)," "," ")
        set hash = $tr($j("",2*i-1)," ","#")
        Write space_hash, !
    }
    Return sc
}

}
0
Yaron Munz · Jan 20, 2023

Class codeGolf.Pyramid
{

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

}

0
Yaron Munz  Jan 20, 2023 to Robert Cemper

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

0
Robert Cemper  Jan 20, 2023 to Yaron Munz

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.MethodDefinitionset 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,!
 
 
 

0
Julius Kavay · Jan 20, 2023
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...

0
Vitaliy Serdtsev · Jan 23, 2023
 

size = 50

ClassMethod Build(As %Integer)
{
a="#" i=f:-1:1 ?i-1,a,?f*2-1,! a=a_"##"
}

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.

0
Robert Cemper  Jan 23, 2023 to Vitaliy Serdtsev

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

0
Vitaliy Serdtsev  Jan 23, 2023 to Robert Cemper

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

0
Julius Kavay  Jan 23, 2023 to Vitaliy Serdtsev

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  

0
Vitaliy Serdtsev  Jan 23, 2023 to Julius Kavay

Perhaps the author wanted to test us for attentiveness ? wink

0
Robert Cemper  Jan 23, 2023 to Vitaliy Serdtsev

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

0
Vitaliy Serdtsev  Jan 23, 2023 to Robert Cemper

I suggest waiting for an answer from @Eduard Lebedyuk, the author of the discussion, who knows how really should be.

0
Vitaliy Serdtsev  Jan 23, 2023 to Robert Cemper

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

0
Robert Cemper  Jan 23, 2023 to Vitaliy Serdtsev

it still exists:

ClassMethod Build(f As%Integer)
{
 s a="#" f i=F:-1:1w ?i,a,! s a=a_"##"
}
0
Vitaliy Serdtsev  Jan 23, 2023 to Julius Kavay

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

0
Eduard Lebedyuk  Jan 23, 2023 to Julius Kavay

Some solutions are just too creative!

0
Julius Kavay  Jan 23, 2023 to Eduard Lebedyuk

Somehow every solution is creative.

0
Eduard Lebedyuk  Jan 23, 2023 to Julius Kavay

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.

0
Julius Kavay  Jan 23, 2023 to Eduard Lebedyuk

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

0
Vitaliy Serdtsev  Jan 24, 2023 to Julius Kavay

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.

0
Vitaliy Serdtsev  Jan 24, 2023 to Eduard Lebedyuk

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

 

size = 40

ClassMethod Build(As %Integer)
{
 a="#" i=f:-1:1 !?i,a=a_"##"
}
0