Archive

Archive for the ‘Daily Life’ Category

Format a string: remove redundant space characters

March 15th, 2010 NamPham 2 comments

Today, I came across one interesting question about string format:

“Format a string so that each word is separated each other by exactly one space. Also there is no preceding and trailing  spaces before the first and last word. Try to optimize it without using additional arrays, just some temporary variables.

For exampe:

“____________” -> “”

“ABCD” -> “ABCD”

“AB__CD” -> “AB_CD”

“______A__B______C_D______” -> “A_B_C_D”

My idea is using two indexes iterating through each character in a String. One index is just used to iterate each character. One index is keep track the length of format string.

Here is the sample code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//input: a String, convert it to array. 
//output: return formated String
        public String removeBlanks(String str){
		int strLength = str.length();
		char[] seq = new char[strLength+1];
		int length = -1;
		for (int i = 0; i < strLength; i++)
			if (str.charAt(i) != ' ' || (length >= 0 && seq[length] != ' '))
				seq[++length] = str.charAt(i);
 
		if (length == -1) return ""; // empty string
 
		if (seq[length] != ' ') 
			return new String(seq,0,length+1);
		else //remove trailing blank
			return new String(seq,0,length);
	}
Categories: Daily Life Tags:

How to compute the square root

March 12th, 2010 NamPham No comments

Hi,

I cam across the Wikipedia page talking about methods of computing square roots (here). Very interesting.

Here is a method that Babylonian used – so simple and clear to understand.

1. Given number n

2. Roughly approximate n1 ~ √n ( worst case n1 = n)

3. Repeat this step

n2 = n1;

n1 = (n2 + n/n2) / 2;

until |n1-n2| < eps

4. Return n1;

Read more…

Categories: Daily Life Tags:

Add “Create a new txt document” in New menu (Windows 7)

February 13th, 2010 NamPham No comments

I have been using Windows 7 for a while and have nice experience with it except two problems with Windows Explorer:

1. It does not automatically update the content folder when I created/deleted files.

2. It does not have “Create a new text document” in New menu when you right click in the folder space.

I followed the guide in How to Add / Remove Items from New Menu in Windows? :

1. Open regedit and expand “HKEY_CLASSES_ROOT” key.

2. Now look for the file type which you want to add in “New” menu, e.g. for adding MP3 file type look for .MP3 key.

3. Right-click on it and select “New -> Key” and give it name “ShellNew“.

4. In right-side pane, right-click and select “New -> String Value“. Give it name “NullFile” and press Enter.

5. Thats it. You’ll immediately get the file type entry in “New” menu.

But it does not work with my windows 7. I checked my registry it already has this kind of information.

I am looking further and found out that my favorite text editor (Notepad++) has some compatibility issue with Windows 7 :( .

Fortunately, I found the solution in Windows 7 forum. It still change the content of registry but with PersistentHandler handler of text file I believe.

Here is the reg file that fixes the problem:

?Download txt.reg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Windows Registry Editor Version 5.00
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.txt]
"PerceivedType"="text"
@="txtfile"
"Content Type"="text/plain"
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.txt\PersistentHandler]
@="{5e941d80-bf96-11cd-b579-08002b30bfeb}"
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.txt\ShellNew]
"ItemName"=hex(2):40,00,25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,\
  6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,\
  00,6e,00,6f,00,74,00,65,00,70,00,61,00,64,00,2e,00,65,00,78,00,65,00,2c,00,\
  2d,00,34,00,37,00,30,00,00,00
"NullFile"=""
Categories: Daily Life Tags: