Lexicographic Permutation Order

Monday 26 March 2012
#lexicographic permutations
def nextt(ls):
n=len(ls)
i=n-2
while ls[i]>ls[i+1]:
i=i-1
#print(i)
if i<0: data-blogger-escaped-for="for" data-blogger-escaped-i="i" data-blogger-escaped-ii="ii" data-blogger-escaped-in="in" data-blogger-escaped-j="i+1" data-blogger-escaped-ls="ls" data-blogger-escaped-mx="0" data-blogger-escaped-n="n" data-blogger-escaped-print="print" data-blogger-escaped-range="range" data-blogger-escaped-return="return">ls[i],ls[ii],ls[i],mx,j)
if ls[ii]>ls[i]:
mx=max(mx,ii)
#print(i,mx)
ls[i],ls[mx]=ls[mx],ls[i]
#print(ls)
#t=input()
nw=ls[:i+1]
#print(nw)
rw=ls[i+1:]
#print(rw)
rw.reverse()
ls=nw+rw
print(ls)
nextt(ls)

num=list(map(int,list(input())))
nextt(num)

Hashing the elite....

Wednesday 21 March 2012
Hash code-maps:
1.Hash function has two components:
->hash code maps
->compression map
Hash code map changes the key to integer value for using as array indices.
Compression map brings the integer to the range of our array indices.

2.Popular hash code maps:
->Integer cast: If numeric type with 32 bits or less take integer cast as inteeger
->Component Sum:If num is more than 32 bits we could take 32 bits at a time and add
their sum... to get our integer
->Polynomial accumulation:for strings..commbine the ascii values and use them as
co-efficients of polynomial a0+a1x+.. for a certain value of x.The choice of x=33
,37,39 or 41 is likely to give less collissions.

3.Compression maps:
->k mod m:donot take m as power of two or close to a poweer of two as mod 2
is giving the last bits of a data which however could be same for a large
number of numbers.Take the size of the table as a prime number.
->h(k)=|m(k A mod )|
k is the key m is the size of table and a is between 0 and
value of m is not critical can use a power of two.Using a as golden ratio
is known as fibonnaci hashing
->h(k)=|ak+b|mod n known as multiply add and divide(MAD).n is the size of hash
table and a and b are any numbers.Here a should not be a multiple of n or else we will get
b everytime.K could be the initial time.

Implimentation of doubly linked lists in C

Sunday 11 March 2012
this is my implimentation :
#include
#include
struct node{
struct node *prev;
struct node *next;
int info;
}*start;

int length()
{
if(start==NULL)
return 0;
int count=1;
struct node *q;
q=start;
while(q->next!=NULL)
{
count++;
q=q->next;
}
return count;
}

void add_beg(int num)
{
struct node *tmp;
if(start==NULL)
{
tmp->next=NULL;
tmp->prev=NULL;
tmp->info=num;
start=tmp;
}
else{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->next=start;
start->prev=tmp;
tmp->prev=NULL;
start=tmp;
}
}
void display()
{
struct node *q;
if(start==NULL)
{
printf("List is empty\n");
return;
}
q=start;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->next;
}
printf("\n");
}/*End of display() */

