Skip to main content


Showing posts from 2014

First post using stackedit

Welcome to StackEdit! Hey! I’m your first Markdown document in StackEdit1. Don’t delete me, I’m very helpful! I can be recovered anyway in the Utils tab of the Settings dialog.
Documents StackEdit stores your documents in your browser, which means all your documents are automatically saved locally and are accessible offline!
StackEdit is accessible offline after the application has been loaded for the first time.Your local documents are not shared between different browsers or computers.Clearing your browser’s data may delete all your local documents! Make sure your documents are synchronized with Google Drive or Dropbox (check out the Synchronization section). Create a document The document panel is accessible using the button in the navigation bar. You can create a new document by clicking New document in the document panel.
Switch to another document All your local documents are listed in the document panel. You can switch from one to another by clicking a document in the li…

How to check if a linked list is cyclic or not

It is a regular interview question: given a one - direction linked list, how to check if there is a cycle inside the list or not.

As usual, you will receive a pointer to the head of the list, and you write a function to return a Boolean value which indicates there is a cycle inside the list or not.

The obvious way is creating another list / array to store all the elements of the list you visited, but in some cases it is not possible.

You can always use this approach, even in the case the node element has no data but a pointer to the next one. If so, you can just store the pointer (i.e, the address).

But of course it is not the intention of the interview. If the interviewer asked you to write an algorithm with time complexity is O(n) or space complexity is O(1), the above approach is not suitable.

There is a better algorithm: using two pointers, one fast and one slow. Slow pointer moves one element per iteration, while fast pointer moves two. If they meet, it means there is a cycle, a…

Finding a room of the princess

There is an interview question as following:

There are 17 rooms line up from 1 to 17. There is a princess who start at a room, then every night she will move to the room next to the previous one (i.e, she has to move to the left or right room - she cannot say at the same room).

You are a beast who has to find the princess to be back a prince. Every night, you can open one and only one room to check if the princess is inside or not. You have 30 nights to find the princess, otherwise ... boom!

Below is the number of the room the beast (or the future prince) should follow to check during 30 nights:

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2

Explaining why it is a good strategy might be an interesting task for the reader.

I wrote a small application to simulate this strategy at:

Probably you cannot run the simulation online, but you can get the Python code and try in your computer.

What doors are closed?

There is an interesting interview question, which has been asked at many US companies.
There are 500 doors, initially closed. There are 500 people, one by one goes through all the doors.
The 1st person change the status of all the doors (i.e, open all of them). The 2nd person change the status of all the doors with number can divide to 2 (i.e, he will change the status of doors number 2, 4, 6, 8, 10, ... 500) The 3rd person change the status of doors 3, 6, 9, ... ... The 500th person change the status of the door 500
Change the status means: close the opening door, and open the closing door.
The question is: in the end, how many doors are close?
You can solve this problem by using mathematics, but it may be faster to simulate it.
You can check my simulation at: and easy to see, the opening doors are the doors with the number are square number.

Monty Hall problem: simulation with Python

You probably know about Monty Hall paradox. If not, you can read more about it at:

But if you do not feel happy with some probability calculation, you may want to see should we really change the door.

I wrote a small Python application to simulate Monty Hall problem.

You can access the program at:

You can see, after running 100 000 times, the change to win if you switch the door is approximately 2/3:

So now you can trust probability calculation.

Getting started with Cryptpad in Ubuntu: step by step

Cryptpad is an open source collaborative editor which is hosted at:

It is easy to clone the github repository and start to try, but if you are a newbie, there maybe some difficulties.

Suppose that you have a clean Ubuntu machine, and want to try with Cryptpad, you can follow these steps:

1. Download mongodb for Linux:

2. Unzip the file you got to a location you want. You will start mongodb from there, or add this directory to your PATH variable so you can start mongodb from anywhere.

3. Suppose that you chose the easier way, i.e start mongodb from its directory.

4. Open Terminal (Ctrl + Alt + T for shortcut), move to the directory of mongodb

5. Type:
mkdir db
mongod --dbpath=./db

These above commands will first, create a directory 'db' insider the directory mongodb, then start mongodb server.

6. Keep the terminal with mongodb server running

