Challenge: Un-Recursive (Simple?)

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

50 Replies - 19599 Views - Last Post: 18 September 2013 - 04:57 AM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Challenge: Un-Recursive (Simple?)

Post icon  Posted 02 May 2013 - 04:43 AM

Challenge: Un-Recursive

The participation in the challenges seems to have dropped off, maybe the recent ones where a little to challenging? So let's go back to our roots and do a simple one.

Challenge

Write a non-recursive version of the following code.
  Public Function AddWorkDays( D As Date, X As Integer ) As Date
    Return AddWorkDays( D , X , If( ( X < 0 ) , -1 , 1 ) )
  End Function

  Private Function AddWorkDays( D As Date, X As Integer , A As Integer ) As Date
    If D.DayOfWeek = DayOfWeek.Saturday Then D = D.AddDays(A)
    If D.DayOfWeek = DayOfWeek.Sunday   Then D = D.AddDays(A)
    Return If( ( X = 0 ), D , AddWorkDays( D.AddDays( A ) , ( X - A  ) , A ) )
  End Function


Edit: Use Recursive Function in Post# 4 instead.

The purpose of the code is to return the next (or previous) work day, With Saturday and Sunday being non-work days.

Spoiler





Entrants
  • Frameworks required.
  • vb.net submissions are preferred.
  • SourceCode must be in spoilered code tags.
    Spoiler

    If you don't want to reveal your code, then don't submit a submission.
  • If you like you can include a Zip-File of the project / executable.

This post has been edited by AdamSpeight2008: 05 May 2013 - 02:56 PM


Is This A Good Question/Topic? 2
  • +

Replies To: Challenge: Un-Recursive (Simple?)

#2 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2020
  • View blog
  • Posts: 4,188
  • Joined: 11-December 07

Re: Challenge: Un-Recursive (Simple?)

Posted 02 May 2013 - 08:34 AM

There is a bug in your code. When I supply a negative value, it sometimes returns a Saturday. Here is an inelegant fix:

    Public Function AdamsAddWorkDays(ByVal D As Date, ByVal X As Integer) As Date
        Return AdamsAddWorkDays(D, X, If((X < 0), -1, 1))
    End Function

    Private Function AdamsAddWorkDays(ByVal D As Date, ByVal X As Integer, ByVal A As Integer) As Date
        If D.DayOfWeek = DayOfWeek.Saturday Then D = D.AddDays(A)
        If D.DayOfWeek = DayOfWeek.Sunday Then D = D.AddDays(A)
        If D.DayOfWeek = DayOfWeek.Saturday Then D = D.AddDays(A)
        Return If((X = 0), D, AdamsAddWorkDays(D.AddDays(A), (X - A), A))
    End Function


Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Challenge: Un-Recursive (Simple?)

Posted 02 May 2013 - 09:07 AM

Here's my version:
Spoiler


