# Challenge: Un-Recursive (Simple?)

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

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

• MrCupOfT

Reputation: 2297
• Posts: 9,535
• Joined: 29-May 08

# Challenge: Un-Recursive (Simple?)

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

• Cabbage

Reputation: 2363
• Posts: 4,954
• 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
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)
End Function

```

### #3 CharlieMay

• This space intentionally left blank

Reputation: 1726
• Posts: 5,696
• 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

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

• MrCupOfT

Reputation: 2297
• Posts: 9,535
• 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
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
Continue While
End If
If X = 0 Then Exit While
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

• MrCupOfT

Reputation: 2297
• Posts: 9,535
• 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

```

### #6 andrewsw

• don't rely on secrecy

Reputation: 5962
• Posts: 23,496
• 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?

• MrCupOfT

Reputation: 2297
• Posts: 9,535
• 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. ( )

### #8 CharlieMay

• This space intentionally left blank

Reputation: 1726
• Posts: 5,696
• 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.

### #9 andrewsw

• don't rely on secrecy

Reputation: 5962
• Posts: 23,496
• 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
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

• MrCupOfT

Reputation: 2297
• Posts: 9,535
• Joined: 29-May 08

## Re: Challenge: Un-Recursive (Simple?)

Posted 02 May 2013 - 02:36 PM

CharlieMay, 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.

andrewsw, 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

### #11 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• 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

• MrCupOfT

Reputation: 2297
• Posts: 9,535
• 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)?

### #13 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

## Re: Challenge: Un-Recursive (Simple?)

Posted 03 May 2013 - 01:49 PM

• MrCupOfT

Reputation: 2297
• Posts: 9,535
• Joined: 29-May 08

## Re: Challenge: Un-Recursive (Simple?)

Posted 04 May 2013 - 08:52 AM

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)
WorkingDays As Integer) As Date
Dim delta = If(WorkingDays < 0, -1, 1)
End Function

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

```

The challenge is to un-recursive the recursive version.
Ie. Write the non-recursive version.

### #15 andrewsw

• don't rely on secrecy

Reputation: 5962
• Posts: 23,496
• Joined: 12-December 12

## Re: Challenge: Un-Recursive (Simple?)

Posted 04 May 2013 - 09:48 AM

Spoiler