Python program to push files into Bitbucket.org

Here I am going to show you a simple program, using which you can add files to your bibucket repository. If you are using Linux operating system, then go to terminal and run this program. When you are asked to input username and password please input your bitbucket username and password correspondingly. When you are asked to enter reponame give your bitbucket repository name as it is.

import shlex, subprocess
from subprocess import call

print 'Enter repository address :' 
repo = raw_input()
call("hg clone "+repo, shell=True)

print 'Enter filenames to copy :'
fnames = raw_input()
files_list=shlex.split(fnames)

print'Enter reponame'
reponame = raw_input()

print 'copying to repository'
for i in range(len(files_list)):
	call(["cp",files_list[i],reponame])

print'adding'
subprocess.Popen(["hg", "add"],cwd=reponame)

print'Enter Username of bitbucket'
username = raw_input()
print'Enter a  passphrase'
passphrase = raw_input()

print 'committing added files'
subprocess.Popen(["hg","commit","-u",username,"-Am",passphrase],
                                                          cwd=reponame)

print'Pushing files to repository'
print'Enter username'
proc=subprocess.Popen(["hg","push"],cwd=reponame,stdout=subprocess.PIPE,)
stdout_value=proc.communicate()[0]

print' program finishing...files are uploaded...'

Linked List Implementation using python

We can implement linked list using python using the class concept.

class node:
    def __init__(self):
    self.data = None # contains the data
    self.next = None # contains the reference to the next node

class linked_list:
    def __init__(self):
        self.cur_node = None

    def add_node(self, data):
        new_node = node() # create a new node
        new_node.data = data
        new_node.next = self.cur_node # link the new node to the                         
                                       'previous' node.
        self.cur_node = new_node # set the current node to the new one.
 
    def list_print(self):
        node = ll.cur_node
        while node:
        print node.data
        node = node.next

ll = linked_list()
ll.add_node(1)
ll.add_node(2)
ll.add_node(3)
ll.add_node(4)
ll.add_node(5)

ll.list_print()

An optimized python program to find the n th fibonacci number

We are familiar with the Fibonacci series and algorithm to find the n th Fibonacci number  using recursion. which may be as follows.

def fibonacci(n):factorial

    if n<2 : return n

    else: return fibonacci(n-1) + fibonacci(n-2)

Which seems to be a simple solution. But in actual practice using recursion is a tedious task for the computer. The complexity of calculation increases incredibly with an increase in single number. with this algorithm to compute fibonacci(30), it will take few seconds to calculate the result( result is 832040 ). Further increase in number may not produce any result due to stack overflow.  Recursion uses stacks to remember function calls and every computer has a stack size limit.If stack cannot hold these recursive function calls it will not produce any result or sometimes take long time to print result.So Even though recursion makes the solution is simple, it is always advisable to stay out from it.

But using python dictionary we can reduce the complexity of recursive function call by remembering the results of each function call and thus reusing the value straight away with out function call. It will dramatically reduce the recursive function call complexity. we can even compute fibonacci(220) ( I have done it in less than 1sec) easily. Here is the program.

def fib(n):
    if n<2 : return n
    elif not n in fib_dict :
            fib_dict[n]= fib(n-1) + fib(n-2)
    return fib_dict[n]

#dictionary which store Fibonacci values
fib_dict = {}
result = fib(220)
print result

# result :4244200115309993198876969489421897548446236915

Optimized python program to find power of a number

def power(a,b):
        if a==0: return 0
        elif b==0: return 1
        elif b==1: return a
        elif b%2 == 0:
                res_even = power(a,b/2)
                return res_even*res_even
        else :
                b=(b-1)/2
                res_odd= power(a,b)
                return a*res_odd*res_odd

pow = power(2,10)
print pow
             #pow = 1024

The best way to find power of a number is to use recursion ( other than using library function). If we use normal recursion as
def power(a,b):
    if b==0: return 1
    else : return a*power(a,b-1)
,the calculation requires as many function call as ‘b’,and this will worsen the condition when we have to find the power of larger numbers.We can simplify this problem by considering the clue : 2^8 = (2^ 4)^2.