Tuesday, March 19, 2013

A Note on String, StringBuffer, StringBuilder

When String (an immutable [not modifiable] type) construction or modification involving dynamic values (whose values are not known at compile time) is required by a single thread, the mutable StringBuilder will perform faster than StringBuffer.  The reason for the performance hit is likely that StringBuffer is thread-safe through synchronization.  StringBuilder is also mutable but not thread-safe, hence the faster predicted performance.

Saturday, March 9, 2013

My OS X Mountain Lion How-Tos, Shortcuts, & Notes

This collection of apps, how-tos, and notes is for my own record.  Hopefully someone will find them useful too.

Skipping .DS_store files while copying directories between two USB mounted volumes
rsync -rv --exclude=.DS_Store <source> <destination>
Source: Skipping .DS_Store files when copying between two external drives 

Showing and Hiding Hidden Files
defaults write com.apple.finder AppleShowAllFiles -boolean true
killall Finder

The reverse is
defaults delete com.apple.finder AppleShowAllFiles

killall Finder


Open Minimized Windows Using Command Tab
This is rather long.
1. Command + Tab to the window
2. unpress Tab
3. press and hold Option (alt)
4. unpress Command

For windows with multiple items open, play with the arrow keys

(Custom) Maximize Current Window (vertically)
Ctrl + m

Switching Between Multiple Safari Windows
Command + ~ (tilde)

Safari Tabs
Ctrl + Tab

Installing Tar Files (example)
   [frodo:~] testuser% tar xf fink-0.34.5.tar.gz
   [frodo:~] testuser% cd fink-0.34.5
   [frodo:~/fink-0.34.5] testuser% ./bootstrap

Building and Installing a C project?
./configure
make
sudo make install

or?

$ mkdir build
$ cd build
$ cmake ../
$ make
$ make install

Installing a Python project from tar

* Unpack: tar zxvf sslstrip-0.5.tar.gz
* Install twisted:  sudo apt-get install python-twisted-web
* (Optionally) run 'python setup.py install' as root to install,
 or you can just run it out of the directory.

IP Forwarding on OS X Lion+
It is a known fact that IP Forwarding (port forwarding) does not work on OS X Lion by default.
But there is a fix!
Modify your /Library/Preferences/SystemConfiguration/com.apple.Boot.plist file and a <string></string> tag set
with the following:
net.inet.ip.scopedroute=0

PATH in OS X Mountain Lion
/etc/paths
http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion
http://superuser.com/questions/28344/path-env-variable-on-mac-os-x-and-or-eclipse

Disable or modify mouse acceleration
SmoothMouse

Snap Windows to Corners
BetterTouchTool

Wednesday, February 27, 2013

Environment Variables in OSX 10.8.2

I'm still fairly new to OSX, and only recently started tinkering with env variables.  After some confusion on this SO thread, I found this pref pane to be very promising: EnvPane.

Thursday, February 21, 2013

Iterative Solution for Tower of Hanoi in Java

Going through some practice problems, and came across the Tower of Hanoi. The implementation of the iterative solution follows the following steps (borrowed from the wiki article):

"Simpler statement of iterative solution
Alternating between the smallest and the next-smallest disks, follow the steps for the appropriate case:
For an even number of disks:
-make the legal move between pegs A and B
-make the legal move between pegs A and C
-make the legal move between pegs B and C
-repeat until complete
For an odd number of disks:
-make the legal move between pegs A and C
-make the legal move between pegs A and B
-make the legal move between pegs C and B
-repeat until complete In each case, a total of 2ⁿ-1 moves are made."

Code: IterativeHanoi.java

Thursday, October 18, 2012

Classic Shell: great replacement for missing windows 8 start menu

http://classicshell.sourceforge.net/

Pokki constantly Page Faulting

I recently installed Windows 8 in Parallels. I was missing the old start menu, so I installed this app called Pokki. It's a replacement for the missing menu. I was checking out the new OS, going through the Task Manager, when I decided to add the Page Fault Delta and Threads columns under the Details tab, sorted by PF Delta, and noticed that there were two separate Pokki processes. The larger of the two with 29 threads was (is) constantly page faulting:

My mac is overheating, possibly in part due to this (and parallels itself), and I'm not running anything else intensive. I've contacted Pokki help/support, and I'll update with what they say. Edit: They replied saying that this behaviour is normal and should not be a cause for concern. Hm.

Tuesday, October 9, 2012

Demonstrating usage of a Binary Search Tree in Java

What is a Binary Search Tree?

Binary Search Tree (BST) is a tree in which each node has up to two child nodes (subtrees).  If pointers are used, then each node contains references to the left, right, and parent nodes.  Otherwise, like the implementation I found for Java, each node contains a left and right subtree in the form of a left and right node (each of which have their own two subtrees and so on...) along with the data associated with the node.

A BST is said to have a binary-search-tree property:

Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] <= key[x]. If y is a node in the right subtree of x, then key[x] <= key[y].1

