Tuesday, September 8, 2015
Very Good Leetcode Summary Reference
Please find below link of very good summary of Leetcode.
http://leetcode.tanglei.name
Thursday, August 27, 2015
How to achieve splitting equally to N number of people using a coin
What do I mean to split equally to N number of people using a coin? For example, we have a group of six people. Suppose we need a coin to decide who's going to buy the group a round of drink and we have to make this decision unbiased. All we have is a quarter. What can we do about this coin? We can't make this coin into a dice.. :(
Analysis:
We start to notice that flip a coin once makes each 1/2 equally.
If we define flip twice and define both head, head first and then tail, tail first and then head, and both tails as four different outcomes then we have 1/4 (1/2 * 1/2).
Same applies when we flip three times, which give 1/8 (1/2 * 1/2 * 1/2).
Here's the solution:
Suppose head represents 0 and tail is 1 when we flip the coin.
Flip 3 times of coin
Denote each time's result as 0 or 1.
000 – 3 heads
001 – head, head, tail
010 – head, tail, head
011 – head, tail, tail
100 – tail, head, head
101 – tail, head, tail
110 – tail, tail, head
111 – 3 tails
There are eight of them all with 1/8 possibility. We can simply use 6 of them and discard the rest. If the group of people is beyond 8 people then we can simply add one more flip to increase the range up to 16 (1/2 * 1/2 * 1/2 * 1/2).
Analysis:
We start to notice that flip a coin once makes each 1/2 equally.
If we define flip twice and define both head, head first and then tail, tail first and then head, and both tails as four different outcomes then we have 1/4 (1/2 * 1/2).
Same applies when we flip three times, which give 1/8 (1/2 * 1/2 * 1/2).
Here's the solution:
Suppose head represents 0 and tail is 1 when we flip the coin.
Flip 3 times of coin
Denote each time's result as 0 or 1.
000 – 3 heads
001 – head, head, tail
010 – head, tail, head
011 – head, tail, tail
100 – tail, head, head
101 – tail, head, tail
110 – tail, tail, head
111 – 3 tails
There are eight of them all with 1/8 possibility. We can simply use 6 of them and discard the rest. If the group of people is beyond 8 people then we can simply add one more flip to increase the range up to 16 (1/2 * 1/2 * 1/2 * 1/2).
Thursday, September 18, 2014
Java - Hash Tables
A simple hash function:
h(k)=k mod N.
If a hash function is chosen well, it should guarantee that the probability that two different keys get hashed to the same bucket is at most 1/N. Therefore, instead, the below hash function is more appropriate:
h(k)= (ak+b) mod N.
Android Development using Python in Linux (Ubuntu)
There are a few steps:
Prerequisite:
$ sudo apt-get install python-setuptools python-pygame python-opengl python-gst0.10 python-enchant gstreamer0.10-plugins-good cython python-dev build-essential libgl1-mesa-dev libgles2-mesa-dev
$ python
>>> import pkg_resources
>>> print pkg_resources.resource_filename('kivy', '../share/kivy-examples')
# Copy the address
>>> exit()
$ ln -s address
# Check the kivy examples
$ cd kivy-examples
$ cd demo/touchtracer
$ python main.py
Build the distribution
./distribute.sh -m "kivy"
The file "distribute.sh" can be found at /home/bz/python-for-android
Prerequisite:
$ sudo apt-get install python-setuptools python-pygame python-opengl python-gst0.10 python-enchant gstreamer0.10-plugins-good cython python-dev build-essential libgl1-mesa-dev libgles2-mesa-dev
$ python
>>> import pkg_resources
>>> print pkg_resources.resource_filename('kivy', '../share/kivy-examples')
# Copy the address
>>> exit()
$ ln -s address
# Check the kivy examples
$ cd kivy-examples
$ cd demo/touchtracer
$ python main.py
Build the distribution
./distribute.sh -m "kivy"
The file "distribute.sh" can be found at /home/bz/python-for-android
Linux for dummies like myself
You will be amazed about how incredible the materials out there from which we can learn. Below is some:
Ryan's Tutorial has this amazing Linux basic tutorial:
http://ryanstutorials.net/linuxtutorial/
I have learned a lot about bash command in Linux system from Ryan's Tutorial.
I found the book "Linux All-in-One For Dummies" very informative about Linux system you may need. More importantly, you can get it free from IT ebooks for free.
It's quite different to issue all kinds of commands in the shell in Linux system. Although it seems hard to get on your hand at the first sight, you may be quite familiar with how things work after you get to know some basic commands and get your hand wet in this.
grep can be used to search this keyword in a file/log in Linux. It's very handy feature to search. The system will provide the search result in no time. It's suitable to extract the specific thing you can looking for in some large files. e.g. grep "keyword" /destination/location. Another example: scan_r |grep delay; the command is also known as a combined command.
cat displays the content of a file on the command screen. e.g. cat /home/file
at any stage, if you feel like to issue the command in more than one line, and would like to break a line into a new line, you can use backslash \.
e.g. ls /etc; cat \
file |grep name
Asterisk (*): Matches zero or more characters in a filename. That means* denotes all files in a directory.
Question mark (?): Matches any single character. If you type test?, thatmatches any five-character text that begins with test.
Set of characters in brackets: Matches any single character from that set. The string [aB], for example, matches only files named a or B, The string [aB]*, though, matches any filename that starts with a or B.
cp can copy a file/directory from one location to another. e.g. cp -avr /source/location /dest/location
Two takeout note for new learner like me:
1. up-arrow is your best companion... it can bring up all history command that's ever issued in that command window. It will become handy when you ask somebody to perform some tasks and you can review and learn the command afterwards. Also, Tab button saves a lot of time to type something in full... double press Tab can bring up any files that match your current partial input if there's any.
2. avoid using space in file name. If you do use a space in a filename, you have to use backslash \ before each space. If you want to copy the file it would also cause problem... If you have to deal with tons of files, say PNG screenshots, that contains space in it, just tar (compress approach) everything would do the trick.
Sunday, July 21, 2013
Install Spotify in Ubuntu 13.04
How to Install Spotify in Ubuntu 13.04
1. Open a terminal window.
2. Execute each bullet in the terminal.
- sudo add-apt-repository “deb http://repository.spotify.com stable non-free”
- sudo apt-get update
- sudo apt-get install spotify-client-qt
Done!
Saturday, June 8, 2013
Java - Thread
Java offers thread manipulation that allows even single core computer do multitasking, which is also known as Multiprogramming, a way of parallelism.
A Java thread will always be in one of four different states:
A Java thread will always be in one of four different states:
- New
- Runnable
- Blocked
- Dead
"When designing a program that uses multiple threads, we must be careful not to allow an individual thread to monopolize the CPU." (Textbook)
The above mentioned could potentially lead to hanging of an application, which appears to be running but in fact, doing no computation at all. However, some OS can solve this problem by bring Round Robin as the mechanism to avoid such monopolization.
A linked list is a collection of nodes that form a linear ordering together.
A singly linked list:
The next reference inside a node can be viewed as a link/pointer to another node.
Moving from one node to another by following a next reference is known as link hopping/pointer hopping.
The first and last node of a linked list are called the head and tail of the list.
The tail contains a null reference, that indicates the ends of the list.
A singly linked list does not have a predetermined fixed size, and uses space proportional to the number of its elements.
Subscribe to:
Posts (Atom)