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 = {}>
<!--- first make the dates --->
<cfloop index="x" from="1" to="100">
<cfset arrayAppend(dates, dateAdd("d", randRange(-1000,1000), now()))>
</cfloop>
<cfloop index="x" from="1" to="#arrayLen(dates)#">
<cfif dayOfWeek(dates[x]) neq myfunc(dates[x])>
<cfset result.status = "fail">
<cfset result.message = "Your function said the DOW for #dateformat(dates[x])# was #myfunc(dates[x])# and it should be #dayofweek(dates[x])#">
<cfreturn result>
</cfif>
</cfloop>
<cfset result.status = "pass">
<cfreturn result>
</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!
Archived Comments
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;
}
As just an FYI, you don't have to name your func "mycheatfunc" - that's just how I named mine.
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>
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;
}
Check out my solution on Pastebin: http://pastebin.com/B3G8hdQk - hope you don't have anything important running when you test it :)
Wow. That. Is. Evil.
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.
Actually I would have accepted that too. ;)
<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>
@Tami - your entry is like a mini-soap opera.
Oops, I forgot... I have the whole function w/ your testharness here: http://hhwd.com/diversions/...
Art imiates life -- at least at my house ;)
Ok Joshua, not only is it evil, it's freaking awesome.
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>
Loving these - thanks guys. :)
Couldn't think of anything humorous, but at least it's complicated and involved :)
Enjoy
http://pastebin.com/eCfXQNSX
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.
Dates are very cool in general - kudos to you for digging into that.
Here is my version.
http://pastebin.com/fVMrnPVr
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
@AXL -- LOVE IT!
Where the heck is Nerd's Master???
Eh?