Tested with the following
myAddWorkDays(#5/2/2013#, 1) 'Returned 5/3/2013
myAddWorkDays(#5/2/2013#, -1)'Returned 5/1/2013
myAddWorkDays(#5/3/2013#, 1) 'Returned 5/6/2013
myAddWorkDays(#5/3/2013#, -1)'Returned 5/2/2013
myAddWorkDays(#5/6/2013#, 1) 'returned 5/7/2013
myAddWorkDays(#5/6/2013#, -1)'returned 5/3/2013
myAddWorkDays(#5/4/2013#, 1))'Returned 5/6/2013
myAddWorkDays(#5/4/2013#, -1)'Returned 5/3/2013
myAddWorkDays(#5/5/2013#, 1))'Returned 5/6/2013
myAddWorkDays(#5/5/2013#, -1)'Returned 5/3/2013

This post has been edited by CharlieMay: 02 May 2013 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Challenge: Un-Recursive (Simple?)

Posted 02 May 2013 - 09:22 AM

Nice test there, shame the outpus doesn't show the day of the week. DOH!

Better Recursive version
Private Function AddWorkDays(D As Date, X As Integer, A As Integer) As Date
  If (D.DayOfWeek = DayOfWeek.Saturday) OrElse 
     (D.DayOfWeek = DayOfWeek.Sunday  ) Then Return AddWorkDays(D.AddDays(A),X,A)
  Return If(X = 0, D, AddWorkDays(D.AddDays(A), ( X-A), A))
End Function



non-recursive
  Private Function AddWorkDays_Non(D As Date, X As Integer, A As Integer) As Date
    While True
      If ( D.DayOfWeek = DayOfWeek.Saturday ) OrElse 
         ( D.DayOfWeek=DayOfWeek.Sunday     ) Then
        D = D.AddDays( A )
        Continue While
      End If
      If X = 0 Then Exit While 
      D = D.AddDays( A )
      X - =A
    End While
    Return D
  End Function


The use of the While True feels so wrong.

NOTE:
The aim of the challenge is to write the non-recursive version.

This post has been edited by AdamSpeight2008: 02 May 2013 - 10:57 AM

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Challenge: Un-Recursive (Simple?)

Posted 02 May 2013 - 11:23 AM

CharlieMay can return incorrect dates.
What happens when x = 0 ? or if x > 4
Input Date is Friday

Let's say D is a Monday and X is positve
Mon   0   5  10
Tue   1   6  11
Wed   2   7  12
The   3   8  13
Fri   4   9  14
Sat
Sun


Now x is negative

Mon   0 -5 -10
Tue     -4  -9
Wed     -3  -8
The     -2  -7
Fri     -1  -6
Sat
Sun


Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3512
  • View blog
  • Posts: 11,994
  • Joined: 12-December 12

Re: Challenge: Un-Recursive (Simple?)

Posted 02 May 2013 - 01:07 PM

I think I've misunderstood, as I'm adding a certain number of working days (positive or negative):

Spoiler

This post has been edited by AdamSpeight2008: 02 May 2013 - 01:11 PM
Reason for edit:: Don't you know how to use CODE Tags yet?

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Challenge: Un-Recursive (Simple?)

Posted 02 May 2013 - 01:25 PM

andrewsw: Wrong.
Read the challenge. No working Days are a Saturday or a Sunday.

If the Date Input (dte) is Non Work Day, thus you can't return it as Workday. ( :devil2: )
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Challenge: Un-Recursive (Simple?)

Posted 02 May 2013 - 01:44 PM

I see the error you pointed out and will fix it, but for clarification, this routine is to take the date entered and either move it back to the first previous working day or the next working day from the date entered.

Is that correct? Or do you take the integer input and move the date to that day IF it is not Saturday or Sunday where if it was Saturday or Sunday it would then proceed from there to the next working day based on negative or not.

I guess, I was thinking the -1, 1 meant move backward or forward one working day from the date entered.

Also, what did you mean by too bad it didn't return day of the week. I went off of your code which returned mm/dd/yyyy result and not the day of the week for the date.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3512
  • View blog
  • Posts: 11,994
  • Joined: 12-December 12

Re: Challenge: Un-Recursive (Simple?)

Posted 02 May 2013 - 01:52 PM

If the date submitted is a weekend and x is 0 then, if we want to return the previous working day (rather than the next):

    Private Function AddWorkDays(ByVal dte As Date, ByVal x As Integer) As Date
        If dte.DayOfWeek = DayOfWeek.Saturday Or dte.DayOfWeek = DayOfWeek.Sunday Then
            dte = dte.AddDays(-1 - If(dte.AddDays(-1).DayOfWeek Mod 6 = 0, 1, 0))
        End If
        While x <> 0
            dte = dte.AddDays(If(x < 0, -1, 1))
            x += If((dte.DayOfWeek Mod 6 = 0), 0, If(x < 0, 1, -1))
        End While
        Return dte
    End Function

but if I've misunderstood again I'll leave it to the gurus here :)
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Challenge: Un-Recursive (Simple?)

Posted 02 May 2013 - 02:36 PM

View PostCharlieMay, on 02 May 2013 - 09:44 PM, said:

I see the error you pointed out and will fix it, but for clarification, this routine is to take the date entered and either move it back to the first previous working day or the next working day from the date entered.

What does Date.AddDays() do? Do the something similar for AddWorkDays but non-Working Days (Sat and Sun) are not included in the offset. Eg 7 Working Days => 9 (min) or 11 (max) Days depending on if the initial date was or wasn't a WorkingDay.

Quote

Is that correct? Or do you take the integer input and move the date to that day IF it is not Saturday or Sunday where if it was Saturday or Sunday it would then proceed from there to the next working day based on negative or not.

What? Confusing..

Quote

I guess, I was thinking the -1, 1 meant move backward or forward one working day from the date entered.

Also, what did you mean by too bad it didn't return day of the week. I went off of your code which returned mm/dd/yyyy result and not the day of the week for the date.

Look at the output of the test you supplied (I didn't supply any), don't you think include the day of the week would make it easier to visually check the validity of your results. so I don't have to go hunting for a calendar to see which day of the week they fall on.




View Postandrewsw, on 02 May 2013 - 09:52 PM, said:

If the date submitted is a weekend and x is 0 then, if we want to return the previous working day (rather than the next):

Look at the code.
 Dim A = If(x < 0, -1, 1) 

It should be a Monday.

Look at the charts I supplied in post #5

This post has been edited by AdamSpeight2008: 02 May 2013 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#11 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Challenge: Un-Recursive (Simple?)

Posted 03 May 2013 - 06:22 AM

I don't know if I understood the challenge... anyway, here it goes:
Spoiler

Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Challenge: Un-Recursive (Simple?)

Posted 03 May 2013 - 11:28 AM

lucky3 Wrong. What about all of the intervening Saturdays and Sundays (which are not work days)?
Was This Post Helpful? 0
  • +
  • -

#13 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Challenge: Un-Recursive (Simple?)

Posted 03 May 2013 - 01:49 PM

AdamSpeight2008 I have no idea what you are talking about. Can you explain please?
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Challenge: Un-Recursive (Simple?)

Posted 04 May 2013 - 08:52 AM

:censored: so called experts. I've give you lot enough explanations.
Last one.

A worked example, initial date falls on a Thursday and we add 14 Work days.
WD  
 0 Thu
 1 Fri
   Sat
   Sun
 2 Mon
 3 Tue
 4 Wed
 5 Thu
 6 Fri
   Sat  A Non Work Day
   Sun  A Non Work Day
 7 Mon
 8 Tue
 9 Wed
10 Thu <- This 14 standard days ( but it the terms of the challenge it includes 4 non work day. So is incorrect to return.) 
11 Fri
   Sat  A Non Work Day
   Sun  A Non Work Day 
12 Mon
13 Tue
14 Wed  <- Return this date  


Let's do a round trip, so this time Initial Date falls on Wed and is -14 Working Days
 WD  
  0 Wed
- 1 Tue
- 2 Mon
    Sun  A Non Work Day
    Sat  A Non Work Day
- 3 Fri
- 4 Thu
- 5 Wed
- 6 Thu
- 7 Fri
    Sun  A Non Work Day
    Sat  A Non Work Day
- 8 Fri
- 9 Thu
-10 Wed
-11 Tue <- This 14 standard days ( but it the terms of the challenge it includes 4 non work day. So is incorrect to return.) 
-12 Mon
    Sat  A Non Work Day
    Sun  A Non Work Day 
-13 Fri
-14 Thu <- Return this date  



A lot of the entries so are including Saturdays and Sundays (that don't count as a work day).
If Workdays is Zero and initial date is a Sat or Sun then return Monday, this is because
Dim delta = If(WorkingDays < 0, -1, 1) say add days if the value is zero.

The recursive version of the code.
  '' This the public function (the api so to speak)
  Public Function AddWorkDays(InitialDate As Date,
                               WorkingDays As Integer) As Date
    Dim delta = If(WorkingDays < 0, -1, 1)
    Return AddWorkDays(InitialDate, WorkingDays, delta)
  End Function

  ' This private and allows an recursive implementation without publicly exposing the direction parameter
  ' (that is worked out once). 
  Private Function AddWorkDays(InputDate As Date,
                               WorkingDays As Integer,
                               Delta As Integer) As Date
    If (InputDate.DayOfWeek = DayOfWeek.Saturday) OrElse
       (InputDate.DayOfWeek = DayOfWeek.Sunday) Then Return AddWorkDays(InputDate.AddDays(Delta), WorkingDays, Delta)
    If WorkingDays <> 0 Then Return AddWorkDays(InputDate.AddDays(Delta), (WorkingDays - Delta), Delta)
    Return InputDate
  End Function



The challenge is to un-recursive the recursive version.
Ie. Write the non-recursive version.
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3512
  • View blog
  • Posts: 11,994
  • Joined: 12-December 12

Re: Challenge: Un-Recursive (Simple?)

Posted 04 May 2013 - 09:48 AM

Spoiler

Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »