Friday Puzzler: The Days of The Week

This post is more than 2 years old.

Here is a little puzzler that I think will be fun, and hopefully much simpler than my last one. For today's puzzler, you must use ColdFusion to create a function that returns the day of the week (numerical) for a date. I know what you're thinking - doesn't ColdFusion have that built in? It does. But your task is to recreate it. I have absolutely no guidelines for how you recreate it - except that you can't - obviously - use the same logic that someone else does. While the winner of these things are pretty much always picked arbitrarily, today's is going to be even more crazy. I want to see the most weird, stupid, insane, etc, ways to solve this problem. Go crazy. The only restriction is that you have to run your code via a test harness to ensure it works right. I've written one for you. It allows you to pass in your UDF and it confirms it works for a large sample of dates.

<cffunction name="testHarness"> <cfargument name="myfunc" required="true"> <cfset var dates = []> <cfset var x = ""> <cfset var result = {}>

&lt;!--- first make the dates ---&gt;
&lt;cfloop index="x" from="1" to="100"&gt;
	&lt;cfset arrayAppend(dates, dateAdd("d", randRange(-1000,1000), now()))&gt;
&lt;/cfloop&gt;	

&lt;cfloop index="x" from="1" to="#arrayLen(dates)#"&gt;
	&lt;cfif dayOfWeek(dates[x]) neq myfunc(dates[x])&gt;
		&lt;cfset result.status = "fail"&gt;
		&lt;cfset result.message = "Your function said the DOW for #dateformat(dates[x])# was #myfunc(dates[x])# and it should be #dayofweek(dates[x])#"&gt;
		&lt;cfreturn result&gt;
	&lt;/cfif&gt;
&lt;/cfloop&gt;

&lt;cfset result.status = "pass"&gt;
&lt;cfreturn result&gt;

</cffunction>

And as an example, check this one. It will fail - eventually.

<cfscript> function mycheatfunc(d) { if(dayofweek(d) == 7) return 8; return dayofweek(d); } </cfscript>

<cfset res = testHarness(mycheatfunc)> <cfdump var="#res#" label="The Result">

Unfortunately, I don't have anything to give away today - except the pride of being called King Nerd. So get cracking!

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by David Hammond posted on 10/14/2011 at 6:07 PM

Okay, not sure why I felt compelled to do this while I have a million other things to do this morning, but there you go. My solution is not really weird or insane, but it is stupid. It's based on the fact that this is the FRIDAY Puzzler.

function mycheatfunc(d)
{
var dayofweek = 0;
var friday = CreateDate(Year(Now()),Month(Now()),Day(Now()));
d = CreateDate(Year(d),Month(d),Day(d));
dayofweek = (7006 + dateDiff("d",friday,d)) % 7;
if (dayofweek is 0) {
dayofweek = 7;
}
return dayofweek;
}

Comment 2 by Raymond Camden posted on 10/14/2011 at 6:16 PM

As just an FYI, you don't have to name your func "mycheatfunc" - that's just how I named mine.

Comment 3 by Angelo Marras posted on 10/14/2011 at 6:53 PM

Probably not crazy enough...

<cffunction name="mycheatfunc">
<cfargument name="d" required="true">

<cfset var myWeekday = DateFormat(arguments.d, "dddd")>
<cfset var arWeekday = ArrayNew(1)>

<cfset arWeekday[1] = "Sunday">
<cfset arWeekday[2] = "Monday">
<cfset arWeekday[3] = "Tuesday">
<cfset arWeekday[4] = "Wednesday">
<cfset arWeekday[5] = "Thursday">
<cfset arWeekday[6] = "Friday">
<cfset arWeekday[7] = "Saturday">

<cfreturn ArrayFind(arWeekday, myWeekday)>
</cffunction>

Comment 4 by Tyson posted on 10/14/2011 at 8:39 PM

I picked a fixed date to serve as a known day of the week and figured the requested day of the week from that.

function uberDayOfWeek(date){
/* A fixed day that I'll know the day of the week.
I've also been catching up on Doctor Who... */
var fixedDateInTime = '2011-4-22';
var fixedDayOfWeek = 6;
var result = fixedDayOfWeek;

date = dateFormat(date, "yyyy-m-d");

// Get difference between our days of the week
var dayDiff = dateDiff("d", fixedDateInTime, date);
var dateIsBefore = (dayDiff < 0);
dayDiff = abs(dayDiff mod 7);

/* Add/subtract days from our fixed day of the week
to get the requested day of the week */
for(var i=1; i <= dayDiff; i++){
if(dateIsBefore)
result = (result == 1) ? 7 : result-1;
else
result = (result == 7) ? 1 : result+1;
}

return result;
}

Comment 5 by Joshua Miller posted on 10/14/2011 at 10:34 PM

Check out my solution on Pastebin: http://pastebin.com/B3G8hdQk - hope you don't have anything important running when you test it :)

Comment 6 by Raymond Camden posted on 10/14/2011 at 10:35 PM

Wow. That. Is. Evil.

Comment 7 by Joshua Miller posted on 10/14/2011 at 10:41 PM

I considered just doing an http() call to the page and extracting out your solution and then evaluating it, but I felt that might be pushing the limits of the rules a bit.

Comment 8 by Raymond Camden posted on 10/14/2011 at 10:42 PM

Actually I would have accepted that too. ;)

Comment 9 by Tami posted on 10/14/2011 at 10:52 PM

