February 13, 2013

Change timestamp for sudo password input

in terminal:

sudo visudo

find the following line

defaults env_reset

change into

Defaults env_reset , timestamp_timeout=xx 

xx: time
eg: 10 means 10mins and 30 means half hour.

ps: -1 means when you logout or shutdown, the terminal will remember your password, but for the security reason not recommend.

February 9, 2013

Unix Shell read file in line and parse it

Read file in line:
while read fname
do
  echo $fname
done < /etc/passwd
This snippet means printout file and the result of each row is like the following one:

: as separator, the 1st column is user name, the 6th column is home directory
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
If we want to printout the 1st & 6th column, we need use parser.

then do as follows:
while read fname
do
  echo "$fname"|cut -d: -f1,6
done < /etc/passwd
-d:define separator as :, -f1,6: designate the 1st & 6th column.

there is another efficient way -- using awkfilter
while read fname
do 
   echo $fname|awk -F:'{print $1,$6}'
done < /etc/passwd

February 7, 2013

JSON Example - String to Object

In order to convert very long string which is separated by common (,) to a JavaScript object and then use these data shown in the web page, we should do,

     1. Split the string into an array
     2. Create a JavaScript string containing JSON syntax
     3. For securer method, use JSON parser JSON.parse()
        instead of Javascript eval() function to parse the JSON text and produce a JavaScript object

Given String: Apple,89,Banana,39,Grape,58,Cheery,33

Printout: how many bananas in the supermarket

Click the button to display the object values after the split and conversion.

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to display the object values after the split and conversion.</p>

<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var str="Apple,89,Banana,39,Grape,58,Cheery,33";
var n=str.split(",");
var txt ="";
 
for(var i=0; i<n.length; i++)
{ txt += '{"Name":"'+ n[i]+'","Num":"' + n[i+1] + '"}';
  if(i!= n.length-2)
  txt += ',';
  i++;}

txt = '{"fruit" : [' + txt + ']}';

//var obj = eval("(" + txt + ")");

obj2 = JSON.parse(txt);

document.getElementById("demo").innerHTML="There are " + obj2.fruit[1].Num + " " + obj2.fruit[1].Name + " in the supermarket.";
}
</script>
</body>
</html>

February 6, 2013

Hash Table 3 -- Hashing

The idea of hashing is to distribute the entries (key/value pairs) across an array of buckets. 
Given a key (e) and an array--hashes with a size of arraySize, the algorithm-hash function (h) computes an entries to map the key to an index (i) between {0, arraySize-1}, which suggests where the entry can be found.

[1]
[1] Image from Wikipedia, A hash function that maps names to integers from 0 to 15.
There is a collision between keys "John Smith" and "Sandra Dee".

Given:

        e: key
        h: hash function
arraySize: the size of the array
        i: integer between {0, arraySize-1}

Then:

       i = h(e, arraySize)

Often above function is deduced from two steps:

hashCode = h(e)
       i = hashCode % arraySize

1.According to the hash function (h), we got the integer type hashCode derived from (e).
2.Then we got the index which is hashCode % arraySize, because the return number must fall in {0, array_size – 1}, by using a remainder operation (%).

Usually, the range of (e) values is much bigger than arraySize. We can image there might be a worse situation – for two different keys (e1) and (e2), if they have h(e1, arraySize) == h(e2, arraySize), then (e1) and (e2) are mapped into the same index of an array. This calls Collision.

Obviously, the most important part in this calculation is hashCode. The better hash function, the better hashCode is and the less collision we got. 
    Generally, a good hash function requires,
  • Easy to calculate, which means time complexity of hash function is Constant Time or Fixed Time.
  • Uniformly distribute keys to array to reduce the chance of collision.

Hash Table 2

A Hash Table can map key to value for highly efficient lookup. 
A Hash Table uses a  hash function to compute an index into an array of buckets or slots, from which the correct value can be found.
[1] [1] Image from Wikipedia, A small phone book as a hash table.
    The goal of Hash Table is,
  1. Keys are uniformly distributed in it, regardless of the sequence.
  2. It supports insert, remove and find operations.
  3. Each operation has better time complexity than O(log n).
Then in the next post, the method of Hashing will be introduced to use on the Hash Table.

February 4, 2013

ERRORS of using Runtime.getRuntime() and exec() in Eclipse

When I use Runtime.getRuntime().exec("java ArrayTest")to run a new java application, it failed and I got lots of errors.

Finally, I found the reasons and solutions:

Reasons:
JVM searches this ArrayTest.class under the bin directory,

then you should add -classpath bin to your exec().
Solutions:
  1. Runtime.getRuntime().exec("java -classpath D:\java\Practice\bin ArrayTest");
  2. Runtime.getRuntime().exec("java -classpath .\bin ArrayTest"); // root directory
  3. Runtime.getRuntime().exec("java -classpath bin ArrayTest");

January 29, 2013

Understanding BITMAP is so easy

1. Syntax

CREATE BITMAP INDEX index_name ON table

2. An simple example

TABLE test(id, name address){ (1,Rob,New York) (2,Mary,Orlando) (3,Jason,Chicago) (4,Lisa,New York) (5,Kim,New York) (6,Philip,Washington) (7,Frank,New York) (8,Elizabeth,New York) (9,Klaus,New York) ... }
if you want to find the result under the condition of 【Where address = 'New York'】,however, many rows meet the condition and then you won't get the benefits from the general index for efficient query. Here, you can create BITMAP INDEX:
New York Orlando Chicago Washington
1 0 0 0
0 1 0 0
0 0 1 0
1 0 0 0
1 0 0 0
0 0 0 1
1 0 0 0
1 0 0 0
1 0 0 0

In this case,  select * from TABLE where address = "New York"  will be quickly execute to find the resultset according to "1" and "0".