Mini-coding challenge

Discussion in 'OT Technology' started by Astro, Feb 8, 2004.

  1. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Create a snippet of code which finds the number of week days in a given month/year. So if you take February 2004, your code snippet would find there is 20 week days.

    Although open to all languages, lets do some speed trials with PHP. This should be original work - no cheating by hitting up google and looking up a fabricated solution. Although looking up other sites (such as php.net) for support is welcome.

    I came up with a solution, but I'll wait on posting it until there's interest from others...
     
  2. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    Is 4 a leap year? Would it be cheating to look at a calendar of the year 1?
     
  3. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    I just calculated that January 1st, 1 was a Sunday. (I hope I'm right.) I'm also assuming 4 is a leap year. I have a working chart of the start of every year for January so far. Here is 1990-2004.

    1 -- 1990
    2 -- 1991
    3 -- 1992
    5 -- 1993
    6 -- 1994
    0 -- 1995
    1 -- 1996
    3 -- 1997
    4 -- 1998
    5 -- 1999
    6 -- 2000
    1 -- 2001
    2 -- 2002
    3 -- 2003
    4 -- 2004
    (0 is Sunday, 6 is Saturday)
     
  4. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Here is my solution using C#. I'll post the code later once more people have posted.

    Exe: http://web.isy.vcu.edu/chris/code/ (you'll need the .NET Framework installed)
     
  5. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    Bah. I think I should stop now because I don't even know what the hell the rules for leap years are.

    I thought the rule went:
    - If it's divisible by 4.
    - If it's not divisible by 100 unless it's also divisible by 400.
     
  6. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    Okay. My problem is at the year 1752/1753. Was some new calendar released or something? The rule breaks here. The following snipet is not from my program, I was tracking where my program went wrong with the UNIX cal utility.
    Code:
    [[email protected] jason]$ cal 2 1752
    	February 1752
    Su Mo Tu We Th Fr Sa
    					1
    2 3 4 5 6 7 8
    9 10 11 12 13 14 15
    16 17 18 19 20 21 22
    23 24 25 26 27 28 29
    
    (the 1 is under Saturday)
    Code:
    [[email protected] jason]$ cal 2 1753
    	February 1753
    Su Mo Tu We Th Fr Sa
    			1 2 3
    4 5 6 7 8 9 10
    11 12 13 14 15 16 17
    18 19 20 21 22 23 24
    25 26 27 28
    
    (the 1 is under Thursday, it went backwards instead of forward like it did from the years 1-1752. Either that or it went forwards more.)
     
  7. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    tHEdARKhORiZON, you know we're just interested in the number of work days in a month, right? Like, how many days you'll have to show up for work during a given month.
     
  8. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    Yeah, I'm making it more difficult than it should be. :o
     
  9. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
  10. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Nice job!

    I'm curious to see if anyone will have written less lines of code than me. :o
     
  11. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    Code:
    Function weekdays(InputDate As Date) As Long
    
    Dim z As Long
    
    For z = 1 To Day(DateAdd("d", -1, DateSerial(Year(InputDate), Month(InputDate) + 1, 1)))
         weekdays = weekdays + IIf(Weekday(DateSerial(Year(InputDate), Month(InputDate), z), vbMonday) > 5, 0, 1)
    Next
    
    End Function
    
    This was the easiest way, but there's another way that's not too hard without using the weekday function.
     
    Last edited: Feb 9, 2004
  12. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Here was my solution (in PHP):

    PHP:
    function daysinmonth($month$year)
    {
        list(
    $day$daysmonth) = explode(' 'date('w t'mktime(0,0,0,$month,1,$year)));
        
    $count 0;
        for(
    $a 0$a $daysmonth$a++)
        {
            if((
    $day $a) % && ($day $a) % 6)
                
    $count++;
        }  
    // end for($a = 0; $a < $daysmonth; $a++)

        
    return $count;
    }  
    // end function daysinmonth($month, $year)
    What was really interest is this function had fewer lines of code compared to some others folks submitted, but it wasn't the fastest: more details here
     
  13. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    :o

    I didn't use the date function at all. I found the start date of each month for the year one. And for each year, the start date was one day later, except for leap years when something weird happened. Then when I had the start date for the month/year from user, I added 1 for each number of days, and if it was monday through friday, I incremented the weekdays count.
     
  14. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    How did you guys benchmark the speed of your script? I used microtime() and it gave me numbers all over the place.
     
  15. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    I had thought of calculating it that way but my way just seemed more logical to me.
     
  16. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Microtime will reflect the server load. So as the server gets hammered the longer it will take your script to process. If its a busy server, its possible the microtime numbers will fluctuate a lot.
     
  17. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    True - unless you run it client side - then it's moot ;)
     
  18. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Again, if the processor isn't doing anything, then sure, client side will be just fine. But if you do timing with Access and Excel fighting for processor time, you'll see a dent in microtime. In fact, microtime will also reflect processing speeds. So if you're going to use microtime, you really need to run all the scripts you're going to compare very close to each other. Then you'll probably want to run the test a few times to make sure you don't get any garbage numbers.
     
  19. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    I guess I just don't want to be that much of a geek to care :rofl:
     
  20. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Here is my source...

    Code:
    private void calendar_DateSelected(object sender, DateRangeEventArgs e)
     		{
     			DateTime currentDay;
     			int daysInSelectedMonth = DateTime.DaysInMonth( e.Start.Year, e.Start.Month );
     			int workDaysInMonth = 0;
     
     			for( int i = 1; i <= daysInSelectedMonth; i++)
     			{
     		    	currentDay = new DateTime( e.Start.Year, e.Start.Month, i );
     
     		    	if( currentDay.DayOfWeek.ToString().Equals("Saturday") || currentDay.DayOfWeek.ToString().Equals("Sunday") )
     				{
     					// weekend
     				}
     				else
     				{
     					// workday
     					workDaysInMonth++;
     				}
     			}
     
     			lblDisplay.Text = workDaysInMonth.ToString() + " work days.";
     		}
     
     
  21. affende

    affende Guest

    what app do you use for your java coding...i use Jbuilder and am interested in some other apps to see what i like bast.
     
  22. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    That's C#, not Java.
     
  23. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    I use JBuilder and IBM Websphere for Java. But what I posted was C#. Quite similar.
     

Share This Page