Saturday, March 24, 2012

Python - Converting character string to HEXA string

ord(c): Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit string.


hex(n): Convert an integer number to a hexadecimal string


We do not need first to characters in the hex output.


zfill(witdh): Return the numeric string left filled with zeros in a string of length width.


Put of together,


Reference:
href=http://docs.python.org/library/functions.html

Friday, March 23, 2012

Perl - Converting character string to HEXA string



Substitution Operator Modifiers
Here is the list of all modifiers used with substitution operator
Modifier Description
     i   Makes the match case insensitive
     m   Specifies that if the string has newline or carriage
         return characters, the ^ and $ operators will now
         match against a newline boundary, instead of a
         string boundary
     o   Evaluates the expression only once
     s   Allows use of . to match a newline character
     x   Allows you to use white space in the expression
         for clarity
     g   Replaces all occurrences of the found expression
         with the replacement text
     e   Evaluates the replacement as if it were a Perl statement,
         and uses its return value as the replacement text

References:
http://icfun.blogspot.com/2009/05/perl-convert-character-string-into-hex.html
http://www.tutorialspoint.com/perl/perl_regular_expression.htm

Saturday, March 26, 2011

Command-line dictionary

It just parse the website wordnetweb and extract the meaning of the word/phrase given. You need lynx to run this script successfully.


References:
http://ubuntuforums.org/showthread.php?t=371482
http://wordnetweb.princeton.edu/perl/webwn

Sunday, January 9, 2011

Heapsort

Heap
 Sort in place
 Binary tree
 Worst case runtime (n lg n)
PARENT(i) -> i/2
Left (i)  -> 2i
Right (i) -> 2i + 1
MAX_HEAP
A[PARENT(i)] >= A[i]

MIN_HEAP
A[PARENT(i)] <= A[i]
MAX-HEAPIFY
 Maintain the max-heap propert
 MAX-HEAPIFY (A, i)
   l = LEFT (i)
   r = RIGHT (i)
   if l <= A:heap-size and A[l] > A[i]
        largest = l
   else largest = i
   if r <= A.heap-size and A[r] > A[largest]
        largest = r
   if largest != i
        exchange A[i] with A[largest]
        MAX-HEAPIFY (A, largest)

Heap Sorting
 HEAPSORT (A)
  BUILD-MAX-HEAP (A)
  for i = A.length downto 2
    exchange A[1] with A[i]
    A.heap-size = A.heap-size - 1
    MAX-HEAPIFY (A, 1)

Priority queues
 One of the main usage is that we can use max-priority queues to schedule jobs on a shared computer.The max-priority queue keeps track of the jobs to
be performed and their relative priorities. When a job is finished or interrupted,the scheduler selects the highest-priority job from among those pending by calling EXTRACT-MAX. The scheduler can add a new job to the queue at any time by calling INSERT.

Reference: Introduction to Algorithms By Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein

Saturday, October 30, 2010

AVL tree - self-balancing binary search tree

In an AVL tree, the heights of the two child subtrees of any node differ by at most one; therefore, it is also said to be height-balanced. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.

Psuedo code:
IF tree is right heavy
{
  IF tree's right subtree is left heavy
  {
     Perform Double Left rotation
  }
  ELSE
  {
     Perform Single Left rotation
  }
}
ELSE IF tree is left heavy
{
  IF tree's left subtree is right heavy
  {
     Perform Double Right rotation
  }
  ELSE
  {
     Perform Single Right rotation
  }
}

Reference:
http://en.wikipedia.org/wiki/AVL_tree
http://oopweb.com/Algorithms/Documents/AvlTrees/Volume/AvlTrees.htm

Monday, October 25, 2010

Signal handler is not your regular function

Signal handler is a function which will be called when the program receives a signal. Most of the signals have their own default handlers and for few the parent program should handle/ignore this signal explicitly. In case of SIGCHLD, if the parent process should define the handler explicitly and wait for the child to exit (we can use SIGIGN too and no need to wait),otherwise the child process will become zombie.

Signal handler can not do everything what any normal function can do. It can call the functions and system calls which are async-safe.
This is the list of async-safe fuctions and system calls,

_Exit(), _exit(), abort(), accept(), access(), aio_error(), aio_return(), aio_suspend(), alarm(), bind(), cfgetispeed(), cfgetospeed(), cfsetispeed(), cfsetospeed(), chdir(), chmod(), chown(), clock_gettime(), close(), connect(), creat(), dup(), dup2(), execle(), execve(), fchmod(), fchown(), fcntl(), fdatasync(), fork(), fpathconf(), fstat(), fsync(), ftruncate(), getegid(), geteuid(), getgid(), getgroups(), getpeername(), getpgrp(), getpid(), getppid(), getsockname(), getsockopt(), getuid(), kill(), link(), listen(), lseek(), lstat(), mkdir(), mkfifo(), open(), pathconf(), pause(), pipe(), poll(), posix_trace_event(), pselect(), raise(), read(), readlink(), recv(), recvfrom(), recvmsg(), rename(), rmdir(), select(), sem_post(), send(), sendmsg(), sendto(), setgid(), setpgid(), setsid(), setsockopt(), setuid(), shutdown(), sigaction(), sigaddset(), sigdelset(), sigemptyset(), sigfillset(), sigismember(), sleep(), signal(), sigpause(), sigpending(), sigprocmask(), sigqueue(), sigset(), sigsuspend(), sockatmark(), socket(), socketpair(), stat(), symlink(), sysconf(), tcdrain(), tcflow(), tcflush(), tcgetattr(), tcgetpgrp(), tcsendbreak(), tcsetattr(), tcsetpgrp(), time(), timer_getoverrun(), timer_gettime(), timer_settime(), times(), umask(), uname(), unlink(), utime(), wait(), waitpid(), and write().

Ref : http://beej.us/guide/bgipc/output/html/singlepage/bgipc.html#signals