September 6, 2011

Largest palindrome made from the product of two 3-digit numbers

This question is an entry level puzzle from Project Euler.

It is very interesting because it has far more pitfalls than it first appears to be. There are some helpful discussions to pick up the whole scenario of this puzzle: 1) and 2). Basically speaking, the first post listed the most straightforward thoughts, which is iterations two numbers both from 100 to 1000 and finding the maximum of the outcomes. While I wrote the algorithm, this is also the first idea came into mind while I added several optimizations on it.
1) Decrease one number 1 per step, and iterate the other one from 999 to 100.
2) Stop the step whenever it finds a palindrome and march to the next step by decrease the benchmark number by one.
3) Meanwhile, increase the lower bound of iterator from 100 to whenever it finds a palindrome. This will exponentially decrease the searching range. e.g. (A--benchmark number, B--iterator) if A*B==palindrome, the searching range for the next step would be [A-1, B]. Because if both numbers less than the previous finding, it must not be the LARGEST palindrome we would like to find.
4) The second stop criteria is the iterator equal or less than the lower bound mentioned in the last item.

The major pitfall that I did see quite a few people mentioned is alternative decrease two numbers from 999, which the result comes to be 834*836=698896. The difference for two numbers are obviously unnecessary to be less or equal to one.

The second reference provides the basic optimization from math point of view. All of the palindrome can be divided by 11, so...
P=100000x+10000y+1000z+100z+10y+x
P=100001x+10010y+1100z
P=11*(9091x+910y+100z)
Then, the question is simplified to find two 3-digit numbers, at least one of which should be divided by 11. Similarly as the previous method, if A can be divided by 11, decreasing B by one per time; and if A cannot divided by 11, decreasing B by 11 per time.

My solutions for the first 5 puzzles are here.
cheers,

July 25, 2011

Editing hex file via Vim

It took me some time to find useful tools comparing hex file in mac. I had tried DiffMerge, kdiff3, HexEditor, and several other apps whose names I could not recall. At last, the most viable way is still using the vim editor. But the problem was still there: it was painful to look at hex code in that editor.

Solution:
Adding some configuration in the ~/.vimrc will make everything peace and clean:)


" vim -b : edit binary using xxd-format!
augroup Binary
au!
au BufReadPre *.bin let &bin=1
au BufReadPost *.bin if &bin | %!xxd
au BufReadPost *.bin set ft=xxd | endif
au BufWritePre *.bin if &bin | %!xxd -r
au BufWritePre *.bin endif
au BufWritePost *.bin if &bin | %!xxd
au BufWritePost *.bin set nomod | endif
augroup END


Note that this configuration only works with the file with .bin extension, which is already enough for me to hack into the targeting files:) Check the reference #1 for further reading of more general hex file editing.

cheers:)

References:
1) Improve hex editing
2) Vim commands cheat sheet

July 23, 2011

Some general programming methodologies

"MIT approach" with CLOS and Scheme:
1) Simplicity-the design must be simple, both in implementation and interface. It is more important for the interface to be simple than the implementation.
2) Correctness-the design must be correct in all observable aspects. Incorrectness is simply not allowed.
3) Consistency-the design must not be inconsistent. A design is allowed to be slightly less simple and less complete to avoid inconsistency. Consistency is as important as correctness.
4) Completeness-the design must cover as many important situations as is practical. All reasonably expected cases must be covered. Simplicity is not allowed to overly reduce completeness.

"Worse-is-better" philosophy is only slightly different:
1) Simplicity-the design must be simple, both in implementation and interface. It is more important for the implementation to be simple than the interface. Simplicity is the most important consideration in a design.
2) Correctness-the design must be correct in all observable aspects. It is slightly better to be simple than correct.
3) Consistency-the design must not be overly inconsistent. Consistency can be sacrificed for simplicity in some cases, but it is better to drop those parts of the design that deal with less common circumstances than to introduce either implementational complexity or inconsistency.
4) Completeness-the design must cover as many important situations as is practical. All reasonably expected cases should be covered. Completeness can be sacrificed in favor of any other quality. In fact, completeness must sacrificed whenever implementation simplicity is jeopardized. Consistency can be sacrificed to achieve completeness if simplicity is retained; especially worthless is consistency of interface.

Extreme Programming (XP) methodology:
1) Make samll, but freguent, release.
2) Develop in iteration cycles.
3) Do not put in anything that is not in the spec (no matter how tempted you are to put in functionarlity "for the future").
4) Writhe the test code FIRST.
5) No killer schedules; work regular hours.
6) Refactor (improve the code) whenever and wherever you notice the opportunity.
7) Do not release anything until it passes all the tests.
8) Set realistic schedules, based around small releases.
9) Keep it simple.
10) Program in pairs, and move people around so that everybody knows pretty much everything about the code.

Reference:
1) Wikipedia--Extreme Programming
2) The Rise of "Worse is Better"

July 11, 2011

Install Django and MySQLdb along with Python

Most of the steps are straightforward. The only twisted place is the combination of mysql-python and the mysql itself. During my installation, there are two errors coming out:

1) "EnvironmentError: mysql_config not found" happened at the 1st place when I install mysql-python using the python install. The solution is to edit the file "setup_posix.py":
# mysql_config.path = "mysql_config"
mysql_config.path = "/usr/local/YOUR_LATEST_MYSQL_DIRECTORY/bin/mysql_config"
--Then, everything in the installation should be fine.


2) As I check the status of installation, I found some confusing info. popping up.
>>> import MySQLdb
Traceback (most recent call last):
  File "build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py", line 19, in
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/creasywuqiong/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/creasywuqiong/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
  Reason: image not found
>>>

The solution is to add some symbolic links:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

Everything is fine:)
cheers~

References:
1) 2) 3)

June 13, 2011

Software used in mac os

Firefox:) --(update: now, this is really a pain in my ass.. It just has no response now and then, on matter whether in snow leopard or lion. I am wondering to switch to chrome now.)

eReading: Skim (.pdf), iChm (.chm), DjView (.djvu)
Coding: Xcode (objective-C), Vim (all of the others)
Word processing: iWork, Office for Mac
Terminal: iTerm2, Terminal.app
Unzip: Zipeg
Media player: MplayerX
IM: Skype, MSN
Google reader app: Reeder.
Sync Notes: Evernote.

Besides, there is something highly recommended by other people:
QuickSilver
IM: Adium

A better bash prompt on Mac OS

May 18, 2011

Install BeautifulSoup in Python

BeautifulSoup is a great error-tolerant HTML parser for Python. The install is rather straightforward:

1. Download the source code from the official website.
2. Unzip it in the working directory of Python "/usr/local/lib/python2.x/site-packages", in which the python2.x is the current using version. (actually... it is OK to install it in anywhere in convenience)
3. In the directory above, input: "~$ python setup.py install".
4. Completed. Check the current version of BeautifulSoup via:
~$ python
>>> import BeautifulSoup
>>> print BeautifulSoup.__version__
3.2.0 (Yeah~!)
5. Cheers.

Days of our lives

Daisypath Anniversary tickers