« first day (2347 days earlier)      last day (300 days later) » 

3:00 PM
Welcome to APL Quest 2020-4! Today's quest is Take a Leap:
> Write a function that, given a right argument which is an integer array of year numbers greater than or equal to 1752 and less than 4000, returns a result of the same shape as the right argument where 1 indicates that the corresponding year is a leap year (0 otherwise).
 
{+⌿1 3 ∘.=+⌿~×4 100 400∘.|⍵}
 
Hm, I'm trying to understand the 1 3 part.
Ah, that's the parity, so +⌿1 3 ∘.= is the same as 2|
 
if 1 or all three conditions are true, it is a leap year
I could have done probably ≠ 2
 
Has to cover the 0 case too.
 
yes
 
3:03 PM
Anyway, now you have {2|+⌿~×4 100 400∘.|⍵}
But the parity 2|+⌿ can also be computed as the XOR: ≠⌿
 
Hmmmm I might have misunderstood the question then
Long time btw
 
Yes, welcome back!
 
Thanks
 
And (for non-negative numbers) is simply 0=
 
I thought checking if a year is a leap year was something as simple as {0=4|⍵} (or something close to that)
 
3:05 PM
No, every 100 years, there's an exception, and every 400 years, there's an exception to that.
It might be a good idea to make yet another exception every 4000 years, but that hasn't been officially adopted yet. Hence the task's upper limit of 3999.
 
Oh, I wasn't aware of that
 
So, now we have {≠⌿0=4 100 400∘.|⍵} — who can spot inner product?
@AndréLeria You're not alone. Microsoft Excel thinks 1900 was a leap year!
Btw, we can easily make this function tacit: ≠⌿0=4 100 400∘.|⊢
 
So, the exception is that multiples of 100 which aren't multiples of 400 ain't leap years, is that right?
I've been trying to learn about trains and tacit but I'm still a work in progress
 
Correct, so 1900 wasn't a leap year, but 2000 was a leap year.
 
{0≠.=4 100 400∘.|⍵}
 
3:11 PM
@AndréLeria What resources have you been using for learning?
@Richard Nice. And again, can trivially tacitable.
What if I told you this functionality is basically built-in?
 
Stefan Kruger's Learn APL for some time, and more recently I learned about the Mastering Dyalog APL book
 
(0≠.=4 100 400∘.|⊢)
 
Still, it's only been a few days since I came back to learning
I did study some BQN a few months ago though
 
You may find some gems at the Tutorials section here.
 
I still can't decide which I want to study more
 
3:15 PM
When we designed ⎕DT (date-time conversion) and had settled on left argument numeric codes for various date-time formats (positive for scalars, negative for vectors), I suggested 0 for validation, returning a Boolean result instead of converting.
 
@AndréLeria Don't know how active the community is for BQN, but this Quest helps me a lot every week
 
Furthermore, the vector formats allow omitting trailing elements, with each element having a default value (e.g. the default month is January and the default day is the 1st, with time-of-day elements being 0).
 
@Adám that was indeed helpful, and since I read it, I can mostly handle the simplest forks and atops. But when the binding operators start coming in, it's no longer obvious to me
 
How can we use the above to check if a year is a leap year?
 
@Richard I only ever see people talking about BQN at the APL Discord
 
3:18 PM
@AndréLeria Did you study the composition diagrams?
 
Nope
 
found also ]box ON -t=tree format useful
 
tacit.help might be useful too.
Is anyone working on a ⎕DT-based solution or can I give it away?
 
yes looking at it. But did not find it yet
 
3:22 PM
OK, I'll give you some time. Let me know if you want a hint.
 
I am trying to calculate the amount of days in that year
 
Oh, you could do that as well, but there's a much simpler way.
@Richard I managed to get a solution with that method. Want to see?
 
yes
I also have that one I thnik
 
What have you got?
 
366 =(1⎕DT ⊂1985 1 1) -1⎕DT ⊂1984 1 1
but not finished
⍥ can do wonders
and skipping the 1 1 did not work
 
3:31 PM
Ah, clever. I was computing the day number of Jan 1st the next year, then subtracting one.
@Richard It will if you make sure the year is a vector.
OK, I have a solution using your method. Let me know if you want it.
 
1⎕DT,1984 also not?
@Adám yes ok
 
@Richard 1⎕DT⊂,1984
{366=-⌿1 ⎕DT 1 0∘.+,¨⍵} or -⌿¯365⍪⍨1⎕DT 1 0∘.+,¨
My awkward solution using the next Jan 1st was 366=2⊃¨¯10⎕DT¯1+1⎕DT 1+,¨
 
nice ,thanks
 
¯10⎕DT converts to a format that begins with year and day-of-year, giving us the number of days in the year by getting the day-of-year number for Dec 31.
But it is much easier to construct Feb 29 for each year, and check if valid with 0⎕DT
 
@Adám :)
 
3:39 PM
Want to try, or shall I give it?
 
sure
 
(sorry, that was bad form, editing a yes/no question into a choice question)
 
I am trying
 
Take your time!
 
{0⎕DT⊂⍵ 2 29}¨
 
3:43 PM
Yes, that works, or a bit more elegant, imo, but worse memory-wise: 0⎕DT,∘2 29¨
And with that, we've done this problem. See you next week for 2020-5: Stepping in the Proper Direction!
 
thanks, next week!
 
4:10 PM
@Adám why'd that be worse memory wise?
 

« first day (2347 days earlier)      last day (300 days later) »