The format I was after is this:

2010-12-20 07:44:19 UTC-5

However, the code can of course be easily modified for various other formats. Here is the code:

#include <cstdio> #include <ctime> #include <windows.h> #define MAX_STR_LEN 30 int WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { // tz_offset is double to allow unusual time zones, e.g., // Nepal Time (NPT), which is UTC+05:45 and would be rendered // by this program as UTC+5.75 HGLOBAL handle; char *buffer, time_str[MAX_STR_LEN]; time_t rawtime, utc_as_local; tm *ptm; double tz_offset; int dec_places = 2, fpart; // prepare clipboard if (!OpenClipboard(NULL)) return 0; EmptyClipboard(); // get and format current time time(&rawtime); ptm = gmtime(&rawtime); utc_as_local = mktime(ptm); ptm = localtime(&rawtime); tz_offset = difftime(rawtime, utc_as_local) / 3600; fpart = (int)(tz_offset * 100) % 100; if (fpart == 0) dec_places = 0; else if (fpart % 10 == 0) dec_places = 1; sprintf(time_str, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d UTC%+.*f", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, dec_places, tz_offset); // copy data to clipboard handle = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, strlen(time_str) + 1); buffer = (char*)GlobalLock(handle); strcpy(buffer, time_str); GlobalUnlock(handle); SetClipboardData(CF_TEXT, handle); // clean up CloseClipboard(); return 0; }

]]>

- My love for you is a monotonically increasing unbounded function. (I think in order to see this as funny you have to imagine somebody saying it
*with feeling*.) - Your beauty cannot be spanned by a finite basis of vectors.
- I don’t like my current girlfriend. Mind if I do a you-substitution?
- I wish I were your problem set, because then I’d be really hard, and you’d be doing me on the desk.

Also worthy of mention:

- Your lab bench, or mine?
- Your mama’s so fat she has a proper subgroup isomorphic to herself.
- I’m a fermata… hold me.

Last but not least, there is this gem:

A doctor, a lawyer and a mathematician are discussing the relative merits of having a wife or a mistress.

The lawyer says: “For sure a mistress is better. If you have a wife and want a divorce, it causes all sorts of legal problems.”

The doctor says: “It’s better to have a wife because the sense of security lowers your stress and is good for your health.”

The mathematician says: “You’re both wrong. It’s best to have both so that when the wife thinks you’re with the mistress and the mistress thinks you’re with your wife — you can do some mathematics.”

]]>

2, 4, 6, 8, 10, …

Of course the expected answer is 12. But then I could tell you that these terms correspond with the formula

so the *actual* sequence has

2, 4, 6, 8, 10, 14, 26, 58, 130, 272, …

This illustrates a common misconception that there is a unique solution to these sorts of problems. In fact, starting with the first five evens, we can pick any real number as the sixth term and find a polynomial of degree at most 5 to model it using some basic linear algebra.

In general, when given the first terms of a sequence, we can find a polynomial of degree at most that will fit those terms. We can do so by solving a system of linear equations whose form can easily be worked out on paper by looking at a few small cases; this technique is described in detail in the article “When Every Answer Is Correct: Why Sequences and Number Patterns Fail the Test”, by Donald L. White. Another way to find a polynomial involves finite differences, but I won’t go into that here; if you’re interested, see this thread on MHF, or do some internet searches.

Here is some Mathematica code I wrote to find a polynomial given a list of terms. There should be a more concise way to write it; I’m not very versed in Mathematica shortcuts.

SeqPolyFit[L_] := ( len = Length[L]; M = Table[i^j, {i, 1, len}, {j, 0, len - 1}]; S = LinearSolve[M, L]; Sum[S[[i]] * n^(i - 1), {i, 1, len}] )

Example usage:

I couldn’t find an online tool that does this computation, although it seems like someone would have made one by now. Maybe I’ll make one down the road.

]]>

]]>

I plan on reporting this to Yahoo! as soon as I can reproduce it to be positive it is a consistent problem.

