« first day (2424 days earlier)      last day (223 days later) » 

3:00 PM
Welcome to APL Quest 2021-5! Today's quest is Rect-ify:
> Write a function that:
> • has a right argument N which is a positive integer less than or equal to 1,000,000.
> • returns a 2-element integer vector R representing the rows and columns of the rectangle such that:
>   ∘ N=×/R meaning N equals the number of rows × the number of columns (you planted all the trees!)
>   ∘ ≤/R meaning the number of rows is less than or equal to the number of columns
>   ∘ |-/R is minimal, meaning the difference between the elements of R is as small as possible
 
{⍵=0:2⍴0 ⋄  (⊢,⍵÷⊢){(⌈2÷⍨≢⍵)⌷⍵}⍸0=⍵∘.|⍨⍳⍵}
but I do not like the last part
(⊢,⍵÷⊢)
 
{⎕DIV←1 ⋄ n,⍵÷n←⊃⌽⍸0=⍵|⍨⍳⌊⍵*0.5}
 
@Richard That's a hook…
 
n,⍵÷n←
tried to avoid this, but the result might be the same
@Adám ?
 
That's the pattern of a fork where one side is an identity.
Isn't it just ⍵(⊢,÷)?
 
3:03 PM
yes!
 
Ooh, I have an idea.
 
@rabbitgrowth yours is less computations
 
Right, it seems to work.
Simply create a multiplication table, and find the most top-left location of the argument.
 
@rabbitgrowth :) just looked up what ⎕DIV←1 does
 
Anyone up for my approach before I reveal it?
And now I have another approach: Use factors.
 
3:12 PM
thinking about it
 
Using factors, my solution is only 18 chars.
 
{⊃⍸⍵=∘.×⍨⍸0=⍵|⍨⍳⌈⍵*0.5}
probably not exactly you were looking for
 
I have {⍵(⊢,÷)⌈/⍵∨⍳⌊⍵*÷2}
Oh it fails on 0.
But {0⌈⍵(⊢,÷)⌈/⍵∨⍳⌊⍵*÷2} fixes it.
20 then.
 
nice
 
Then this outer producty one is 1 char shorter: {⊃⌽⍸⍵=(∘.××∘.≤)⍨⍳⍵}
 
3:20 PM
@Adám I had, uhhh, ⊢(⊢,÷)((⊢⌷⍨∘⌊2÷⍨≢)(((⌊=⊢)⊢÷⍳)⊢⍤/⍳))
 
uhhh, right.
 
I think it finds all the factors and then picks the middle one (left-biased) and then returns that paired with the number divided by it
made it last week, can't remember exactly, but that is what it looks like it does
nevermind it doesn't even work
 
Right, that was my idea too, but there's a neat way to find the factors.
 
⊢(⊢,÷)((⊢⌷⍨∘⌈2÷⍨≢)(((⌊=⊢)⊢÷⍳)⊢⍤/⍳)) does, though
 
that's what I did also. Where does yours go wrong, beside with 0?
 
3:25 PM
I think we can do even better, we simply need the middle 1 or 2 elements from ∪⊢∨⍳
{⍵[(⌊,⌈)2÷⍨1+≢⍵]}(∪⊢∨⍳) works on all but 0.
 
(⊢⌷⍨∘⊂∘(⌊,⌈)2÷⍨1+≢)(∪⊢∨⍳)
@Adám oh it's the same as this
 
is ∪⊢∨⍳ faster then {⍸0=⍵|⍨⍳⍵}?
 
Probably not.
 
it is shorter though
 
      ]runtime -c (∪⊢∨⍳)123456 (⍸0=⍳|⊢)123456

  (∪⊢∨⍳)123456   → 5.2E¯3 |   0% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
  (⍸0=⍳|⊢)123456 → 5.6E¯4 | -90% ⎕⎕⎕⎕
Oh well, I'm out of ideas. Anyone, or shall we finish up?
 
3:35 PM
bruteforce :)
 
Isn't that what I did with the outer products?
 
which one again?
 
{⊃⌽⍸⍵=(∘.××∘.≤)⍨⍳⍵}
 
I do not have any other suggestions.
 
{⊃pairs[⊃⍋(|-/)¨pairs←((⍵=×/)¨pairs)/(pairs←,⍳⍵ ⍵)]}
this is what I mean by bruteforce
 
3:42 PM
Yay: (2↑2∘/↓⍨¯1+≢)⊢∪⍤∨⍳
So we're back at 18
OK, that's all folks. See you next week for 2021-6: Fischer Random Chess.
 
thanks!
 
 
3 hours later…
7:11 PM
0
Q: Is Execute unsafe in namespece paths?

mappoI'm handling a json string converted into a namespace. Here's a toy example to illustrate my concern: What if a malicious client POSTed this payload? js←'{"foo": "bar", "⎕OFF": "bye"}' ⍝ this is what is POSTed by a client ns←(⎕JSON ⍠'D')js ⍝ when the payload reaches my code it...

 
7:40 PM
@Adám early entry: (≠/2|∘⍸'B'∘=)∧('RKR'≡∊∘'RK'⊢⍤/⊢)
spent exactly zero minutes trying to optimize it but it's not really a challenge I like
 

« first day (2424 days earlier)      last day (223 days later) »