last day (80 days later) » 

SJT
SJT
10:24 AM
Jesús López showed me a simple puzzle that begs for multiple solutions. He calls t Pyramid. Think of a contour map for a pyramid.

q)pyramid 1
1
q)pyramid 2
1 1 1
1 2 1
1 1 1
q)pyramid 3
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

Solutions?
10:53 AM
@SJT Edit the room info to include protocol ("https://") for "q201.org" as then it will become a proper clickable link (the protocol won't be shown in the text).
SJT
SJT
11:13 AM
room topic changed to q201: Discussion of topics in studying kdb/q in q201.org [kdb,] [q]
room topic changed to q201: Discussion of topics in studying kdb/q in q201.org [kdb] [q]
@Adám Thanks – great tip.
Hello! I'm glad you liked it! However, credit goes to the fantastic APL Problem Solving Competition: dyalog.com/uploads/files/student_competition/… We showed a original solution for it in this talk some time ago: youtu.be/J8QpxM36Ryw?si=mwFK00ayCgzFi2_Z&t=2002
11:33 AM
@SJT the q tag isn't used on stackoverflow for kdb questions - should remove it from the room info
SJT
SJT
@neutropolis Ah, that’s nice! But the `cross` is a detour:

pyr:{x &/:\:x}{x,1_reverse x}1+til ::
room topic changed to q201: Discussion of topics in studying kdb/q in q201.org [kdb]
@cillianreilly Tx! Grateful for tips.
One can think of the pyramid as a sum on the 3rd axis. (This is specific to x=3; haven’t devised a general form.)

q)5 5#sum 25#'(1b;raze(00000b;01110b)0 1 1 1 0;01b where 12 1)
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
If you bake bread you can think of rolling dough in flour.

embrace:{x,'y,'x}
roll:{embrace[n]flip embrace[n:x[0;0]-1] x}
pyramid:{$[x>1;(x-2)roll/x-3 3#11110b;1]}
APL of course has the operators (combinators) that make the lambdas unnecessary:

pyr←∘.⌊⍨ ,∘(1∘↓)∘⌽⍨∘⍳ 3
@Adám In The APL Orchard APL displays in the APL385 font. How’s that done?
12:14 PM
Similar to your first answer Stephen, but we can generate a single quadrant and reflect it to create the entire pyramid for some speedup:

pyr:{flip x,1_reverse x}/[2;]{x&/:x:1+til x}@
Further speed up by taking the flip outside of the iteration so it's only called once (as the pyramid is it's own transpose). Composing using :: instead of @ also gives some performance:

pyr:{x,1_reverse x}flip{x,1_reverse x}{x&/:x:1+til x}::
 
4 hours later…
4:51 PM
@neutropolis You may like apl.quest
@SJT Maybe make something like ^
@SJT It doesn't by default. I use the Stylus browser extension with the custom styling div.message pre,div.message code{font-family:"APL385 Unicode"}
But q doesn't need a special font. Just put backticks before and after inline code (like pyr:{flip x,1_reverse x}/[2;]{x&/:x:1+til x}@) and put code blocks in separate messages where you press Ctrl+k before Enter:
embrace:{x,'y,'x}
roll:{embrace[n]flip embrace[n:x[0;0]-1] x}
pyramid:{$[x>1;(x-2)roll/x-3 3#11110b;1]}

  last day (80 days later) »