Option #1
man -P cat ls > man_ls.txt
Option #2:
man ls | col -b > ls.txt
man -P cat ls > man_ls.txt
man ls | col -b > ls.txt
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace dotNet
{
class Greeting
{
public static int displayGanesh()
{
Console.WriteLine("Ganesh Here !!");
return 0;
}
public static int displayVishnu()
{
Console.WriteLine("Vishnu Here !!");
return 1;
}
}
delegate int delGreetings();
class Program
{
static void Main(string[] args)
{
int choice = 1;
delGreetings[] Greetings =
{
new delGreetings(Greeting.displayGanesh),
new delGreetings(Greeting.displayVishnu)
};
int return_ = Greetings[choice - 1]();
Console.Write("Return Value ");
Console.WriteLine(return_);
return;
}
}
}
delegate int delGreetings();
Nilavodu theigiren..
Ninaivale karaigiren..
sugamana vedhanai..
Idhu kaadala...
Engeyo.. engeyo..
ennule engeyo..
vinmeengal sidharudhe
yaenadi..
Anbe vaa..
Anbe vaa..
Anbe vaa
Anbe vaa.. a...
Ah...
(Female)
Kannukule kannukule.. kaadhal vandhu nenjai thotu povathen..
(Male)
Enna idhu enna idhu.. vaanavillil vanam rendu koodudhe..
(Female)
Theendinal.. vaanile..
megamai alaigiren
(male)
neenginal dhoorathil..
pulliyai tholaigiren..
Nillarendral nillaamal
yenendru kelaamal
edhedho seigirai..
yaenadi..
Anbe vaa..
Anbe vaa..
Anbe vaa
Anbe vaa.. a...
(F)Enna solli enna solli kaadhal adhai unnidathil kaatuven..
(m)Sathamindri sathamindri mounamai nenjukulle pootuven..
(F)kavithaigal ezhudhida vaarthaigal thedinen..
(M)un peyar ezhudhinaal kavidhayai paadinen..
Enn ulle en ulle..
un kannin minsaram..
edhedho seiyudhe..
Yaenadi..
Anbe vaa..
Anbe vaa..
Anbe vaa
Anbe vaa.. a...
#include "stdafx.h"
#include < iostream >
#include < map >
int main()
{
// 1. define the map
typedef std::mapMap;
// 2. Create the object for it
Map myMap;
for (int i=0; i<10; ++i)
{
/****************************
3. Whats going on here ???.
We just created the object.
Could this be a buggy code ??
Nope. When we reference an item in map
and if the item is not available,
it creates the object. Great :)
I personally seen/used the creation
of map on the left side,
something like myMap[2] = 20
But the below is of something which
I am seeing new today :)
**********************************************/
int &ref = myMap[i];
ref = i;
}
for (int i=0; i<10; ++i)
{
std::cout << myMap[i] << " ";
}
std::cout << std::endl;
return 0;
}
class A
{
struct B; // forward declaration
B* c; A()
{
c->i;
}
};
struct A::B
{
/** we define struct B like this
** becuase it was first declared
** in the namespace A */
int i;
};
int main()
{
}
struct timeval {
time_t tv_sec; /* in secs */
suseconds_t tv_usec; /* in Microseconds */
};
#include
#include
#include
int main()
{
struct timeval start, end;
long mtime, seconds, useconds;
// Get the start time
gettimeofday(&start, NULL);
// Here you go with your method call
usleep(2000);
// note the end time
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
printf("Elapsed time: %ld milliseconds\n", mtime);
return 0;
}
#!/usr/bin/env python
def elapsed_time (seconds, suffixes=['y','w','d','h','m','s'], add_s=False, separator=' '):
"""
Takes an amount of seconds and turns it into a human-readable amount of time.
"""
# the formatted time string to be returned
time = []
# the pieces of time to iterate over (days, hours, minutes, etc)
# - the first piece in each tuple is the suffix (d, h, w)
# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52),
(suffixes[1], 60 * 60 * 24 * 7),
(suffixes[2], 60 * 60 * 24),
(suffixes[3], 60 * 60),
(suffixes[4], 60),
(suffixes[5], 1)]
# for each time piece, grab the value and remaining seconds, and add it to
# the time string
for suffix, length in parts:
value = seconds / length
if value > 0:
seconds = seconds % length
time.append('%s%s' % (str(value),
(suffix, (suffix, suffix + 's')[value > 1])[add_s]))
if seconds < 1:
break
return separator.join(time)
if __name__ == '__main__':
# 2 years, 1 week, 6 days, 2 hours, 59 minutes, 23 seconds
# 2y 1w 6d 2h 59m 23s
seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 1) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
print elapsed_time(seconds)
print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'])
print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True)
print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True, separator=', ')
#!/bin/bash
if [ this -eq that ];then
command1
else
command2
fi
#!/bin/bash
[ this -eq that ] && CMD=”/bin/ls” || CMD="/bin/date";
eval $CMD;
#!/bin/bash
execute() {
# $1 holds the arg to this function
CMD="$1";
eval $CMD;
}
## Here is your main function
if [ this -eq that]
then
execute "/bin/ls | wc -l";
else
execute "/bin/ls";
fi
#! /usr/bin/python
import sys;
if __name__ == "__main__":
for args in sys.argv:
print args;
#!/usr/bin/env python
## In Python each module will have a name associated with it
## And here is the main module
if __name__ == "__main__":
print "Hello World";
This is an interview question from MS.
Answer:
n! = 2*3*...*n >= 2*2*...*2 = 2^(n-1)
Since 2^n <= 2*n! for all n, we have that 2^n = O(n!).
for(j = 0; j <= 1; j++)
{
for(i = 0; i<n; i++)
{
if(arr[ i ] != i)
{
swap(arr[ i ], arr[ arr[ i ] ]);
}
}
}
</pre>
Note: j loop runs only twice irrespective on 'n' and has constant complexity. The order of this whole loop is 2*n = O(n).
4. After the array is sorted, Again traverse thru the array and make elements arr[0] to
arr[n/2] to '1' and arr[(n/2)+1] to arr[n] as '0'.
Space complexity is constant and time complexity is O(step2) + O(step3) + O(step4) = n + 2n +n = 4*n = O(n).
Solution #1 (Stable):
------------------------
<pre name='code' class='cpp'>
#include<iostream.h>
int a[] = { 1,0,0,0,1,1,1,0,0,1};
#define _SIZE sizeof(a)/sizeof(a[0])
/* Helper Function */
void printArray()
{
for(int i=0;i<_SIZE; ++i)
cout<<a[i]<<" ";
cout<<endl;
}
int main()
{
int countOne = 0;
int countZero = (_SIZE)/2;
int i = 0;
/* Fill the array with numbers from 1 to N */
for(i=0; i < _SIZE; ++i)
{
if(a[i] == 1)
{
a[i] = countOne;
countOne++;
}
else
{
a[i] = countZero;
countZero++;
}
}
printArray();
/* Swap the number and make it a sorted one */
for(int j = 0; j< 2; ++j)
{
for(i=0; i < _SIZE; ++i)
{
if(a[i] != i)
{
int temp = a[i];
a[i] = a[a[i]];
a[temp] = temp;
}
}
}
printArray();
/* Fill the 1st N/2 elements with 1 */
for(i=0; i<_SIZE/2; ++i)
a[i] = 1;
/* Fill the last N/2 elements with 1 */
for(i=_SIZE/2; i<_SIZE; i++)
a[i] = 0;
printArray();
return 0;
}
</pre>
Solution #2 (UnStable):
-----------------------------------
Here is a small variation of the quick sort to solve the same problem but this solution is not stable. So I prefer #1.
<pre name='code' class='cpp'>
#include<iostream.h>
using namespace std;
#define _SIZE sizeof(a)/sizeof(a[0])
void swap(int *a, int l, int r)
{
cout<<"Swaping "<<l<<" "<<r<<endl;
if(a[l] != a[r])
a[l] ^= a[r] ^= a[l] ^= a[r];
return;
}
bool compare_1_upper(int *a, int u, int p)
{
return (a[u] == p);
}
bool compare_1_lower(int *a, int l, int p)
{
return (a[l] < p);
}
bool compare_0_upper(int *a, int u, int p)
{
return (a[u] > p);
}
bool compare_0_lower(int *a, int u, int p)
{
return (a[u] == p);
}
bool (*funcPtr_upper)(int *, int, int) = 0;
bool (*funcPtr_lower)(int *, int, int) = 0;
void qPartition(int *a, int low, int upper)
{
int pivot = a[low];
int l = low - 1;
int u = upper + 1;
if(a[low])
{
funcPtr_upper = compare_1_upper;
funcPtr_lower = compare_1_lower;
}
else
{
funcPtr_upper = compare_0_upper;
funcPtr_lower = compare_0_lower;
}
while(1)
{
while(funcPtr_upper(a, --u, pivot));
while(funcPtr_lower(a, ++l, pivot));
if(l<u)
swap(a, l, u);
else
return;
}
return;
}
int a[] = { 1,1,1,1,1,0,0,1};
void print()
{
for(int i=0; i< _SIZE; ++i)
cout<<a[i]<<" ";
cout<<"\n";
}
int main()
{
print();
qPartition(a, 0, _SIZE - 1);
print();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/* pray that a long is the size of a struct link* */
typedef unsigned long pointer;
struct link
{
pointer next_prev;
int payload;
};
typedef struct link link;
link* add_data(int payload, struct link* list)
{
struct link * new_link = (struct link*)malloc(sizeof(link));
assert(new_link);
new_link->next_prev = (pointer)list;
new_link->payload = payload;
if (list != NULL)
{
list->next_prev = (pointer) list->next_prev ^ (pointer)new_link;
}
return new_link;
}
void walk_list(link *list)
{
struct link* prev = 0;
while (list != NULL)
{
pointer next = ((pointer)prev) ^ list->next_prev;
printf("%d ", list->payload);
prev = (struct link*)list;
list = (link*)next;
}
printf("\n");
}
int main(void)
{
link *l1 = add_data(1, NULL);
link *l2 = add_data(2, l1);
/* add something to the front ... */
/* add something to the back ... */
link *l3 = add_data(3, l2);
link *l4 = add_data(4, l3);
link *l5 = add_data(5, l4);
/* walk from front to back */
walk_list(l1);
/* walk from back to front */
walk_list(l5);
return 0;
}
<br />#include<iostream.h><br />#define _SIZE sizeof(a)/sizeof(a[0])<br />using namespace std;<br />void printArray(int *array, int size)<br />{<br /> for(int i=0; i<size; ++i)=""><br /> cout<<array[i]<<" ";<br /> cout<<endl;<br /> return;<br />}<br />void swap(int *a, int left, int right)<br />{<br /> if(a[left] != a[right])<br /> {<br /> a[left] ^= a[right] ^= a[left] ^= a[right];<br /> }<br />}<br />void reArrange(int *a, int length)<br />{<br /> int low, mid, high = length -1;<br /> low =0; mid = 0;<br /> while(mid <= high)<br /> {<br /> switch(a[mid])<br /> {<br /> case 0 :<br /> swap(a, low, mid);<br /> low++;<br /> mid++;<br /> break;<br /> case 1 :<br /> mid++;<br /> break;<br /> case 2 :<br /> swap(a, mid, high);<br /> high--;<br /> break;<br /> default:<br /> cout<<">>> Error \n";<br /> }<br /> }<br />}<br />int main()<br />{<br /> int a [ ] = { 1,1,1,0,1,2,2,1,0,2,1,2,0 };<br /> printArray(a, _SIZE);<br /> reArrange(a, _SIZE);<br /> printArray(a, _SIZE);<br /> return 0;<br />}<br /><br />