exercism/website-copy

Leap: order of operation

Closed this issue · 6 comments

Some tracks have notes about the order of operation:

  • order of operation matters:
    • 75% of all years cannot be leap years because they are not multiples of 4; test year % 4 == 0 first
    • 98.97% of all years that are multiples of 4 are not multiples of 100; test year % 100 != 0 second
    • 1.03% of all years that are multiples of 4 are also multiples of 100 and 400; test year % 400 == 0 third

Please correct me if I'm wrong, but I believe a couple of percentages are incorrect here.

98.97% of all years that are multiples of 4 are not multiples of 100

The years that are multiples of 4 would make a list with an increment of 4 (1900, 1904, 1908, 1910, 1912, ...). The years in this list that are multiples of 100 would be 1/25 = 0.04 = 4%. This would make the years that are non multiples of 100 24/25 = 0.96 = 96%, and not 98.97% stated above. The note seems to be taking divisibility by 400 into account, which it shouldn't at this point.

1.03% of all years that are multiples of 4 are also multiples of 100 and 400

Years that are multiples of 4, 100, and 400 are e.g., 1600, 2000, 2400. Using the list from before, this would occur 1/100 = 0.01 = 1%, and not 1.03% stated above. I'm not sure where the extra 0.03% comes from.

I guess the third item wouldn't really make sense anymore, and should probably just state that we should test for divisibility by 400 last.

I’m the original source of those numbers, but I’ll admit they’re definitely not worded well.

It should read “98.97% of all leap years are multiples of 4 that are not multiples of 100” and “1.03% of all leap years are multiples of 4, 100, and 400”.

The point is to convey the fairly drastic diagnostic gradient... in 75% of cases you know the correct answer immediately by only testing division by 4, and in ~99% of all remaining cases you know the correct answer by further testing division by 100 ... testing for division by 400 virtually never needs to occur.

@yawpitch Thank you for explaining, now I understand :)

If I make a list with all the leap years, 1/97 will be multiples of 100. That would make 96/97 = 98.97% non multiples by 100.

I think this is a great way to show which order of operation to use!

I thought about this differently, which is why I didn't understand where the 98.97% came from:

  • Test divisibility by 4 first. This will eliminate 75% of the years with only one test.
  • Test divisibility by 100 second. This will eliminate 96% of the remaining years with only one more test. 96% because there are still some non leap years left.
  • Test for divisibility by 400 last.

What do you think, should I create a pull request using your new wording, or just leave it as it is?

I'm not tied to any particular wording, but because it's been replicated in a fair number of places it's probably best to come up with a clear phrasing and then replace them all in one go.

The key thing is to convey the obvious "power-to-weight" value gradient of the tests and their diagnostic value when applied in the proper order:

Stepping through 4 -> 100 -> 400
1. 300:400 answered, 100:400 ambiguous
2. 396:400 answered,     4:400 ambiguous
3. 400:400 answered

Vs.

Stepping through 400 -> 100 -> 4
1.     1:400 answered, 399:400 ambiguous
2.     4:400 answered, 396:400 ambiguous
3. 400:400 answered

Which at least makes more sense than:

Stepping through 100 -> 400 -> 4
1.     0:400 answered, 400:400 ambiguous
2.     4:400 answered, 396:400 ambiguous
3. 400:400 answered

Once you recognize that then it doesn't really matter the way in which it's conveyed, the order is obvious and any other ordering is simply wasteful, like assembling a sieve the wrong way round.

For me it's made sense to explain that in terms of the percentage of leap years that each of the two refining tests will unambigously answer, but since it's caused you confusion I can see that your explanation above may be less ambiguous to others. Maybe have a bit of a think about how to word your approach as clearly and concisely as possible? For instance are round percentages easier or harder for the brain to process than ratios? I'm not sure myself.

it's probably best to come up with a clear phrasing and then replace them all in one go.

I agree!

For instance are round percentages easier or harder for the brain to process than ratios?

I think percentages are easier to process, since that's being used more often. For example, right now I'm looking at the battery life of my phone, which is 65% :)

Now that you've explained it, your approach makes sense to me as well.

The mentoring notes for python/leap was commited over a year ago. Since we haven't heard of any issues before (?), I wouldn't think this causes confusion for a lot of people. Finding out would probably require some sort of survey.

I think we should stick with your approach, but maybe update the text to make it more clear.

Right ... I'll try and take a look at finding all the instances ASAP.