I found Sununda's algorithm challenge to be very interesting. I'm often
amazed by the different approaches to a given problem.

In this spirit, I thought I'd offer up a basic exercise based on a
post about Ruby I came across yesterday. See:

*Calculate the number of working days between two dates*
http://www.railsonwave.com/railsonwave/2007/1/30/calculate-the-number-of-working-days-between-two-dates


Here's an approach I took, which embellishes with a refinement:

calc-workdays: func [
    "Return number of workdays between two dates, excluding holidays"
    date1 [date!] "Start date"
    date2 [date!] "End date"
    holidays [block!] "Block of dates to exclude (holidays, etc.)"
    /non "Return number of non-work days (weekend + holidays) between 2
dates"
    /local days day1 day2 diff param
][
    days: copy []
    set [day1 day2] sort reduce [date1 date2]
    diff: day2 - day1
    param: pick [[> 5 union][< 6 exclude]] either found? non [1][2]
    loop diff [
        day1: day1 + 1
        if do param/1 day1/weekday param/2 [append days day1]
    ]
    return length? do param/3 days holidays
]

Results:
>> calc-workdays now/date 3-feb-2007 [1-feb-2007]
== 1
>> print calc-workdays/non now/date 3-feb-2007 [1-feb-2007]
== 2

>> calc-workdays now/date 31-dec-2007 [25-dec-2007 1-Jan-2008]
== 237
>> print calc-workdays/non now/date 31-dec-2007 [25-dec-2007 1-Jan-2008]
== 98

Anyone care to share a different or more natural approach? One of the things
I'd like to see in REBOL's future is a guide or "phrase-book" for common
expressions found in problems big and small.

Ed


-- 
To unsubscribe from the list, just send an email to 
lists at rebol.com with unsubscribe as the subject.

Reply via email to