Thursday, 7 July 2011

QuickBits-4: sshfs

If you have ssh access to another PC and you want to access the folder of that PC the best way is to ssh mount that.
 sshfs username@hostname:/folder /mountfolder 


With this command the the /folder on the ssh host is mounted locally to /mountfolder. And you can access it as local folder.

To unmount the mounted folder

 fusermount -u /mountfolder 

Friday, 1 July 2011

QuickBits-3: Vim diff

Most often when you are working in shell you want to find diff between two files and merge them. vimdiff is very good utility.

 vim -d file1 file2 




Keyboard Shortcuts:



ctrl+w ctrl+w - switch windows
do - diff obtain
dp - diff put
[c - previous difference
]c - next difference
:diffupdate - diff update ;)
:syntax off - syntax off
zo - open folded text
zc - close folded text



Tuesday, 7 June 2011

A Boolean Array Puzzle

Input: A array arr[] of two elements having value 0 and 1

Output: Make both elements 0.

Specifications: Following are the specifications to follow.
1) It is guaranteed that one element is 0 but we do not know its position.
2) We can’t say about another element it can be 0 or 1.
3) We can only complement array elements, no other operation like and, or, multi, division, …. etc.
4) We can’t use if, else and loop constructs.
5) Obviously, we can’t directly assign 0 to array elements.




void changeToZero(int a[2])
{
a[ a[1] ] = a[ !a[1] ];
}

int main()
{
int a[] = {1, 0};
changeToZero(a);

printf("arr[0] = %d \n", a[0]);
printf("arr[1] = %d ", a[1]);
getchar();
return 0;
}


Monday, 23 May 2011

QuickBits-2: Convert flv to mp4

I had lot of flv video files and wanted to convert it to mp4 for my itouch. Here is the quick way. -f is to force the format to mp4.

$ ffmpeg -i file.flv -f mp4 file.mp4

If you have to convert all files in a folder. Here is the small script. It will convert all flv files to mp4 with the same name with extenstion to mp4.

for file in *.flv
do
ffmpeg -i "$file" -f mp4 "${file%.flv}.mp4
done



QuickBits-1: !$

some times u want to take argument from the previous command you gave

say for example

$ ls /user/ramz/dir1/dir2/dir3/

Now you want to cd to the directory you used in ls command. The quick way is

$ cd !$

- This is equivalent to cd /user/ramz/dir1/dir2/dir3
- !$ - Takes the last argument of previous command.


Wednesday, 18 May 2011

Segregate even and odd nodes in a Linked List

Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers same.

Examples:
Input: 17->15->8->12->10->5->4->1->7->6->NULL
Output: 8->12->10->4->6->17->15->5->1->7->NULL

Input: 8->12->10->5->4->1->6->NULL
Output: 8->12->10->4->6->5->1->NULL

// If all numbers are even then do not change the list
Input: 8->12->10->NULL
Output: 8->12->10->NULL

// If all numbers are odd then do not change the list
Input: 1->3->5->7->NULL
Output: 1->3->5->7->NULL





#include "linkedlist.h"
#include

NODE* createLL(int maxNode)
{
int index;
NODE* headNode=NULL;

for (index = 0; index <= maxNode; index++)
{
headNode = addAtHead(headNode, rand()%100);
}
return headNode;
}

NODE* segregateEvenOdd(NODE* headNode)
{
NODE* currentNode = headNode;
NODE* lastNode = headNode;
NODE* previousNode = NULL;
int length = 0;

/* Find lastNode and length */
while(currentNode != NULL)
{
lastNode = currentNode;
currentNode = currentNode->next;
length++;
}

currentNode = headNode;
while(length)
{
/* push OddNumber node to last */
if (currentNode->data % 2 == 1)
{
if (headNode != currentNode)
{
previousNode->next = currentNode->next;
lastNode->next = currentNode;
lastNode = currentNode;
currentNode->next = NULL;
currentNode = previousNode->next;
}
else
{
lastNode->next = headNode;
lastNode = headNode;
headNode = headNode->next;
currentNode->next = NULL;
currentNode = headNode;
}
}
else
{
previousNode = currentNode;
currentNode = currentNode->next;
}
length--;
}

return headNode;
}

/* Driver program */
int main()
{
NODE* headNode=createLL(10);

printList(headNode);

headNode = segregateEvenOdd(headNode);

printList(headNode);
}



Tuesday, 17 May 2011

Write a C program to remove duplicates from a sorted linked list.


#include
#include "linkedlist.h"

#define LL_LIMIT 6
#define LL_DUP1 4

NODE* createLL()
{
int index=LL_LIMIT;
NODE* headNode = NULL;
while(index > 0)
{
headNode = addAtHead(headNode, index);
if (index == LL_DUP1)
{
headNode = addAtHead(headNode, index);
}
index--;
}
return headNode;
}

int removeDupNode(NODE* headNode)
{
NODE* dupNode;
if (headNode == NULL)
{
printf("List is Empty\n");
return 1;
}

while(headNode->next != NULL)
{
if(headNode->data == headNode->next->data)
{
printf("Duplicate data is %d\n", headNode->next->data);
dupNode = headNode->next;
headNode->next = dupNode->next;
free(dupNode);
}
headNode = headNode->next;
}
return 0;
}

int main()
{
NODE* headNode = NULL;

headNode = createLL();

displayAll(headNode);

if(!removeDupNode(headNode))
{
displayAll(headNode);
}
}