<cffunction name="whatdayisit" returntype="numeric">
<cfargument name="d" required="yes">
<cfscript>

var daynumber = 0;
var todayis = 0;
var seed = dayofweek('1900-01-01');
writeoutput("What day would #dateformat(d, 'mm/dd/yyyy')# be?<br />");

// set conditions for asking what day is it
var DadisSmart = randrange(0,1); // dad is either smart or not
var gameison = randrange(0,1); // dad may be watching the game
var TeacherisHelpful = randrange(0,1); // teacher is sometimes helpful
var SiblingisClueless = randrange(0,1); // why would I even bother to ask this one?
var MomisBusy = randrange(0,2); // mom is almost always busy, but there is a chance I can get answer from her

if ( dadissmart and not gameison )
{ writeoutput( "Dad says the day is #dayofweekasstring(randrange(1,7))# -- but I'm just not sure<br/>");
}
else {writeoutput(" Dad doesn't know or isn't interested in finding out<br/>"); }

if (not SiblingisClueless )
{ writeoutput("Brother says day is #dayofweekasstring( randrange(2,8)-1 )#, but I don't trust him<br/>"); }

if (TeacherisHelpful)
{ writeoutput("Teacher says, ""I think today is is #dayofweekasstring( dayofweek(now()) )#, but you'll get extra credit if you look it up yourself""<br/>");
}
else { writeoutput("If I ask teacher, she'll make it a homework assignment<br/>"); }

while (MomisBusy) {
WriteOutput("Mom is busy... gotta wait...");
MomisBusy = randrange(0,2);
}
if (not MomisBusy) {
todayis = d- seed; //number of days between this day and seed date
daynumber = (todayis mod 7) + seed ; // get offset by dividing by 7,then readding seed
if ( daynumber eq 8) {daynumber = 1;} //reset if it goes over 7
WriteOutput(" Mom says, ""Well honey, today must be #dayofweekasstring(daynumber)# cause it's #listgetat('pot-roast,chicken,burger,tuna,hotdog,pizza,spaghetti', daynumber)# night"" <br/><br/><hr/>");
}
</cfscript>
<cfreturn daynumber>
</cffunction>

Comment 10 by Raymond Camden posted on 10/14/2011 at 10:53 PM

@Tami - your entry is like a mini-soap opera.

Comment 11 by Tami posted on 10/14/2011 at 10:54 PM

Oops, I forgot... I have the whole function w/ your testharness here: http://hhwd.com/diversions/...

Comment 12 by Tami posted on 10/14/2011 at 10:55 PM

Art imiates life -- at least at my house ;)

Comment 13 by Chad Larter posted on 10/14/2011 at 11:42 PM

Ok Joshua, not only is it evil, it's freaking awesome.

Comment 14 by Joshua Miller posted on 10/15/2011 at 12:24 AM

Just for grins, I'm going to enter a second effort:

<cfscript>
function OfficialDayOfWeek(d){
var h = new http();
h.setMethod("get");
h.setUrl('http://adobe.ly/qa2DrM');
var r = h.send().getPrefix();
var s = Find("<title>",r.filecontent,1);
var e = Find("</title>",r.filecontent,s);
var c = e-s;
var t = Mid(r.filecontent,s,c);
var m = ListLast(t," ");
return evaluate(m & "('" & dateformat(d,"mm/dd/yyyy") & "')");
}
</cfscript>

Comment 15 by Raymond Camden posted on 10/15/2011 at 12:25 AM

Loving these - thanks guys. :)

Comment 16 by Nick Harvey posted on 10/15/2011 at 2:46 AM

Couldn't think of anything humorous, but at least it's complicated and involved :)

Enjoy

http://pastebin.com/eCfXQNSX

Comment 17 by Nick Harvey posted on 10/15/2011 at 4:06 AM

So in testing a bit more, I found out that my script didn't work prior to the Gregorian switch in 1582. I know your test harness doesn't go back that far, but I thought it was important. So there's a new pastebin.

http://pastebin.com/zPCbd7ik

Thanks wikipedia, I was wandering why CF wouldn't let me createDate(1582, 10, 12)!! Seems that day and 9 others never existed. How cool.

Comment 18 by Raymond Camden posted on 10/15/2011 at 4:58 AM

Dates are very cool in general - kudos to you for digging into that.

Comment 19 by AXL posted on 10/15/2011 at 3:58 PM

Here is my version.
http://pastebin.com/fVMrnPVr

Comment 20 by Dave White posted on 10/15/2011 at 7:23 PM

I found an article about how you can tell someone the Day Of The Week given any date (and do it in your head).

It took me a while to figure out how to code it and here is my example.

You can find the article at

http://www.jimloy.com/math/...

It reminded me of a DIBOL function I had to write when I was sweet 16 that calculated wether a given year was a Leap Year (wish I had saved a copy of that code but you don't think of that when your 16 and the internet had not even been invented).

Yes it is tag based (but I am a bit old fashioned in that way)

What I would really like to know, is what the dayofweek function in CF looks like, perhaps Ray Camden can get the code for this and share it with us.

My code is on PasteBin at the following url :-

http://pastebin.com/cHB77YUv

Comment 21 by Tami posted on 10/15/2011 at 8:19 PM

@AXL -- LOVE IT!

Comment 22 by AXL posted on 11/2/2011 at 10:15 PM

Where the heck is Nerd's Master???

Comment 23 by Raymond Camden posted on 11/2/2011 at 10:25 PM

Eh?