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
- Use this code to check the solution length
- You also can use this test case here
Rules
- The signature of the contest entry MUST be:
Class codeGolf.Pyramid
{
ClassMethod Build(f As %Integer)
{
}
}
- 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).
- 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)
}
- The use of $ZWPACK and $ZWBPACK is also discouraged.
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 } }
{
ClassMethod Build(f As %Integer)
F i=1:1:f K s,c S ($P(s," ",f-i+1),$P(c,"#",i*2))="" W s,c,!
{
}
}
65 ???
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:
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,!
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...
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.
Hmm!
.png)
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!
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 ?
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 suggest waiting for an answer from @Eduard Lebedyuk, the author of the discussion, who knows how really should be.
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.
it still exists:
ClassMethod Build(f As %Integer) { s a="#" f i=F:-1:1 w ?i,a,! s a=a_"##" }
Perhaps it's the inattentiveness of the author. This is not the first time, we got an incomplete described task to solve like punctuations etc. in https://community.intersystems.com/post/code-golf-word-order
You are absolutely right.
There should be no discrepancies in the description of the task and in the code for checking the solution.
Some solutions are just too creative!
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: