Challenge: Un-Recursive (Simple?)

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

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

#31 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 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

Was This Post Helpful? 1
  • +
  • -

#32 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • 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
     D = D.AddDays(a)
  GoTo Foo 
End Function


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

Was This Post Helpful? 0
  • +
  • -

#33 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,352
  • 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!
Was This Post Helpful? 0
  • +
  • -

#34 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • 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)
R2 = AddWorkdays( R1, -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

Was This Post Helpful? 0
  • +
  • -

#35 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 05 May 2013 - 11:19 AM

AdamSpeight2008 I'm confused by your explanation. Following your example:

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

Here is my adjusted code:
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

Was This Post Helpful? 0
  • +
  • -

#36 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,352
  • 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).
Was This Post Helpful? 0
  • +
  • -

#37 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 05 May 2013 - 11:35 AM

I agree andrewsw, I just edited my previous reply the 2nd time.... ;) (Simple challenge huh?)
Was This Post Helpful? 0
  • +
  • -

#38 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • 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

Was This Post Helpful? 0
  • +
  • -

#39 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • 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.
Was This Post Helpful? 0
  • +
  • -

#40 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 05 May 2013 - 12:04 PM

How about this for testing:
Spoiler

Was This Post Helpful? 0
  • +
  • -

#41 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9073
  • View blog
  • Posts: 34,115
  • 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

Was This Post Helpful? 0
  • +
  • -

#42 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • Joined: 29-May 08

Re: Challenge: Un-Recursive (Simple?)

Posted 05 May 2013 - 12:25 PM

modi123_1: It returns wrong results. :devil2:
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

Was This Post Helpful? 0
  • +
  • -

#43 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9073
  • View blog
  • Posts: 34,115
  • 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

Was This Post Helpful? 2
  • +
  • -

#44 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 05 May 2013 - 12:45 PM

AdamSpeight2008 why does recursive version of function return for working days and X = 0 the same date?
Was This Post Helpful? 0
  • +
  • -

#45 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • 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

Was This Post Helpful? 0
  • +
  • -

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