-
-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(leap): add approaches #759
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You put a lot of effort in. I like it.
I've mentored this exercise a lot. There are some more notable approaches:
- Some students like to create three variables for the three atomic sub-expressions:
IMHO that's not bad, it makes the final
bool div_by_4 = year % 4 == 0; bool div_by_100 = year % 100 == 0; bool div_by_400 = year % 400 == 0; return div_by_4 && (!div_by_100 || div_by_400);
return
easy to read and the overhead is probably acceptable in most cases. - A few students did use a lambda:
That's similar to (1) but avoids the computational overhead.
auto div_by = [year](int divisor) { return year % divisior == 0; }; return div_by(4) && (!div_by(100) || div_by(400));
- A lot of students want to solve this exercise step by step. Either starting with the most narrow case:
Or starting with the most general case:
if (year % 400 == 0) return true; if (year % 100 == 0) return false; if (year % 4 == 0) return true; return false;
That's definitely not wrong and it's easy to read. I usually challenge them to find a one-liner to get a shorter and more concise solution ... and then choose what they like more.if (year % 4 != 0) return false; if (year % 100 != 0) return true; if (year % 400 == 0) return false; return true;
- The two approaches from (3) can also be written as a series of nested
if
statements.if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) return true; return false } return true; } return false;
Whether you want to add or mention some of them in your approaches is of course up to you.
|
||
All lines produce the same result, but differ a lot in readability. | ||
The reason for their equal results, is the order of operator evaluation (`%`, `!=`, `||`, etc) or [precedence][operator-precedence]. | ||
In this example the first operator to get the attention of the compiler is the remainder, followed by the equality operators. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like the three "a % b == 0" sub-expressions would be evaluated first.
Sadly I don't have a better alternative yet, perhaps Bethany ...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could be more explicit an write down every line as they are evaluated. But that might be a bit too much.
Oh, I forgot:
I know that not all of this information will be usable in the "approaches", but maybe at least some other mentor might find it useful. |
This is very good information. As many of those are versions that would evolve into one of the provided approaches, I would not include them as their own approaches. Do we have mentoring notes on the track were this could be used? I have not done anything with the mentoring system yet. |
I will merge this to use it for the article, we could add more in the future. |
closes #758
@BethanyG
I would appreciate some language nitpicks :)