void add_after(int num,int loc)
{
struct node *q,*tmp;
int i;
q=start;
if(loc==length())
{
while(q->next!=NULL)
q=q->next;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->next=NULL;
tmp->prev=q;
q->next=tmp;
return;
}
printf("hi");
for(i=0;inext;
if(q==NULL)
{
printf("list does not have that many elements");
return;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->prev=q;
q->next->prev=tmp;
tmp->next=q->next;
q->next=tmp;
}

void del(int num)
{
struct node *q;
q=start;
while(q->next!=NULL)
q=q->next;
if(q->info==num)
{
q->prev->next=NULL;
free(q);
return;
}
else
{q=start;}
while(q!=NULL)
{
if(q->info==num)
{
//delete q
q->prev->next=q->next;
q->next->prev=q->prev;
free(q);
return;
}
q=q->next;
}
printf("element not in in list");
}
void rev()
{
struct node *p1,*p2;
p1=start;
p2=p1->next;
p1->next=NULL;
p1->prev=p2;
while(p2!=NULL)
{
p2->prev=p2->next;
p2->next=p1;
p1=p2;
p2=p2->prev; /*next of p2 changed to prev */
}
start=p1;
}/*End of rev()*/



int main()
{
int choice,n,m,po,i;
start=NULL;
while(1)
{
printf("1.Add at begining\n");
printf("2.Display\n");
printf("3.exit\n");
printf("4.add after\n");
printf("5.delete\n");
printf("6.length\n");
printf("7.reverse\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the element : ");
scanf("%d",&m);
add_beg(m);
break;
case 2:display();
break;
case 3:return 0;
case 4:printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is inserted : ");
scanf("%d",&po);
add_after(m,po);
break;
case 5:printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 6:printf("%d\n",length());
break;
case 7:rev();
break;
default:printf("hello option");
}
}
return 0;
}

Implimentation of linked lists in C

Saturday 10 March 2012
The code works out as follows :
# include
#include
struct Node
{
int Data;
struct Node *Next;
}*Head;
//Set HEAD as NULL
// Adding a Node at the Beginning of the List
// Counting number of elements in the List

int length()
{
struct Node *cur_ptr;
int count=0;

cur_ptr=Head;

while(cur_ptr != NULL)
{
cur_ptr=cur_ptr->Next;
count++;
}
return(count);
}
void addBeg(int num)
{
struct Node *temp;

temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data = num;

if (Head == NULL)
{
//List is Empty
Head=temp;
Head->Next=NULL;
}
else
{
temp->Next=Head;
Head=temp;
}
}
//Adding a Node at the end of the list

void addEnd(int num)
{
struct Node *temp1, *temp2;

temp1=(struct Node *)malloc(sizeof(struct Node));
temp1->Data=num;

// Copying the Head location into another node.
temp2=Head;

if(Head == NULL)
{
// If List is empty we create First Node.
Head=temp1;
Head->Next=NULL;
}
else
{
// Traverse down to end of the list.
while(temp2->Next != NULL)
temp2=temp2->Next;

// Append at the end of the list.
temp1->Next=NULL;
temp2->Next=temp1;
}
}
// Adding a new Node at specified position

void addAt(int num, int loc)
{
int i;
struct Node *temp, *prev_ptr, *cur_ptr;

cur_ptr=Head;

if(loc > (length()+1) || loc <= 0) { printf("\nInsertion at given location is not possible\n "); } else { // If the location is starting of the list if (loc == 1) { addBeg(num); } else { for(i=1;iNext;
}

temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=num;

prev_ptr->Next=temp;
temp->Next=cur_ptr;
}
}
}

// Deleting a node from List depending upon the data in the node.

int delNodeData(int num)
{
struct Node *prev_ptr, *cur_ptr;

cur_ptr=Head;

while(cur_ptr != NULL)
{
if(cur_ptr->Data == num)
{
if(cur_ptr==Head)
{
Head=cur_ptr->Next;
free(cur_ptr);
return 0;
}
else
{
prev_ptr->Next=cur_ptr->Next;
free(cur_ptr);
return 0;
}
}
else
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->Next;
}
}

printf("\nElement %d is not found in the List", num);
return 1;
}
// Deleting a node from List depending upon the location in the list.

int delNodeLoc(int loc)
{
struct Node *prev_ptr, *cur_ptr;
int i;

cur_ptr=Head;

if(loc > (length()) || loc <= 0) { printf("\nDeletion of Node at given location is not possible\n "); } else { // If the location is starting of the list if (loc == 1) { Head=cur_ptr->Next;
free(cur_ptr);
return 0;
}
else
{
for(i=1;iNext;
}

prev_ptr->Next=cur_ptr->Next;
free(cur_ptr);
}
}
return 1;
}
// Displaying list contents

void display()
{
struct Node *cur_ptr;

cur_ptr=Head;

if(cur_ptr==NULL)
{
printf("\nList is Empty");
}
else
{
printf("Elements in the List: ");
//traverse the entire linked list
while(cur_ptr!=NULL)
{
printf(" -> %d ",cur_ptr->Data);
cur_ptr=cur_ptr->Next;
}
printf("\n");
}
}
//Reversesing a Linked List

void reverse()
{
struct Node *prev_ptr, *cur_ptr, *temp;

cur_ptr=Head;
prev_ptr=NULL;

while(cur_ptr != NULL)
{
temp=prev_ptr;
prev_ptr=cur_ptr;

cur_ptr=cur_ptr->Next;
prev_ptr->Next=temp;
}

Head=prev_ptr;
}
int main()
{
Head=NULL;
while(1){
printf("1.addbeg 2.addend 3.addat 4.reverse 5.display 6.deletenum 7.length 8.exit");
int i;
scanf("%d",&i);
switch(i){
case 1:int num;scanf("%d",&num);
addBeg(num);
break;
case 2: num=0;scanf("%d",&num);
addEnd(num);
break;
case 3: num=0;scanf("%d",&num);
int loc;scanf("%d",&loc);
addAt(num,loc);
break;
case 4:reverse();
break;
case 5:display();
break;
case 6:num=0;scanf("%d",&num);
delNodeData(num);
break;
case 7:printf("%d\n",length());
break;
case 8:return 0;
default:printf("not valid\n");
}
}
return 0;
}
Copyright @ 2013 code-craft. Designed by Templateism | MyBloggerLab