Since Java doesn't have pointers that we can use, we won't keep track of keys. We'll work directly with node data.

Cormen et-all mention that BSTs support the following operations:

Search trees are data structures that support many dynamic-set operations, including
SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, and
DELETE. Thus, a search tree can be used both as a dictionary and as a priority
queue.


What are some applications of BST?

This StackOverflow question answers this well:
What are the applications of binary trees?

Apparently, it is also used in the solution to the Traveling Salesman Problem and the related Shortest Path Problem. I'll be checking these out soon.


How do I use a BST in Java?

I found a good Java BST impl here:
BST.java
BSTNode.java

Class BST contains a root BSTNode node and provides most BST operations (as identified by Cormen et-all). The PREDECESSOR and SUCCESSOR operations are not implemented. Search is implemented, though not exposed directly (it's exposed as isInTree(T el)).

I've slapped together an Eclipse project that demonstrates the usage of this BST implementation. I added some code to BST.java (the one in the Eclipse project) borrowed from this Stanford CS Library page on BSTs. I also borrowed some inspiration from this SO page: How to print binary tree diagram. The class BTreePrinter in the project is pretty much directly from this SO page.

It would be worthwhile to study at least the BST.java and BSTNode.java.

>Download BST_Demo (Eclipse Project)

Here is the output:


>String arrays:
ACEG:  [Aliyah, Chris, Elijah, Gamal]
BDFH:  [Brian, Doug, Farah, Hasan]

>Insert ACEG:
Aliyah Chris Elijah Gamal 

>Insert BDFH:
Aliyah Brian Chris Doug Elijah Farah Gamal Hasan 

>Removing 'Aliyah':
Brian Chris Doug Elijah Farah Gamal Hasan Aliyah Brian Chris Chris Doug Elijah Elijah Farah Gamal Gamal Hasan 

>Clearing bst.

>Reinserting both arrays:
Aliyah Brian Chris Doug Elijah Farah Gamal Hasan 

>Search for 'Irene':
false

>Search for 'Aliyah':
true

>Postorder: 
Brian Doug Farah Hasan Gamal Elijah Chris Aliyah 
>Postorder (iterative): 
Brian Doug Farah Hasan Gamal Elijah Chris Aliyah 
>Postorder (Morris): 
Brian Doug Farah Hasan Gamal Elijah Chris Aliyah 

>Preorder: 
Aliyah Chris Brian Elijah Doug Gamal Farah Hasan 
>Preorder (iterative): 
Aliyah Chris Brian Elijah Doug Gamal Farah Hasan 
>Preorder (Morris): 
Aliyah Chris Brian Elijah Doug Gamal Farah Hasan 

>Size:
8

>Max depth:
5

>Min val:
Aliyah

>Max val:
Hasan

Aliyah Brian Chris Doug Elijah ElijahGamal Farah Gamal Hasan 

>Print all root-leaf paths:
Aliyah Chris Brian 
Aliyah Chris Elijah Doug 
Aliyah Chris Elijah Gamal Farah ElijahGamal 
Aliyah Chris Elijah Gamal Hasan 


>Mirroring bst:
Aliyah Brian Chris Doug Elijah ElijahGamal Farah Gamal Hasan 
Hasan Gamal Farah ElijahGamal Elijah Doug Chris Brian Aliyah 

>Doubling bst:
Hasan Hasan Gamal Gamal Farah Farah ElijahGamal ElijahGamal Elijah Elijah Doug Doug Chris Chris Brian Brian Aliyah Aliyah 

>bst:
Aliyah Brian Chris Doug Elijah Farah Gamal Hasan 
>bst2:
Aliyah Brian Chris Doug Elijah Farah Gamal Hasan 
>bst identical to bst2?:
true

>Possible unique trees given this bst's size: (8)
1430

>Check bst:
true

>Printing bst (bad spacing):
               Aliyah                               
                      \               
                       \              
                        \             
                         \            
                          \           
                           \          
                            \         
                             \        
                       Chris               
                      /      \       
                     /        \      
                    /          \     
                   /            \    
                   Brian       Elijah       
                          /       \   
                         /         \  
                         Doug   Gamal   
                            /      \ 
                            Farah Hasan 
                                                                

>Another bst print:
'- Aliyah
    '- Chris
        '- Brian
        '- Elijah
            '- Doug
            '- Gamal
                '- Farah
                '- Hasan

>Inst and init int bst (unbalanced because of ordered insert):
'- 1
    '- 2
        '- 3
            '- 4

Unbalanced integer tree:
       1               
        \       
         \      
          \     
           \    
           2       
            \   
             \  
             3   
              \ 
              4 
                                

Cleared and intBst.balance(intArr):
'- 2
    '- 1
    '- 3
        '- 4

>Balanced integer tree:
   2       
  / \   
 /   \  
 1   3   
      \ 
      4 
                


1 Introduction to Algorithms Second Edition (Cormen, Leiserson, Rivest, Stein), page 254