Skip to main content

Posts

First post using stackedit

Welcome to StackEdit! Hey! I’m your first Markdown document in StackEdit 1 . 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! Note: 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 anoth...

First post using stackedit

Welcome to StackEdit! Hey! I’m your first Markdown document in StackEdit 1 . 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! Note: 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 anoth...

Install latest R (3.2.5) on Debian Jessie

Installing R on Ubuntu/Debian is quite painful, especially when you want to install the latest version. I have no clue why this situation stills for a long time, but this is a way to work around. On terminal: ```bash Appends the CRAN repository to your sources.list file You could replace jessie-cran3 by the newer one Find the correct value at https://cloud.r-project.org/ sudo sh -c 'echo "deb http://cran.rstudio.com/bin/linux/debian jessie-cran3/" >> /etc/apt/sources.list' Adds the CRAN GPG key, which is used to sign the R packages for security. sudo apt-key adv --keyserver subkeys.pgp.net --recv-key 381BA480 sudo apt-get update sudo apt-get install r-base r-base-dev ``` Installing h2o : ```bash to retrieve curl-config otherwise you may have a problem with RCurl sudo apt-get install libcurl4-gnutls-dev ``` R install.packages ("h2o")

Using markdown with blogspot

How to use markdown with blogspot Originally, BlogSpot does not support markdown very well. It makes its disadvantage compared with new services such as github.io. However, we still can write markdown then post it to blogspot with online tools. Markdown editor I am using http://dillinger.io/ but there are other services. Convert Markdown code to HTML code Dillinger can export Markdown code to HTML code, but we have to download a new generated HTML file. I do not like it so much, so there is a website http://daringfireball.net/projects/markdown/dingus so we can convert and get HTML code directly, then just copy and paste it to Blogspot.

Installing tensorflow on Mac OS 10.11

Tensorflow (https://www.tensorflow.org/) is the open source deep learning library from Google (http://www.wired.com/2015/11/google-open-sources-its-artificial-intelligence-engine/). Installing Tensorflow is not easy on Mac if you follow exactly the installation instruction on the homepage. I have no idea why. I have error then error while trying to install Tensorflow with pip, with virtualenv, or even with Docker. However, with Anaconda (https://www.continuum.io/downloads), it will be easy. The trick is you should download the wheel file and install it offline other than retrieving it online as suggested by Tensorflow homepage. So, with anaconda for python2 installed, I did: # create a new environment with sklearn installed, up to you # if you want a pure Python, replace scikit-learn by python conda create -n tensorflow scikit-learn # activate the new environment source activate tensorflow # download the wheel file curl https://storage.googleapis.com/tensorflo...

Testing the difference between two correlations in R

Introduction Correlation is the measurement to measure how two datasets increase or decrease together. It's a real number range from -1 (i.e, when one dataset increases, another one decreases and vice versa) to +1 (i.e, these two datasets increase or decrease together). Value 0 means there is no relationship between two datasets. Calculating correlation Calculating correlation of two datasets in R is quite straightforward: In R: > x = c (1,5,2,6,7) > y = c (2,4,3,9,-5) > cor(x,y) [1] -0.1459338 Usually, a single value does not say much in statistics. You need a confidence level. > cor.test (x, y) Pearson's product-moment correlation data:  x and y t = -0.2555, df = 3, p-value = 0.8149 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval:  -0.9109173  0.8451475 sample estimates:        cor  -0.1459338  The function 'cor.test' will gives you the correlation, ...

An interesting card game

Here is a very interesting card game which has been introduced by Prof. Kandori at University of Tokyo. There are two players, one is Red and one is Black. Each players have four cards in corresponding colors: King (K), Ace (A), 2 and 3. Each round, both two players simultaneously (i.e, together) select one card from their hand, and determine the winner as the rule in the picture: - If two cards are two Kings, Red wins. - If one card is King and other card is not, Black wins. - Otherwise, if number of two cards are different, Red wins. If number of two cards are identical, Black wins. At first sight, it is very easy to say that the game is favor on Red. But it is not true. Even if both players play randomly, the Black has a bigger chance to win. You can check the simulation at: http://www.codeskulptor.org/#user39_B0DWPHGSyD_1.py It is easy to calculate the chance to win of Black is 9/16. But we can go further. Take into account the fact that K is very important ...

Generating a random number between 1 and 6 using only a function which generate 0 or 1

Suppose you have a function toss, which returns 0 or 1 randomly. Now you need to write a function which uses toss () to generate a random integer number between 1 and 6. The idea is: you toss 3 times, and if the value is outside of first six values, you toss 3 times again. The Python code is following: #suppose we have a function called "toss" which return 0 and 1 randomly #we need to write a function to generate number between 1 and 6 #means 1 <= x <= 6 use only the function "toss" from random import randint def toss (): return randint (0, 1) def generateRandomBetween1And6 (): while (True): sum = 0 for i in range (3): sum += toss () * (2 ** i) if (sum >= 1 and sum <= 6): return sum #test res = [0] * 6 for i in range (10000): num = generateRandomBetween1And6 (); res [num - 1] += 1 for i in range (6): print(str(res[i]) + '\n')

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 i...

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: http://www.codeskulptor.org/#user38_wKWpYR0yZ3_2.py 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: http://www.codeskulptor.org/#user38_rrZv4nVRno_1.py 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: http://en.wikipedia.org/wiki/Monty_Hall_problem 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: http://www.codeskulptor.org/#user38_hxSeBAZW5H_1.py 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: https://github.com/cjdelisle/cryptpad 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: https://www.mongodb.org/downloads 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...

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: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-an-ubuntu-14-04-server 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: http://nodejs.org/download/ 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 term...

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=G 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 Then 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),...

Computer Science Programming Basics in Ruby

Get this book from Amazon (recommend) Product Details Paperback:  188 pages Publisher:  O'Reilly Media; 1 edition (May 1, 2013) Language:  English ISBN-10:  1449355978 ISBN-13:  978-1449355975 Product Dimensions:  7 x 0.4 x 9.2 inches Editorial Reviews Amazon.com 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 sci...

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. Actually, 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 Voila!

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:...