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
Saturday, March 26, 2011
Command-line dictionary
Labels:
awk
,
Bash
,
dictionary
,
grep
,
linux
,
lynx
,
Shell scripting
,
wordnetweb
Sunday, January 9, 2011
Heapsort
Heap
Sort in place
Binary tree
Worst case runtime (n lg n)
MIN_HEAP
Maintain the max-heap propert
Heap Sorting
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
Sort in place
Binary tree
Worst case runtime (n lg n)
PARENT(i) -> i/2 Left (i) -> 2i Right (i) -> 2i + 1MAX_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
Labels:
algorithms
,
C
,
data structures
,
interview
,
Programming
,
questions
,
sorting
Friday, January 7, 2011
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:
Reference:
http://en.wikipedia.org/wiki/AVL_tree
http://oopweb.com/Algorithms/Documents/AvlTrees/Volume/AvlTrees.htm
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
Labels:
avl
,
binary
,
C
,
deletion
,
height
,
insertion
,
log n
,
Programming
,
self balance
,
self-balanced
,
tree
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
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
Labels:
async-safe
,
C
,
child
,
handler
,
parent
,
process
,
Programming
,
signal
,
wait
,
zombie
Sunday, October 24, 2010
[screen] - Access multiple separate terminal sessions inside a single terminal window or remote terminal session
Its very useful if we want to work in multiple system (office and home PC). We won't lose the shell session connection.
Ref:
http://www.rackaid.com/resources/linux-screen-tutorial-and-how-to/
http://en.wikipedia.org/wiki/GNU_Screen
Ref:
http://www.rackaid.com/resources/linux-screen-tutorial-and-how-to/
http://en.wikipedia.org/wiki/GNU_Screen
Prime numbers
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <string.h>
#include <errno.h>
#define ERRNUM -1
typedef enum bool {
FALSE =0,
TRUE
} e_bool;
typedef enum stauts {
FAILURE = 0,
SUCCESS
} e_status;
typedef struct list* tsp_list;
struct list{
unsigned int val;
unsigned int index;
tsp_list next;
};
e_status Append (unsigned int num, tsp_list *list)
{
tsp_list lsp_primeNode = NULL;
lsp_primeNode = (tsp_list) calloc (1, sizeof (struct list));
if (NULL == lsp_primeNode)
{
fprintf (stderr, "ERROR: memory allocation failed.\n");
return FAILURE;
}
lsp_primeNode->val = num;
lsp_primeNode->next = NULL;
if (NULL == *list)
{
*list = lsp_primeNode;
lsp_primeNode->index = 1;
}
else
{
tsp_list node = *list;
while (NULL != node->next)
{
node = node->next;
}
node->next = lsp_primeNode;
lsp_primeNode->index = node->index + 1;
}
return SUCCESS;
}
e_bool IsPrime (unsigned int num, tsp_list list)
{
if (1 >= num)
{
return FALSE;
}
switch (num)
{
case 2:
case 3:
return TRUE;
default:
{
unsigned int lv_max_limit = (unsigned int) sqrt (num);
while (list && list->val <= lv_max_limit)
{
if (0 == num % list->val)
{
return FALSE;
}
list = list->next;
}
return TRUE;
}
}
}
void GetPrimeList (tsp_list *list, unsigned count)
{
unsigned int i = 2;
unsigned int index = 1;
struct timeval start;
struct timeval end;
struct timeval diff;
if(-1 == gettimeofday(&start, NULL))
{
fprintf (stderr, "ERROR: %s\n", strerror (errno));
return;
}
for (; i && 0 < count; i++)
{
if (TRUE == IsPrime (i, *list))
{
if(-1 == gettimeofday(&end, NULL))
{
fprintf (stderr, "ERROR: %s\n", strerror (errno));
return;
}
/* Ref [http://www.linuxquestions.org/questions/programming-9/how-to-calculate-time-difference-in-milliseconds-in-c-c-711096] */
diff.tv_sec =end.tv_sec - start.tv_sec ;
diff.tv_usec=end.tv_usec - start.tv_usec;
if(diff.tv_usec<0)
{
diff.tv_usec+=1000000;
diff.tv_sec -=1;
}
printf ("%d (index: %d) (Time taken: %f sec)\n", i, index++, (float)(1000000LL*diff.tv_sec + diff.tv_usec)/1000000);
if (FAILURE == Append (i, list))
{
return;
}
else
{
count --;
}
}
}
}
int main (int argc, char *argv[])
{
tsp_list primeList = NULL;
int dig = 0;
if ( 2 != argc)
{
return 0;
}
GetPrimeList (&primeList, (int) atoi(argv[1]));
return 0;
}
Labels:
C
,
number
,
prime
,
Programming
,
time
,
time interval
Subscribe to:
Posts
(
Atom
)