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
ordered
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)
Combinatorics
Combinations with repetitions
C(n+r1 , 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
*****
11236
i.e. we need to add n1 bars to separate r objects.
hence to choose n1 places from (n+r1) we have
C(n+r1,n1)=C(n+r1, r).
“My fault, my failure, is not in the passions I have, but in my lack of control of them.”
Jack Kerouac
The Next Slope
Sorts (Insertion and Bubble)
The following is a code to perform insertion sort and bubble sort.
We define the macro swap as shown below:
#include<conio.h> #include<stdio.h> #include<stdlib.h> #define N 10 inline void swap(int *a,int *b){ if(*a==*b) return; *a+=*b; *b=*a*b; *a=*a*b; }
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++) { temp=arr[i]; for(j=0;j<i;j++) if(arr[j]>arr[i]) break; for(k=i1;k>=j;k) arr[k+1]=arr[k]; arr[j]=temp; } }
bubble sort:
void bubble(int arr[], int size) { int lightest,temp; for(int i=0;i<size;i++) { lightest=size1; for(int k=size2;k>=i;k) { if (arr[lightest]>arr[k]) lightest=k; } swap(arr+lightest,arr+i); } }
driver program:
int main(void) { int arr[]={4,13,2,6,19,4,7,21,8,1}; bubble(arr,N); //or call : //insertsort(arr,N); for(int i=0;i<N;i++) printf("%d ",arr[i]); }
Dreamers of the day
“All men dream but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity; but the dreamers of the day are dangerous men, for they may act their dream with open eyes to make it possible.”
T. E. Lawrence