Continue reading


Learning Python

  • List methods:

append ( list.append( element )  same list returned)

extend ( list.extend(list2)  same list returned)

list1 + list2 (forms a different list – hence not efficient)

list1+=list2 (same as extend)

in  (“item” in list )

remove ( list.remove(“item”) )

slicing, (gives new list )

negative indices

index   ( list.index(“item”) )

tupple ( list ) returns a tupple of the elements of the list

  • Tupples


slicing allowed

negative indices allowed

tupple[i] – to access ith element

in (can be used)

dictionary keys are immutable – use tupples not lists (also tupple of list is invalid

list( tupple) return a list

Assigning multiple values at once :  v=(‘a’, ‘b’, ‘c’) ;  (x,y,z) =v implies x=’a’, y=’b’, z=’c’

  • String formatting

print “string= %s int = %d” %(str, i)


Combinations with repetitions


C(n+r-1 , r)

n= no of distinct objects to choose from.

r=no of  objects to choose.

Label the objects. Let them be 1,2,… n.

Representation for a combination with repetition:

Let 1,1,2,3,6 be a combination(assuming r=5, n=10). It can be represented as



i.e. we need to add n-1 bars to separate r objects.

hence to choose n-1 places from (n+r-1) we have

C(n+r-1,n-1)=C(n+r-1, r).

The Next Slope

“Everybody wants to be on the mountaintop, but if you’ll remember, mountaintops are rocky and cold. There is no growth on the top of a mountain. Sure, the view is great, but what’s a view for? A view just gives us a glimpse of our next destination, our next target. But to hit that target, we must come off the mountain, go through the valley, and begin to climb the next slope. It is in the valley that we slog through the lush grass and rich soil, learning and becoming what enables us to summit life’s next peak.”
 Andy Andrews

Sorts (Insertion and Bubble)

The following is a code to perform insertion sort and bubble sort.

We define the macro swap as shown below:

#define N 10
inline void swap(int *a,int *b){
       if(*a==*b) return;

notice the importance of the step

  if(*a==*b) return;

if we do not have this step then if the pointers point to the same element we get a result 0 which is not the case.

insertion sort:

void insertsort(int arr[],int size)
                int temp,j,k;
                for(int i=1;i<size;i++)
                        if(arr[j]>arr[i]) break;

bubble sort:

void bubble(int arr[], int size)
     int lightest,temp;
     for(int i=0;i<size;i++)
             for(int k=size-2;k>=i;k--)
             { if (arr[lightest]>arr[k])

driver program:

int main(void)
              int arr[]={4,13,2,6,19,4,7,21,8,1};
              //or call :
              for(int i=0;i<N;i++)
              printf("%d  ",arr[i]);