Sunday, September 25, 2016

git commands

List of git commands that we might need when working with git.

In local machine after installing git:

git log #Log of our repo

git clone repo #To take a copy from repository in git hub to our local machine

git checkout <commitid> #To a detached headstate

For details on detached head state  please check this link

git diff <first commit id> < second commit id>

git init

git status

--Commands to add files to staging area

git add 'filename1'
git add 'filename2'

git status

git commit -m "Comment"

git diff <workingdir> <Staging dir>

git diff --staged #to diff between staging and repo

git reset --hard #to revert to previous commit ,to make master point to some previous commit

Branches:

git branch <branch-name>
git checkout <branch-name>

git log --graph --oneline branchname branchaname

[git checkout  -b <branchname>

equals to following two commands

git branch <newbranchname>
git checkout <newbranchname>]

Merge :

git merge <b1> <b2>

Diff between a commit and parent with out knowing it's parent id

git show <commitid>

Delete a branch:

git branch -d <branchname>

Remote branch:

git remote  add origin url
git remote
git remote -v

git push origin master

Pull branch:

git pull origin master

git log  #to update the remote to local and check if updates happens.

Fork a repo:

Merging remote changes:

If remote and local has changes on same file.During conflicts, use fetch.

git fetch origin
git merge master origin/master

origin/master points to the remote repository on git hub

Pull request commands:

git branch newbranchname
git checkout newbranchname

git checkout master

git add 'filename'
git commit
git push origin <newbranchname>


Clone Remote and cross -repo conflicts:

git remote add upstream 'original repo'
git checkout master
git pull upstream master

git checkout 'anotherbranch'

origin - forked repo
upstream - original repo

Merge newbranch with master

git merge master 'anotherbranch'

git add
git commit
git push origin 'anotherbranch'

Delete a remote branch:

git branch --remote
git push origin --delete <branch-name> 


List of Programming languages from A- Z

A - Ada
B - Bash
C - C
D - D
E - Erlang
F - Fortan
G - go
H - Haskell
I - Icon
J - Java
K - K
L - Lisp
M - MATLAB
N - NSIS
O - Octave
P - Python
Q - Q
R - R
S - Scala
T - Tcl
U - Unix
V - Verilog
W - Whitespace
X - xBase
Y - Yorick
Z - ZShell

Project Euler #8 - Largest Product in a series - Python

n = map(int, raw_input().split()) #To read the number of digits and 
                                  #k consecutive numbers
num = int(raw_input()) # To read the digit
#print n[1]
#print num
stri = str(num)
product = 0
for i in xrange(len(stri) - n[1] + 1):
        temp = 1
        k = i
        for j in range(n[1]):        
            temp = temp * int(stri[k])
            k = k + 1
           
        if(temp > product):
            product = temp
            #print product
print product  

Saturday, August 27, 2016

Longest Increasing Sequence using python


import sys
import copy

#Binary search
def binary(s, num):
#print "in binary s:"  + str(s)
#print "in binary num:" + str(num)
    l = len(s)
    mid = l/2
    #print "in binary s:" + str(s)
    #print "in binary num:" + str(num)
    #print "s[mid]:" + str(s[mid])
    if((l!=0) and  (l == 1)):
    if(s[mid] < num):
    return s[mid]

    if((l!=0) and (l != 1)):
   if ((s[mid] >= num) and (s[mid - 1 ] < num)):
       return s[mid - 1]
   elif ((s[mid] > num) and (s[mid - 1] > num)):
       return binary(s[:mid -1] , num)
   else:
       return binary(s[mid:], num)
     
 
 
t = int(raw_input())
for p in xrange(0,t):
    n = int(raw_input())
    arr = map(int, raw_input().split())
#maxlen in  worst case would be 1 and end element index is 0.
    maxlen = 1
    bestend = 0

    s = []
    LIS = []
    s.append(arr[bestend])
    LIS.append(s)
    #LIS[-1].append(s)
    #print s
    #print LIS

    #To get last elements of all lists
    #print max(LIS, key=len)[-1]
    # to get the last element of the max length in the list
     
 
    for i in xrange(1,n):
        #print "arr[i]:" + str(arr[i])
        #print "list end element to compare:" + str(max(LIS, key=len)[-1])
        if(arr[i] > max(LIS, key=len)[-1]):
         
            seq = copy.deepcopy(LIS)
         
            max(seq, key=len).append(arr[i])        

            LIS.append(max(seq, key=len))
            #print "post updated LIS:" + str(LIS)
         
         
     
        else:
         
            seq = copy.deepcopy(LIS)
            last_elem = [x[-1] for x in seq]
            val_greater_elem = binary(sorted(last_elem), arr[i])
            if val_greater_elem in last_elem:
                ind = last_elem.index(val_greater_elem)
                #print "Index of ele:" + str(ind)# to get index of the ele to append
                #print "seq ind to append:" + str(seq[ind][-1])
                seq[ind].append(arr[i])#replace element
                #print "updated seq:" + str(seq)
                #print "updated seq index:" + str(seq[ind])
                #print "Length of seq:" + str(len(seq[ind]))
                len_of_updated = len(seq[ind])
                k = [x for x in LIS if (len(x) == len_of_updated)]
                [LIS.remove(l) for l in k]
                #print "post discard LIS:" + str(LIS)
                LIS.append(seq[ind])
                #print "post updated LIS:" + str(LIS)
     
    print len(max(LIS, key=len))
    print "Final subseq:" + str(max(LIS, key=len))
 
     
stdin:
1
16
0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15            

Output:
6
Final subseq: [0 2 6 9 11 15]
    

Binary search to find largest smaller element than given value python

import sys
def binary(s, num):
    l = len(s)
    mid = l/2
    #print "in binary s:" + str(s)
    #print "in binary num:" + str(num)
    #print "s[mid]:" + str(s[mid])
    if((l!=0) and  (l == 1)):
     if(s[mid] < num):
     return s[mid]
        #else:
#return None
    if((l!=0) and (l != 1)):
    if ((s[mid] >= num) and (s[mid - 1 ] < num)):
        return s[mid - 1]
    elif ((s[mid] > num) and (s[mid - 1] > num)):
        return binary(s[:mid -1] , num)
    else:
        return binary(s[mid:], num)


lst = [8,0]

print binary(sorted(lst), 4)

Output:

0