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!
KTHXBYE


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

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;');


Pango Silliness

September 24, 2010 at 17:50 (silly)

Next time you need to buy your friend a birthday present, save money by getting them a deluxe 0-sized Pango board!

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.

Undocumented Ext JS Grouping Sort Property

July 22, 2010 at 04:28 (extjs, javascript)

Background: Ext JS is an open source JavaScript library used for web design and geared towards interactive apps using technology such as Ajax. I am using version 3.2.1.

Today after an hour or two of frustration, I discovered an omission in the official online documentation of Ext JS. Under Ext.data.GroupingStore, the very useful property groupDir is mentioned nowhere, as of the time of this post. Without this property, it is exceedingly difficult (in my experience) to specify a simple sorting preference on the grouping field for this type of grid.

MediaWiki – Multi-Line <pre></pre> within List

July 14, 2010 at 21:03 (mediawiki, tutorial)

Background: MediaWiki is open source software used to create wikis; it was developed for and is currently used by Wikipedia. I am using version 1.15.4.

I’ve spent a lot of time searching without success for a way to insert a multi-line pre-formatted block of text within an ordered or unordered list in MediaWiki using wiki markup * and # syntax. Today I finally figured out this can be done using either &#10; (Line Feed) or &#13; (Carriage Return). I haven’t tested across platforms or browsers, but here is an example: