Wednesday, April 2, 2014

Chown, Chgrp and Chmod Introduction

1) chown :  change file owner and group
To check the ownership of a file or directory use ls -l
Usage: chown [-Rcfv] newowner filenames/directory. Take note only root can change the ownership.
Example:
chown linda file.txt
This will cause file.txt to now be owned by linda.
chown -R abu:sales /home/account/
This is going to make all files inside /home/account/ and its subdirectories to belong to abu and to be associated with the group sales. -R means include all subdirectories
.
2) chgrp : change group ownership 
Usage : chgrp [-Rcfv] groupname foo.txt
Example:
chgrp marketing file.txt - to change the group specified to a certain document
chgrp oracle /usr/database – to change the group specified to a certain directory
chgrp -R marketing /sales/2008 – to change the group specified to a certain directory recursively
.
3) chmod : to change the permissions of a file or directory. Use ls -l to see the permission settings.
Below is how the permission is assigned.
rwx rwx rwx = 111 111 111
rw- rw- rw- = 110 110 110
rwx --- --- = 111 000 000

and so on...

rwx = 111 in binary = 7
rw- = 110 in binary = 6
r-x = 101 in binary = 5
r-- = 100 in binary = 4
For example, if we wanted to set some_file to have read and write permission for the owner, but wanted to keep the file private from others, we would:
chmod 600 some_file

Thursday, March 27, 2014

Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree.



If this were a binary search tree, we could do a modified find on the two nodes and see where the paths diverge. Unfortunately, this is not a binary search tree, so we much try other ap- proaches. 

Attempt #1:
If each node has a link to its parent, we could trace p and q’s paths up until they intersect. 

Attempt #2:
Alternatively, you could follow a chain in which p and q are on the same side. That is, if p and q are both on the left of the node, branch left to look for the common ancestor. When p and q are no longer on the same side, you must have found the first common ancestor. 

__author__ = 'nitin'

def common_ancestor(root,node_p,node_q):
    if covers(root.getLeftChild(),node_p) and covers(root.getLeftChild(),node_q):
        return common_ancestor(root.getLeftChild(),node_p,node_q)
    if covers(root.getRightChild(),node_p) and covers(root.getRightChild(),node_q):
        return common_ancestor(root.getRightChild(),node_p,node_q)
    return root

def covers(root,node_p):
    if root is None:
        return False
    if root ==node_p:
        return True
    return covers(root.getLeftChild(),node_p) or covers(root.getRightChild(),node_p)
 

My Profile

My photo
can be reached at 09916017317