10/26/06

(not) Using "text" as table name in RoR

-bash-3.1# ruby script/generate model Text
./script/../config/../config/environment.rb:8: warning: already initialized constant RAILS_GEM_VERSION
The name ‘Text’ is reserved by Ruby on Rails.
Please choose an alternative and run this generator again.
Suggestions:

Sense 1
text, textual matter — (the words of something written; “there were more than a thousand words of text”; “they handed out the printed text of the mayor’s speech”; “he wants to reconstruct the original text”)
=> matter — (written works (especially in books or magazines); “he always took some reading matter with him on the plane”)

WTF?!

10/1/06

interesting line

Life is a sexually-transmitted disease with a 100% fatality rate.

9/30/06

CentOS xen guest domain ssh problem

When I tried to ssh to a new CentOS guest domain, it said:
“Server refused to allocate pty”

It's not kernel problem because the kernel has pty support compiled in
The problem is lack of mountpoint defined in fstab. Just add this line to /etc/fstab:
none /dev/pts devpts gid=5,mode=620 0 0

Problem solved.

9/29/06

CNNIC

CNNIC doesn't support Firefox

6/29/06

"calculate 24" python implementation

“Calculate 24″ is one of my childhood game. It was played like this: 2 kids pull 4 cards from a stack of playing cards and try to get the number 24 out of them,he who calculate correctly first wins.

The rules are:
1, Each card must be used once and only once.
2, One can only use addition, subtraction, multiplication, and division.
3, the intermediate result must be integer.

For example: 1 3 3 10 -> (10+1-3)*3=24

The 3rd rule are often violated by almost all other implementations out there on the web, which give solution like (5-1/5)*5=24 or 6/(1-3/4)=24. It's good for generate brain teasers, but it's not valid. And these algorithms (often use floating point calculation) that ignoring 3rd rule are actually much easier to program.

Here is the program.

Some note:
Function bags() gets the bags of a list, in each bag, the list is divided into 2 parts. for example: bags([1,2,3,4] will give us:

[[1],[2,3,4]], [[1,2],[3,4]], [[1,2,3],[4]]

calrec() is the recursive engine that do the calculation on a list. From each bags of a list, it perform + - * / operation on the 2 parts. Each part will also call calrec() on itself. In the end, we get all combinations of calculations on a list.

calrec() only calculate on an ordered list. We need to do this on all permutation of the list. The function permu() gets the permutations of a list without redundant items, see the previous post.

We call permu() in calc(), where we call calrec() on each permutation. Whenever we see result 24, we return.

The program can not only be used on 4 numbers, it can take any numbers (>1). For example:
# python c24.py 3 8
3*8
0.000813961029053
# python c24.py 5 5 5 5 5
((5*(5*5))-5)/5
1.08426403999

Of course, with growing numbers, the execution time grow exponentially.

The program can also be easily called on any expected number, not just 24.

(This blog is moved here from my old blog)

3/3/06

How to create images from Chinese text using PIL

Here's how to create images from Chinese text using PIL (python image library).

We need Chinese fonts. The fonts in Linux doesn't work. For example, pilfont.py can't convert the font file 'fangsongti24.pcf' located in my '/usr/share/fonts/bitmap-fonts/' directory. Fortunately we can use truetype fonts in Windows. Get a chinese font (I chose simsun.ttc) from Windows's 'windows/fonts' directory, and copy it to linux.
To draw Chinese text correctly into image file, use unicode. The following is an example:
#-*- coding:utf8 -*-
import sys
import Image
import ImageDraw
import ImageFont

txt = '你好,世界!'

font = ImageFont.truetype('simsun.ttc',24)

im = Image.new("RGBA",(300,200),(0,0,0))

draw = ImageDraw.Draw(im)

#draw.text( (0,50), u'你好,世界!', font=font)
draw.text( (0,50), unicode(txt,'UTF-8'), font=font)

del draw

im.save(sys.stdout, "PNG")

That's it.