## Copy Date/Time to Windows Clipboard with C++

December 20, 2010 at 12:44 (c++, windows)

I was unable to find a suitable program to quickly generate a timestamp in the format I desired, so I decided to write one using C++, specifically MinGW’s g++ v4.5.0 along with <windows.h>. This is my first real experience with Windows-specific programming other than a few hello worlds, so it was instructive as well as frustrating. It took considerable time to discover and familiarise myself with SetClipboardData(). Eventually I was able to modify the code in this thread (which didn’t compile as-is) and obtain a working clipboard prototype very similar to this, which I didn’t find until afterward. After that, it was mainly a matter of understanding the functions in <ctime>.

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:

## Simple Asterisk Pattern with Recursion

October 18, 2010 at 14:23 (code golf, java)

Background: I introduced code golf in a previous post.

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:

*
*
**
***
**
*


## Reflexive Assignment in Java

October 11, 2010 at 06:54 (code golf, java)

Background: Code golf is a game in which the goal is to solve programming problems using as few keystrokes as possible. It has its roots in Perl golf, and anyone with golfing experience knows that Perl is particularly well suited to such challenges. J and K, as well as the more esoteric GolfScript, are also known for combining terse syntax with powerful features. Obfuscation is a related topic.

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:

## SPOJ TEST in LOLPython

October 2, 2010 at 06:07 (esoteric language, tutorial)

Background: I introduced SPOJ in a previous post.

If you haven’t heard, LOLCODE is a powerful programming language used mainly by cats. It is deplorable that such a beautiful and natural language has not received wider support thus far. To help remedy the situation, I decided to code a solution to TEST using LOLPython, which is derived from LOLCODE and is just as elegant. Of course the program cannot be submitted because neither LOLCODE nor LOLPython is currently supported by SPOJ. But we must have faith that one day such blatant discrimination against cats and their programming languages will end, and we must prepare for that glorious day!

I can haz code?

WHILE I CUTE?
MENUMBR CAN HAZ NUMBR raw_input WIT OK!
IZ MENUMBR KINDA LIKE 42?
KTHXBYE


Save a kitten – ask for LOLCODE support in your local programming competition (and syntax highlighter) today!

## Java Custom addSlashes()

October 1, 2010 at 06:56 (java, javascript, tutorial, web dev)

Java regexes can get pretty clunky due to the dual use of the backslash character, as is commonly known. Although there’s not a lot to it, below is some code to add slashes for these characters: backslash, line feed, carriage return, null, single quote. It is used in a web application to format JSON strings on the server before passing to the client, where the objects are then processed by JavaScript/Ext JS. Maybe someone will find it useful!

public class StringUtil {
public static String addSlashes(String s) {
s = s.replaceAll("\\\\", "\\\\\\\\");
s = s.replaceAll("\\n", "\\\\n");
s = s.replaceAll("\\r", "\\\\r");
s = s.replaceAll("\\00", "\\\\0");
s = s.replaceAll("'", "\\\\'");
return s;
}
}


Incidentally, there are other places where the following JavaScript replacement is necessary:

s = s.replace(/"/g, '&quot;');


## Frobenius Numbers – Round Robin Algorithm

September 20, 2010 at 14:50 (algorithm, java, math.NT)

Frobenius numbers are solutions to the coin problem. Let $\displaystyle 0 be coin denominations; what is the smallest sum of money that cannot be obtained using these coins? More formally, define the Frobenius number $\displaystyle g(a_1,\,\ldots\,,a_n)$ as the greatest number that is not a linear combination $\sum_{i=1}^n x_ia_i$ with $\displaystyle 0 \le x_i \in \mathbb{Z}$. The Frobenius number exists if and only if $\displaystyle a_1 > 1$ and $\displaystyle \gcd(a_1,\,\ldots\,,a_n)=1$. A special case of Frobenius numbers involves the interestingly named McNugget numbers, and there is a well-known formula when $\displaystyle n=2$ given by $\displaystyle g(a_1,a_2)=a_1a_2-a_1-a_2$ sometimes known as the Chicken McNugget Theorem.

## SPOJ, ideone, and TEST

September 14, 2010 at 05:53 (computer language, tutorial)

Background: I introduced SPOJ in a previous post.

ideone is an excellent tool that allows you to write and execute code in many languages, all online. It’s basically a pastebin with added functionality, making it an online mini-IDE. It is based on the SPOJ engine but supports some languages that SPOJ does not.

I’m mainly writing this post to report a useful discovery regarding ideone and the first SPOJ problem, TEST. If you happen to know a language like JavaScript and would like to submit a solution to TEST but can’t figure out how I/O is supposed to work, there is an easy solution through ideone. Simply go there, set the language to say JavaScript (rhino) and use the link to insert sample that appears above the textarea. A basic solution to TEST in the given language will magically appear, complete with sample input. This seems to work for just about any language on the list (even Whitespace!), and could come in handy when there is no corresponding thread in the SPOJ forums.

## How To: SPOJ TEST Using JAR File

September 12, 2010 at 07:39 (java, tutorial)

Background: Sphere Online Judge (SPOJ) is an online judge system accepting a wide variety of programming languages. It’s a great way to test your skills as a programmer, and can be quite addictive if you like a good challenge.

While browsing the SPOJ forums, I did not find a thread detailing how to successfully submit a JAR for the first problem, TEST. So, here’s a quick guide. I’m using Windows, but there should be little difference with other operating systems since everything is done through the command line. I will assume you already have the JDK installed and paths set up such that if you type java, javac, or jar from the command line the appropriate programs will be executed.

## Mac Newlines Can Cause PHP Headaches

August 31, 2010 at 21:47 (php, web dev)

While working on a project, somehow the majority of my PHP files acquired Mac style \r newline characters rather than DOS/Windows \r\n or UNIX \n. I don’t know exactly how this happened, but the \r newlines were perpetuated by Notepad++, which identified the files as Macintosh and displayed them transparently. The problem is if I take this code:

<?php
echo 'hello';
?>


and upload it to the Linux project server (on Go Daddy), it becomes:

<?phpecho 'hello';?>


which of course causes an error. It took a while to trace the problem to its source, mainly because I had no idea such newline characters existed. So now you know, and knowing is half the battle.

## Undocumented Ext JS PagingToolbar Method

August 22, 2010 at 07:01 (extjs, javascript)

Background: I’ve mentioned Ext JS in a previous post and am still using version 3.2.1.

After another fun-filled trip through ext-all-debug.js, I’ve discovered a simple yet hidden way to load an arbitrary page number in a grid that uses a PagingToolbar (Ext.PagingToolbar). Simply call yourGridPanel.getBottomToolbar().doLoad(start), where start is the row index starting from 0. So for example if you set pageSize = 10, then to display the first page call doLoad(0), second page call doLoad(10), etc. Note that when choosing a non-multiple of pageSize, the page number gets rounded up, so for example doLoad(1) would display items 2-11 (if you have 11 or more items) and report the page number as 2.

And of course, as the title of this post implies, this method is absent from the official API documentation (as of the time of this post) despite its being a public and very useful method.