# Challenge: Un-Recursive (Simple?)

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

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

### #31 lucky3

• Friend lucky3 As IHelpable

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

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

Posted 05 May 2013 - 02:36 AM

Now that the role of X parameter is clear to me:
Spoiler

This post has been edited by lucky3: 05 May 2013 - 02:37 AM

• MrCupOfT

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

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

Posted 05 May 2013 - 08:47 AM

What the hell? Now both of lucky3 and andrewsw entries return incorrect results.
I swear and promise, all I change from the copy and paste over is the name of the function.

```d = #5/5/2013#
x = 10

both return #5/17/2013# when it should be #5/20/2013#?

d = #5/5/2013#
x = -10
both return #4/22/2013# when it should be #4/19#2013#

```

You may want to recheck those codes.

This one was inspire by looking at the IL.
```Public Function AddWorkdays_H(D As Date, X As Integer) As Date
Dim a As Integer = If(X < 0, -1, 1)
Foo:
If Not D.IsA(Saturday) AndAlso Not D.IsA(Sunday) Then
If X = 0 Then Return d
X -= a
End If
GoTo Foo
End Function

```

This post has been edited by AdamSpeight2008: 05 May 2013 - 09:06 AM

### #33 andrewsw

• blow up my boots

Reputation: 6524
• Posts: 26,402
• Joined: 12-December 12

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

Posted 05 May 2013 - 09:00 AM

Quote

d = #5/5/2013#
x = 10

both return #5/17/2013# when it should be #5/20/2013#?

d = #5/5/2013#
x = -10
both return #4/22/2013# when it should be #4/19#2013#

I was assuming that, if the user happens to supply a weekend-date AND the number 0, just push this to the following Monday, as a convenient return value. Whereas we should first push a weekend date to the Monday, and then start counting!

• MrCupOfT

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

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

Posted 05 May 2013 - 09:12 AM

Nope.

If d is weekend (non workdays) and x >= 0, then day 0 is Monday. (day 1 is Tue etc)
If d is weekend (non workdays) and x < 0, then day 0 is Friday and not Monday. (day -1 is Thu etc)

Then above could have been inferred from this and the specification of no workday are Sat and Sun.
Dim a = If(x < 0, -1, 1)

The codes work for x = 0.

A simple test is can you (sort of) round trip the dates.
```D = #5/6/2013#
R1 = AddWorkdays( D ,  10)

If D began on a work day then D should equal R2
If D began on a non workday and X>0 Then R2 should be the first Monday after the date in D.
If D began on a non workday and X<0 Then R2 should be the first Friday before the date in D

```

This post has been edited by AdamSpeight2008: 05 May 2013 - 09:49 AM

### #35 lucky3

• Friend lucky3 As IHelpable

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

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

Posted 05 May 2013 - 11:19 AM

D = #5/5/2013#

then X = 0 would be Monday, May 6th
and X = +10 would be Monday, May 20th

but X = -1 would be Thursday, May 2nd
if X = -10 should return Friday, April 19th

What X should return Friday, May 3rd (0 is Monday, -1 is Thursday)?

Edit: ignore.... 0 moves to Monday or Friday according to +/- value of X, and is Monday when X = 0 on weekend day...

Spoiler

Edit2: Well, I guess I'm still confused... If D = #5/6/2013# (Monday), then if X = 0 return would be Tuesday 7th, but if X = -1, return would be Thursday the 2nd (indicating that X = 0 in this case means Friday the 3rd?)

This post has been edited by lucky3: 05 May 2013 - 11:34 AM

### #36 andrewsw

• blow up my boots

Reputation: 6524
• Posts: 26,402
• Joined: 12-December 12

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

Posted 05 May 2013 - 11:31 AM

I will stick with my previous answer, even though it is wrong. Logically, and in use, it makes more sense to me:

If someone supplies a weekend date, asking for +1 workdays then, to me, this should be the Monday;
If they happen to supply a weekend date, and the specific value 0, then, as a convenience, I will return the following Monday (being a working day).

### #37 lucky3

• Friend lucky3 As IHelpable

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

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

Posted 05 May 2013 - 11:35 AM

I agree andrewsw, I just edited my previous reply the 2nd time.... (Simple challenge huh?)

• MrCupOfT

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

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

Posted 05 May 2013 - 11:38 AM

The data format is the American Date forma which is MM/DD/YYY. (yep got be at first)
So #5/5/2013# is Sun 5th of May 2013.

Have you done the basic test of round tripping first?

It may make more sense to you andrewsw but it was not what the client (DIC) wanted.

This post has been edited by AdamSpeight2008: 05 May 2013 - 11:42 AM

• MrCupOfT

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

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

Posted 05 May 2013 - 11:51 AM

Look at what the recursive version in Post #4 does. Note: A is passed via a public method A = If(X<0,-1,1)

Work your way through it with the various states.
D is workday
D is non work day
X is Zero.
X is negative.
X is positive.

### #40 lucky3

• Friend lucky3 As IHelpable

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

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

Posted 05 May 2013 - 12:04 PM

Spoiler

### #41 modi123_1

• Suitor #2

Reputation: 13857
• Posts: 55,328
• Joined: 12-June 08

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

Posted 05 May 2013 - 12:10 PM

A little convoluted, but after rereading what is all up an implied I believe this works.. or at least satisfies the various examples thrown around.

Quote

start: 5/5/2013 12:00:00 AM : Sunday.. find -10 working day
stop: 4/19/2013 12:00:00 AM : Friday
---------
start: 5/5/2013 12:00:00 AM : Sunday.. find 10 working day
stop: 5/20/2013 12:00:00 AM : Monday
---------
start: 5/6/2013 12:00:00 AM : Monday.. find 1 working day
stop: 5/7/2013 12:00:00 AM : Tuesday
------
start: 5/6/2013 12:00:00 AM : Monday.. find 5 working day
stop: 5/13/2013 12:00:00 AM : Monday

Spoiler

• MrCupOfT

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

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

Posted 05 May 2013 - 12:25 PM

modi123_1: It returns wrong results.
Look at what happens when d is non workday and X = 0. It should be a Monday not a Friday.

This post has been edited by AdamSpeight2008: 05 May 2013 - 12:25 PM

### #43 modi123_1

• Suitor #2

Reputation: 13857
• Posts: 55,328
• Joined: 12-June 08

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

Posted 05 May 2013 - 12:29 PM

mm.. so why would 0, the current working day, advance forward, and say not backwards? If the day entered is Saturday, the 'get working day' is 0.. what's the edge case rule for that? If zero always advance? Seems arbitrary, but okay.

Spoiler

### #44 lucky3

• Friend lucky3 As IHelpable

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

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

Posted 05 May 2013 - 12:45 PM

AdamSpeight2008 why does recursive version of function return for working days and X = 0 the same date?

• MrCupOfT

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

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

Posted 05 May 2013 - 12:49 PM

It's not arbitrary if you are tasked to convert the recursive version to a non-recursive version.
Which you were.

You certainly want it return the same results, would you.

It only advances to Monday when X = 0 if the date passed is a Saturday or Sunday, because that's what the recursive version does.

Another way to think about X is how many complete working days have passed.

lucky3 Cos its does and it is also a working day.

You lot all got monk on? or sumat?

This post has been edited by AdamSpeight2008: 05 May 2013 - 12:51 PM