7. Open another terminal (Ctrl + Shift + T to open a new tab, lik…

How to install NodeJS in Ubuntu

NodeJS provided installation file for MS Windows and Mac OS, but maybe you will not find a similar package for Ubuntu.

There are some instructions on the Internet which guide you installed NodeJS by using default repository of Ubuntu, or add more PPA, but probably you will receive an older version of NodeJS. (For instance, today I tried the instruction as: but I only got NodeJS 0.10.25, when the latest one is 0.10.33)

Actually, installing NodeJS in Ubuntu is very easy, maybe easiest among other operating systems.

1. Go to:

2. Depends on your operating system, download Linux Binaries file 32 - or 64 - bit.

3. Unzip the file you downloaded

tar xzvf node-v0.10.33-darwin-x64.tar.gz

4. Modify the file ~/.bashrc by adding the following line to the end of the file:

export PATH=$PATH:/path/to/bin/directory/of/node/you/downloaded

5. Restart terminal, or reload the bash…

How to auto indent in vim

For many years, vim still be one of the most common editor for developers.

There are (too) many shortcuts and commands for vim to remember, so sometimes it is quite difficult.

For auto indent, one feature I feel very important, especially when you copied the code from somewhere:


1G: move to the first line
=: auto indent
G: go to end of file

Getting started with source code of the book: Haskell the craft of functional programming in Ubuntu

If you are following the book
you probably want to download and try the source code than typing everything from scratch.

 To use the source code provided by the author, first of all you need to install QuickCheck:

cabal install quickcheck


cabal unpack Craft3e

If cabal cannot find the appropriate package, you need to update cabal:

cabal update

How to use diffent keyboard than English in Ubuntu Server 14.04 (or 14.10) with Gnome Core

Even in my laptop, I still use Ubuntu Server because it seems more stable than Ubuntu Desktop.

At the first time of using Ubuntu Server, you may have some issues, especially if you are not familiar with Linux command, because there is no GUI in Ubuntu Server by default.

There are some ways to install GUI to Ubuntu Server.

I am choosing Gnome Core.

To install Gnome Core:

sudo apt-get install xorg gnome-core gnome-system-tools gnome-app-install
If you have used Ubuntu Desktop before (with Unity), you may find something different in Gnome Core.

1. The notification bar now in on the bottom but not top.
 To see the notification bar, move your mouse to the bottom of the screen.

2. There is no Text Entry anymore, so if you are not native English speaker like me and want to install another typing application such as ibus to type in your own language, it may be pain.

In Gnome Core, to configure your typing, press Windows button (if there is this key in your keyboard), or click to Activities at…

Computer Science Programming Basics in Ruby

Get this book from Amazon (recommend)
Product DetailsPaperback: 188 pagesPublisher: O'Reilly Media; 1 edition (May 1, 2013)Language: EnglishISBN-10: 1449355978ISBN-13: 978-1449355975Product Dimensions: 7 x 0.4 x 9.2 inches Editorial Reviews Review Author Ophir Frieder offers insights on his latest book, "Computer Science Programming Basics in Ruby" "Computer Science Programming Basics in Ruby" is timely as many of the world's web sites and applications are built with a framework called Ruby on Rails. The core of that framework is a programming language called Ruby. It turns out that Ruby is an exceptional language with which to teach introductory computer science topics. Hence, a rare combination of thing-that-is-useful, along with thing-that-is-good-to-teach has occurred. Our book is the first to really treat Ruby as a solid introductory programming language with which to teach core computer science topics. Ruby's simplicity in demonstrating…

How to cut and paste in Mac OS

If you switch to Mac OS from MS Windows or even Linux distro, the lack of cut feature may be annoy.

You can also install Total Finder for Mac OS, to add some features into Finder, but I always prefer to use the default one.


To cut and paste (move the file) in Finder of Mac OS, you can:
- Select file(s).
- Command + C
- Move to destination directory
- Option + Command + V


Reference books for edX CS50X: Introduction to Computer Science, Havard University (Part 2): Programming in C (update to 4th edition)

Get this book from Amazon (recommend)

Programming in C, Fourth Edition is a newly revised and updated edition of Steven Kochan's classic C programming tutorial — a book that has helped thousands of students master C over the past 25 years. This edition fully reflects the latest developments in the C programming  and has been crafted to help students master C regardless of the platform they intend to use or the applications they intend to create—including mobile and gaming-device applications — where C's elegance and speed make it especially valuable. Kochan begins with the fundamentals, then covers every facet of C programming: variables, data types, arithmetic expressions, program looping, making decisions, arrays, functions, structures, character strings, pointers, operations on bits, the preprocessors, I/O, and more. Coverage also includes chapters on working with larger programs; debugging programs, and the fundamentals of object-oriented programming. - See more at: http://…

How to start writing LaTex in Ubuntu

It is very easy:

1. Install TexLive:
sudo apt-get install texlive-full
(it will take time)

2. Install TexMaker (there are many LaTex editors, but TexMaker is a very good one, easy to use, and cross platform so you can easily switch between operating systems):
You can download TexMaker from this website:

(select the version of your Ubuntu)

TexMaker will take about 1.1GB after installed, so prepare disk for it.

3. That's it. Start TexMaker.

4. By default, when click Quick Build, TexMaker will generate PDF file. For many cases, it is good enough and you do not have to modify anything.

Python for Data Analysis

Publisher: O'Reilly Media Released: October 2012 Pages: 470
Python for Data Analysis is concerned with the nuts and bolts of manipulating, processing, cleaning, and crunching data in Python. It is also a practical, modern introduction to scientific computing in Python, tailored for data-intensive applications. This is a book about the parts of the Python language and libraries you’ll need to effectively solve a broad set of data analysis problems. This book is not an exposition on analytical methods using Python as the implementation language. Written by Wes McKinney, the main author of the pandas library, this hands-on book is packed with practical cases studies. It’s ideal for analysts new to Python and for Python programmers new to scientific computing. Use the IPython interactive shell as your primary development environmentLearn basic and advanced NumPy (Numerical Python) featuresGet started with data analysis tools in the pandas libraryUse high-performance tools to load, clea…

More iOS 6 Development: Further Explorations of the iOS SDK

Book DescriptionPublication Date:December 27, 2013 Interested in iPhone and iPad apps development? Want to learn more? Whether you’re a self-taught iPhone and iPad apps development genius or have just made your way through the pages of Beginning iOS 6 Development, we have the perfect book for you.

More iOS 6 Development: Further Explorations of the iOS SDK digs deeper into Apple’s latest iOS 6 SDK. Bestselling authors Dave Mark, Alex Horovitz, Kevin Kim and Jeff LaMarche explain concepts as only they can—covering topics like Core Data, peer-to-peer networking using GameKit and network streams, working with data from the web, MapKit, in-application e-mail, and more. All the concepts and APIs are clearly presented with code snippets you can customize and use, as you like, in your own apps.

If you are going to write a professional iPhone or iPad app, you’ll want to get your arms around Core Data, and there’s no better place to do so than in the pages of this book. The book continues right…

Reference books for edX CS50X: Introduction to Computer Science, Havard University (Part 4): Hacker's Delight (2nd Edition)

Get this book from Amazon (strong encouragement)
DOWNLOAD (EPUB Version, 45 MB) Mediafire: Link or Link Prefiles: Link or Link
Book DescriptionPublication Date: October 5, 2012| ISBN-10: 0321842685 | ISBN-13: 978-0321842688| Edition: 2 In Hacker’s Delight, Second Edition, Hank Warren once again compiles an irresistible collection of programming hacks: timesaving techniques, algorithms, and tricks that help programmers build more elegant and efficient software, while also gaining deeper insights into their craft. Warren’s hacks are eminently practical, but they’re also intrinsically interesting, and sometimes unexpected, much like the solution to a great puzzle. They are, in a word, a delight to any programmer who is excited by the opportunity to improve.

Extensive additions in this edition include
A new chapter on cyclic redundancy checking (CRC), including routines for the commonly used CRC-32 codeA new chapter on error correcting codes (ECC), including routines for the Hamming codeMor…

Reference books for edX CS50X: Introduction to Computer Science, Havard University (Part 3): How Computers Work

Buy from Amazon (Recommended)
PDF Version (193 MB) Mediafire: Link or Link Prefiles: Link or Link

Book DescriptionPublication Date: November 24, 2007| ISBN-10: 0789736136 | ISBN-13: 978-0789736130| Edition: 9 Having sold more than 2 million copies over its lifetime, How Computers Work is the definitive illustrated guide to the world of PCs and technology. In this new edition, you’ll find detailed information not just about every last component of hardware found inside your PC, but also in-depth explanations about home networking, the Internet, PC security, and even how cell phone networks operate. Whether you’re interested in how the latest graphics cards power today’s most demanding games or how a digital camera turns light into data, you’ll find your answers right here.

Ron White is a former executive editor and columnist for PC Computing, where he developed the visual concept behind How Computers Work. Founder of one of the
earliest PC user groups, he has been writing abou…

Reference books for edX CS50X: Introduction to Computer Science, Havard University (Part 2): Programming in C

These books are uploaded for people who cannot buy the books (such as many students in poor countries, without access to international purchase system such as Visa card or Paypal).
If you can, please buy books.

2. Programming in C, 3rd edition, 2004

Buy from Amazon (recommended) Book DescriptionPublication Date: July 18, 2004| ISBN-10: 0672326663 | ISBN-13: 978-0672326660| Edition: 3 Learn the C programming language from one of the best. Stephen Kochan's Programming in C is thorough with easy-to-follow instructions that are sure to benefit beginning programmers. This book provides readers with practical examples of how the C programming language can be used with small, fast programs, similar to the programming used by large game developers such as Nintendo. If you want a one-stop-source for C programming, this book is it.The book is appropriate for all introductory-to-intermediate courses on programming in the C language, including courses covering C programming for games and …

Reference books for edX CS50X: Introduction to Computer Science, Havard University (Part 1): C Programming Absolute Beginner's Guide

These books are uploaded for people who cannot buy the books (such as many students in poor countries, without access to international purchase system such as Visa card or Paypal).
If you can, please buy books.

C Programming Absolute Beginner's Guide (3rd Edition) The course indicated the second edition, but it seems out of date (published in 1994). I uploaded the latest version (3rd edition, 2013)

Buy from Amazon (recommended)
Book DescriptionPublication Date: August 17, 2013| ISBN-10: 0789751984 | ISBN-13: 978-0789751980| Edition: 3 Updated for C11

Write powerful C programs…without becoming a technical expert! This book is the fastest way to get comfortable with C, one incredibly clear and easy step at a time. You’ll learn all the basics: how to organize programs, store and display data, work with variables, operators, I/O, pointers, arrays, functions, and much more. C programming has neverbeen this simple!

Who knew how simple C programming could be?

This is today’s best be…