Easter Eggs In Python

Wednesday 20 June 2012
This is just for fum:

try these in IDLE and notice whats hidden in python-------
1. import this
2. import __hello__
3. import antigravity
4. from __future__ import braces


Radix Sort In Python

Sunday 17 June 2012
def radix_sort(array,k):
    """k is the max no. of digits in the number in array"""
    num=10
    max_num=10**k
    while num!=max_num:
        def key_arr(x):
            return x%num
        array.sort(key=key_arr)
        num=num*10
    return array

#############################################
array=list(map(int,input().split()))
print(array)
#for a maximum of 4 digit number
array=radix_sort(array,4)
print(array)
#############################################

Traversal in Binary Trees II

Tuesday 12 June 2012
,
Function to print the postorder given the preorder and inorder:

void postorder( int preorder[], int prestart, int inorder[], int inostart, int length)
{
  if(length==0) return; //terminating condition
  int i;
  for(i=inostart; i<inostart+length; i++)
    if(preorder[prestart]==inorder[i])//break when found root in inorder array
      break;
  postorder(preorder, prestart+1, inorder, inostart, i-inostart);
  postorder(preorder, prestart+i-inostart+1, inorder, i+1, length-i+inostart-1);
  printf("%d ",preorder[prestart]);
}

Function to print the preorder given the postorder and inorder:

void preorder( int postorder[],int poststart,int inorder[],int inostart,int length)
{
  //printf("length=%d\n",length);
  if(length==0) return;//terminating condition
  int i;
  for(i=inostart;i<inostart+length;i++)
    if(postorder[poststart]==inorder[i])//break when found root in inorder array
        break;
  printf("%d ",postorder[poststart]);
  preorder(postorder,i-1,inorder,inostart,i-inostart);
  preorder(postorder,poststart-1,inorder,i+1,length-i+inostart-1);
}


Copyright @ 2013 code-craft. Designed by Templateism | MyBloggerLab