Interesting Coding Problem
-
How Should Schools Grade Unexpected-But-Correct Answers On Coding Tests? - Slashdot[^] I solved this back in the early 80's using Turbo Pascal. Back then, the solution wasn't "unexpected", because we didn't have convenient framework methods available to us that actually did the math. Here's my approach, converted to C# from that ancient Pascal code (yes, I still have source code from the 80's on my box).
static int CountLeapYears2(int minYear, int maxYear)
{
// do the basic math up here so we don't clutter the final expression
int years = (maxYear - minYear);// A leap year is evenly divisible by 4, and those that are divisible by 100 AND 400. // This is why I use 0.03 to find the number of periods to subtract from the number // of years being evaluated. int periods = (int)(years \* 0.03); int count = (int)((years - (years \* 0.03)) \* 0.25); return count;
}
It should be noted that iterating from the minimum
DateTime
value to the maximumDateTime
value with the C#DateTime.IsLeapYear()
takes a measurable amount of time (for me, it was 0.0068359 seconds), while my almost 40-year old method did not produce a measurable duration at all. In the ineterst of completeness, here's my iterative method:static int CountLeapYears1(int minYear, int maxYear)
{
int count = 0;
for (int i = minYear; i <= maxYear; i++)
{
if (DateTime.IsLeapYear(i))
{
count++;
}
}
return count;
}".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
How Should Schools Grade Unexpected-But-Correct Answers On Coding Tests? - Slashdot[^] I solved this back in the early 80's using Turbo Pascal. Back then, the solution wasn't "unexpected", because we didn't have convenient framework methods available to us that actually did the math. Here's my approach, converted to C# from that ancient Pascal code (yes, I still have source code from the 80's on my box).
static int CountLeapYears2(int minYear, int maxYear)
{
// do the basic math up here so we don't clutter the final expression
int years = (maxYear - minYear);// A leap year is evenly divisible by 4, and those that are divisible by 100 AND 400. // This is why I use 0.03 to find the number of periods to subtract from the number // of years being evaluated. int periods = (int)(years \* 0.03); int count = (int)((years - (years \* 0.03)) \* 0.25); return count;
}
It should be noted that iterating from the minimum
DateTime
value to the maximumDateTime
value with the C#DateTime.IsLeapYear()
takes a measurable amount of time (for me, it was 0.0068359 seconds), while my almost 40-year old method did not produce a measurable duration at all. In the ineterst of completeness, here's my iterative method:static int CountLeapYears1(int minYear, int maxYear)
{
int count = 0;
for (int i = minYear; i <= maxYear; i++)
{
if (DateTime.IsLeapYear(i))
{
count++;
}
}
return count;
}".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013wrong forum? It looks more a like a start point for a debate in the lounge
M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.
-
wrong forum? It looks more a like a start point for a debate in the lounge
M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.
Well, it was a tech article, and I simply commented that I'd done the "unexpected" almost 40 years ago...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
Well, it was a tech article, and I simply commented that I'd done the "unexpected" almost 40 years ago...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013#realJSOP wrote:
Well, it was a tech article,
It looks more like a reddit thread It doesn't matter... I just pointed it out, just in case you had a lapsus. But if it was on purpose... ok :)
M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.
-
How Should Schools Grade Unexpected-But-Correct Answers On Coding Tests? - Slashdot[^] I solved this back in the early 80's using Turbo Pascal. Back then, the solution wasn't "unexpected", because we didn't have convenient framework methods available to us that actually did the math. Here's my approach, converted to C# from that ancient Pascal code (yes, I still have source code from the 80's on my box).
static int CountLeapYears2(int minYear, int maxYear)
{
// do the basic math up here so we don't clutter the final expression
int years = (maxYear - minYear);// A leap year is evenly divisible by 4, and those that are divisible by 100 AND 400. // This is why I use 0.03 to find the number of periods to subtract from the number // of years being evaluated. int periods = (int)(years \* 0.03); int count = (int)((years - (years \* 0.03)) \* 0.25); return count;
}
It should be noted that iterating from the minimum
DateTime
value to the maximumDateTime
value with the C#DateTime.IsLeapYear()
takes a measurable amount of time (for me, it was 0.0068359 seconds), while my almost 40-year old method did not produce a measurable duration at all. In the ineterst of completeness, here's my iterative method:static int CountLeapYears1(int minYear, int maxYear)
{
int count = 0;
for (int i = minYear; i <= maxYear; i++)
{
if (DateTime.IsLeapYear(i))
{
count++;
}
}
return count;
}".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013Are you sure you've got that conversion right? The methods return different values in quite a lot of cases.
const int MIN_YEAR = 1753;
const int MAX_YEAR = 2199;for (int minYear = MIN_YEAR; minYear <= MAX_YEAR; minYear++)
for (int maxYear = minYear; maxYear <= MAX_YEAR; maxYear++)
{
int y1 = CountLeapYears1(minYear, maxYear);
int y2 = CountLeapYears2(minYear, maxYear);
if (y1 != y2) Console.WriteLine("({0}, {1}) :: {2} != {3}", minYear, maxYear, y1, y2);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer