Friday, September 8, 2017

Solution- python 3.6 - pytest - RecursionError: maximum recursion depth exceeded while calling a Python object

Here's roughly what I did:
  • create/activate a virtualenv
  • pip install -rmisc/requirements/requirements-tests.txt
  • pip install -rrequirements.txt
  • pip install git+https://github.com/pytest-dev/pytest.git@features
  • /usr/bin/python3 scripts/link_pyqt.py .venv
  • edit conftest.py to always skip BDD tests in pytest_ignore_collect (simply set skip_bdd = True)
  • py.test

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)