Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Insider News
  4. Interesting Coding Problem

Interesting Coding Problem

Scheduled Pinned Locked Moved The Insider News
csharpdelphihelpquestionannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    realJSOP
    wrote on last edited by
    #1

    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 maximum DateTime 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

    N Richard DeemingR 2 Replies Last reply
    0
    • R realJSOP

      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 maximum DateTime 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

      N Offline
      N Offline
      Nelek
      wrote on last edited by
      #2

      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.

      R 1 Reply Last reply
      0
      • N Nelek

        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.

        R Offline
        R Offline
        realJSOP
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • R realJSOP

          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

          N Offline
          N Offline
          Nelek
          wrote on last edited by
          #4

          #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.

          1 Reply Last reply
          0
          • R realJSOP

            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 maximum DateTime 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

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Are 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

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups