Wednesday, April 12, 2017

count number of leaf nodes

Python 3 solution:

class Node():
    def __init__(self, data, left=None , right=None):
        self.data = data
        self.left = left
        self.right = right
   
    def countLeafNode(self):
        node = self
        stack = []
        stack.append(node)
        count = 0
        while len(stack):
            node = stack.pop()
            if node.left is None and node.right is None:
                count += 1
            else:
                stack.append(node.right)
                stack.append(node.left)
        return count

if __name__ == "__main__":
    # make tree
    root = Node(1)
    root.left= Node(2)
    root.left.left = Node(4)
    root.left.right = Node(5)
    root.right = Node(3)
    root.right.left = Node(5)
    root.right.right = Node(7)
    print(root.countLeafNode())
   
    

Friday, August 12, 2016

A “failed to fetch” error occurs when apt-get update is run. How do I fix this?

I was facing the same problem and figured the easiest solution is to reset the sources of /etc/apt/sources.list. To do so, follow these steps:
  1. Obtain the release of your Ubuntu version, type into the console:
    lsb_release -r
  2. Go to http://repogen.simplylinux.ch/ to generate a new sources.list
  3. Select your country & release
  4. Check the first 12 boxes:
    All Ubuntu Brances + Security & Updates
  5. Generate and copy your new list
  6. Backup the old file to sources.list.old
    mv /etc/apt/sources.list /etc/apt/sources.list.old
  7. Open vim to save the new list
    vi /etc/apt/sources.list
  8. Paste and save using :wq
  9. G2G, retry running apt-get update


If you are getting below error then remove broken repository.

W: The repository 'http://ppa.launchpad.net/colingille/freshlight/ubuntu xenial Release' does not have a Release file.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.


$ sudo add-apt-repository --remove ppa:colingille/freshlight
 This PPA contains my free softwares.
Website: http://www.congelli.eu/
 More info: https://launchpad.net/~colingille/+archive/ubuntu/freshlight
Press [ENTER] to continue or ctrl-c to cancel removing it


If you are getting below error:


W: Failed to fetch gzip:/var/lib/apt/lists/partial/us.archive.ubuntu.com_ubuntu_dists_natty_main_source_Sources  Hash Sum mismatch, 
E: Some index files failed to download. They have been ignored, or old ones used instead.

then just remove all the content of /var/lib/apt/lists directory and update.

sudo rm -rf /var/lib/apt/lists/*
sudo apt-get update

A “failed to fetch” error occurs when apt-get update is run. How do I fix this?

I was facing the same problem and figured the easiest solution is to reset the sources of /etc/apt/sources.list. To do so, follow these steps:
  1. Obtain the release of your Ubuntu version, type into the console:
    lsb_release -r
  2. Go to http://repogen.simplylinux.ch/ to generate a new sources.list
  3. Select your country & release
  4. Check the first 12 boxes:
    All Ubuntu Brances + Security & Updates
  5. Generate and copy your new list
  6. Backup the old file to sources.list.old
    mv /etc/apt/sources.list /etc/apt/sources.list.old
  7. Open vim to save the new list
    vi /etc/apt/sources.list
  8. Paste and save using :wq
  9. G2G, retry running apt-get update


If you are getting below error then remove broken repository.

W: The repository 'http://ppa.launchpad.net/colingille/freshlight/ubuntu xenial Release' does not have a Release file.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.

$ sudo add-apt-repository --remove ppa:colingille/freshlight
 This PPA contains my free softwares.
Website: http://www.congelli.eu/
 More info: https://launchpad.net/~colingille/+archive/ubuntu/freshlight
Press [ENTER] to continue or ctrl-c to cancel removing it

Thursday, March 31, 2016

bubble sort - o(n*n)

#!/usr/bin/python

def bubble_sort(arr):
for i in range(0, len(arr)):
for j in range(i, len(arr)):
if arr[i] > arr[j]:
arr[j], arr[i] = arr[i], arr[j]

arr= [8, 3, 0, 8, 9, 3, 0, 5]
bubble_sort(arr)
print arr

selection sort - O(n*n)

#!/usr/bin/python
def selection_sort(arr):
    for i in range(0,len(arr)):
        min_index = i
        ## find minimum element in remaining
        ## array
        for j in range(i+1, len(arr)):
            if arr[j] < arr[min_index]:
                min_index = j
        ## swap the minimum element with arr[i]
        arr[i], arr[min_index] = arr[min_index], arr[i]

arr = [3, 0, 9, 4, 2, 9, 1, 0]
selection_sort(arr)
print arr

Wednesday, March 30, 2016

Binary Search O(log N)

#!/usr/bin/python

### A recursive binary search function
def binarySort(arr,l,r,ele):
    m = (l+r)/2
    if arr[m] == ele:
        return m;
    elif arr[m] < ele:
        return binarySort (arr, m + 1, r, ele)
    else:
        return binarySort (arr, l, m - 1, ele)
    return -1
'''
##A iterative binary search function
def binarySort(arr, l, r, ele):
    while l < r:
        m = (l+r)/2;
        if arr[m] == ele:
            return m
        elif arr[m] < ele:
            l = m + 1
        else:
            r = m - 1
    return -1
'''
arr = [1,2,3,4,5,6,7]
l= 0
r= len(arr)-1
ele= 2
index = binarySort(arr, l, r, ele)
if index == -1:
    print "not found"
else:
    print "found at index -- "+str(index)

Friday, March 4, 2016

How to setup SVN server on ubuntu 14.04

Step 1 » Issue the below command to update the repositories.

sudo apt-get update
Step 2 » After updating repositories , Issue the below command to install SVN and apache webserver (To access SVN through http ) .
sudo apt-get install subversion apache2 libapache2-svn apache2-utils
Step 3 » Now create a directory and create a new repository in that directory ( Here i’m using testrepo as repository name ).
sudo mkdir -p /svn/repos/
sudo svnadmin create /svn/repos/testrepo

Step 4 » Now change ownership for the repository.
sudo chown -R www-data:www-data /svn/repos/testrepo
Step 5 » Create a file testrepo.conf in /etc/apache2/sites-available/ and add the below lines for creating apache virtual host.
SVNParentPath /svn/repos/ : Parent Directory without repository name.
AuthUserFile /etc/svnpasswd : File need to be created ( Step 8) for user details.
Step 6 » Issue the below command to enable the Site ( testrepo in the below command should match the file name created in the previous step )
sudo a2ensite testrepo
Step 7 » Now restart or reload apache service.
sudo service apache2 reload
Step 8 » Issue the command to create user for accessing repository and add the user details to /etc/svnpasswd file.
Use this command to create first user.
sudo htpasswd -cm /etc/svnpasswd user1
Use the same command without c option to create additional users.
sudo htpasswd -m /etc/svnpasswd user2
Step 9 » Now you can access http://yourip/svn/testrepo in your browser ( Eg http://10.0.1.15/svn/testrepo ) and you can see the page like below after successful authentication.
Setup svn server ubuntu 14.04
That’t it , your server is ready.