Web developers beware! If it can happen to a large corporation with vast resources, maybe it could happen to you too.

]]>

I recently came across an easy but fun problem posted on Yahoo! Answers. It could make for a good code golf exercise. I will write the problem statement in a purposely vague manner, but clear enough to make the goal obvious; specifics can be added according to any particular contest or testing environment.

Using only recursion and no loops, print a triangular pattern of asterisks according to the following example:

Input:

1 3

Output:

* * ** *** ** *

Input/output wasn’t part of the original problem statement, so the code below is for hard-coded n-values. Also, the original problem statement required the solution to be in Java.

Initially I found it most convenient to separate the pattern into two parts, as follows:

class AsterisksPattern { public static void main(String[] args) { printPattern(5); } static void printPattern(int n) { printPatternAsc(1, n); printPatternDesc(n); } static void printPatternAsc(int ind, int stop) { if (ind == stop) return; printLine(ind); printPatternAsc(ind + 1, stop); } static void printPatternDesc(int ind) { if (ind == 0) return; printLine(ind); printPatternDesc(ind - 1); } static void printLine(int ind) { if (ind == 0) { System.out.println(); return; } System.out.print("*"); printLine(ind - 1); } }

That code felt long, so I reduced it to this:

class AsterisksPattern { public static void main(String[] args) { int n = 5; printPattern(1, n, 1); } static void printPattern(int ind, int stop, int inc) { if (ind == stop) { if (stop == 0) return; printLine(ind); printPattern(ind - 1, 0, -1); return; } printLine(ind); printPattern(ind + inc, stop, inc); } static void printLine(int ind) { if (ind == 0) { System.out.println(); return; } System.out.print("*"); printLine(ind - 1); } }

But this seems like just the sort of problem that could be solved with a few brilliantly cryptic and diabolical squiggles in Perl. I’m thinking of suggesting it for the SHORTEN contest at SPOJ. However, I’m guessing it would require a special judge, and I have no idea how that works.

]]>

I also personally dislike the idea of medications for such problems, although in some extreme cases (for example, having hallucinations that won’t go away) then I think medications are perfectly reasonable. But for non-hallucination type situations, I believe in human spirit to overcome such things!

Of course many will disagree, but I believe this firmly.

To me it boils down to two things: (1) awareness (2) control.

(1) awareness as in paying attention, noticing things, being observant.

(2) control meaning being able to directly influence some outcome.

In a nutshell, if you make an effort to increase your awareness of yourself and your surroundings, and what you can and can’t control, you will realize that you have some measure of control over your mood, and will start feeling as though you have the power not to be depressed, simply by the way you choose to live life. Maybe you don’t buy it, but it works for me. It’s like a muscle that people don’t use; like how some people can twitch their ears and others can’t. But I think basically everyone has this ability and we simply forget sometimes because it’s not practiced or emphasized.

If you observe babies, you will notice that sometimes they are really really happy and then, one second later, for no apparent reason, they can become very upset…. and then another few seconds later can be very happy again. Basically, if babies can do it, why can’t we? But since we’re older, we can do it a little more.. purposefully.

]]>

In this post, I will discuss a snippet of Java code one might not normally think to write. I understand that “Java code golf” is practically an oxymoron, since it seems the goal of Java is to write code using as *many* keystrokes as possible, but let us overlook this point for now.

Suppose we want to increment a variable by an integer from standard input, but only if that integer is positive. We could use this code fragment:

Scanner r = new Scanner(System.in); int x, s = 0; // do stuff with s x = r.nextInt(); s += x > 0 ? x : 0;

But we could accomplish the same thing without introducing the variable x, as follows:

Scanner r = new Scanner(System.in); int s = 0; // do stuff with s s += (s = r.nextInt()) > 0 ? s : 0;

If you don’t see how it works, take a closer look. The variable s doubles as its own temporary variable! I admit it’s not as cool as the XOR swap algorithm, but I still got a kick out of discovering it while playing around.

Of course, I always try my best to contribute to the production of completely unreadable code.

]]>

]]>

]]>