Sunday, December 27, 2009

Minimum of Binary Sub Tree

public Node getMinOfSubTree(Node n)
        {
            if (n.Left == null && n.Right == null) return n;
            Node t = n;
            while (t.Left != null)
            {
                t = t.Left;
            }
            return t;
        }

Maximum of Binary Sub Tree

public Node getMaxOfSubTree(Node n)
        {
            if (n.Left == null && n.Right == null) return n;
            Node t = n;
            while (t.Right != null)
            {
                t = t.Right;
            }
            return t;
        }

Next Smaller Node


public Node getNextSmaller(Node r)
{
Node t = root;
Node gp = root;
Node p = root;
t = root;

while (t != null && !r.Equals(t))
{
gp = p;
p = t;
if (r.CompareTo(t) < 0)
{
t = t.Left;

}
else
{
t = t.Right;
}

}
if (t.Data == this.getMin()) return null;
if (t.Left == null && p.Data < t.Data) return p;
if (t.Left == null && p.Data > t.Data) return gp;
if (t.Left != null && t.Left.Right != null)
{
t = t.Left.Right;
while (t.Right != null)
{
t = t.Right;
}
return t;
}
if (t.Left != null && t.Left.Right == null) return t.Left;

return null;
}

Next Greater Node

public Node getNextGreater(Node n)
{
Node t = null;
Node p = root;
t = root;
Node gp = null;
while (t != null && !n.Equals(t))
{
gp = p;
p = t;
if (n.CompareTo(t) < 0)
{
t = t.Left;

}
else
{
t = t.Right;
}

}
if (n.Data == this.getMax()) return null;
if (t.Right == null && t.Equals(p.Right)) return gp;
if (t.Right == null && t.Equals(p.Left)) return p;
if (t.Right.Left == null && t.Right.Right == null) return t.Right;
if (t.Right.Left != null) return t.Right.Left;
return null;
}

Tuesday, December 15, 2009

Count words in a sentence

public int countWords(string s1)
{
int cnt = 0;
//char space = ' ';
int i = 0;
for (i = 0; i < s1.Length; i++)
{
if (s1[i] == ' '){}
else if (s1[i] != ' ' && i == 0) cnt++;
else if (s1[i] != ' ' && s1[i - 1] != ' ') { }
else if (s1[i] != ' ' && s1[i - 1] == ' ') cnt++;
}
return cnt;
}

Thursday, November 5, 2009

Find files and folder in Windows Vista.

It becomes really hectic to find files in windows vista. It does not search in the folders if you don't have the ownership. It becomes a problem if you have a USB hard drive and you plug it in different computers.

You can download it from here:-

http://www.esnips.com/doc/3ba518f7-6341-476f-98a7-ae2cae8b8454/Find

Saturday, October 17, 2009

ListWmiNamespaces.ps1

# This is a example taken from the book "Microsoft Windows Powershell Step by step"


$wmi = Get-WmiObject -class __Namespace -namespace root
"Listing namespaces on " + $wmi[0].__server +
" please wait a second "
for ($i=0;$i -le $wmi.length;$i++)
{if ($i -lt $wmi.length)
{Write-Host -noNewLine "."
Start-Sleep -m 75}
else
{Write-Host "."}
}

$wmi | Format-List name
Write-Host -foregroundColor green "There are" $wmi.length `
"namespaces on this machine `n"

GetCPUinfo.ps1

# This is a example taken from the book "Microsoft Windows Powershell Step by step"


$wmi = get-wmiObject win32_processor
if ($wmi.Architecture -eq 0)
{"This is an x86 computer"}
elseif($wmi.architecture -eq 1)
{"This is an MIPS computer"}
elseif($wmi.architecture -eq 2)
{"This is an Alapha computer"}
elseif($wmi.architecture -eq 3)
{"This is an PowerPC computer"}
elseif($wmi.architecture -eq 6)
{"This is an IPF computer"}
elseif($wmi.architecture -eq 9)
{"This is an x64 computer"}
else
{$wmi.architecture + " is not a cpu type I am familiar with"}
"Current clockspeed is : " + $wmi.CurrentClockSpeed + " MHZ"
"Max clockspeed is : " + $wmi.MaxClockSpeed + " MHZ"
"Current load percentage is: " + $wmi.LoadPercentage + " Percent"
"The L2 cache size is: " + $wmi.L2CacheSize + " KB"

DisplayComputerRoles.ps1

# This is a example taken from the book "Microsoft Windows Powershell Step by step"




$wmi = get-wmiobject win32_computersystem
"computer " + $wmi.name + " is: "
switch ($wmi.domainrole)
{
0 {"`t Stand alone workstation"}
1 {"`t Member workstation"}
2 {"`t Stand alone server"}
3 {"`t Member server"}
4 {"`t Back up domain controller"}
5 {"`t Primary domain controller"}
default {"`t The role can not be determined"}
}

Sunday, October 11, 2009

RTR record keeping application

Just a small RTR record keeping application. It uses xml. You just need to download and run it. Thats all. Built on C# 2008.

Download it from:-

http://www.esnips.com/doc/23b9e54d-e3ec-4e14-bb85-72d81523beee/RTRApplication1

Wednesday, October 7, 2009

Function to test whether 2 array of objects are equal or not.

public static bool AreEqual(object[] o1, object[] o2)
{
if (o1 == null && o2 == null)
{
return true;
}
if (o1 == null || o2 == null)
{
return false;
}

if (o1.Length == o2.Length)
{
int i;
bool eq = true;
for (i = 0; i
{
if (AreNotEqual(o1[i], o2[i]))
{
return false;
}
}

return eq;
}

return false;
}

Function to test whether 2 objects are equal or not.

public static bool AreEqual(object o1, object o2)
{
if (o1 == null && o2 == null)
{
return true;
}
if (o1 == null || o2 == null)
{
return false;
}
if (o1.GetType().FullName != o2.GetType().FullName)
{
return false;
}
return o1.Equals(o2);
}


//ofcourse Are not equal can be done just by negating the above function

public static bool AreNotEqual(object o1, object o2)
{
return !AreEqual(o1, o2);
}

Saturday, October 3, 2009

Aliases in PowerShell

Name Definition
ac Add-Content
asnp Add-PSSnapin
clc Clear-Content
cli Clear-Item
clp Clear-ItemProperty
clv Clear-Variable
cpi Copy-Item
cpp Copy-ItemProperty
cvpa Convert-Path
diff Compare-Object
epalExport-Alias
epcsvExport-Csv
fcFormat-Custom
flFormat-List
foreachForEach-Object
%ForEach-Object
ftFormat-Table
fwFormat-Wide
galGet-Alias
gcGet-Content
gciGet-ChildItem
gcmGet-Command
gdrGet-PSDrive
ghyGet-History
giGet-Item
glGet-Location
gmGet-Member
gpGet-ItemProperty
gpsGet-Process
groupGroup-Object
gsvGet-Service
gsnpGet-PSSnapin
guGet-Unique
gvGet-Variable
gwmiGet-WmiObject
iexInvoke-Expression
ihyInvoke-History
iiInvoke-Item
ipalImport-Alias
ipcsvImport-Csv
miMove-Item
mpMove-ItemProperty
nalNew-Alias
ndrNew-PSDrive
niNew-Item
nvNew-Variable
ohOut-Host
rdrRemove-PSDrive
riRemove-Item
rniRename-Item
rnpRename-ItemProperty
rpRemove-ItemProperty
rsnpRemove-PSSnapin
rvRemove-Variable
rvpaResolve-Path
salSet-Alias
sasvStart-Service
scSet-Content
selectSelect-Object
siSet-Item
slSet-Location
sleepStart-Sleep
sortSort-Object
spSet-ItemProperty
sppsStop-Process
spsvStop-Service
svSet-Variable
teeTee-Object
whereWhere-Object
?Where-Object
writeWrite-Output
catGet-Content
cdSet-Location
clearClear-Host
cpCopy-Item
hGet-History
historyGet-History
killStop-Process
lpOut-Printer
lsGet-ChildItem
mountNew-PSDrive
mvMove-Item
popdPop-Location
psGet-Process
pushdPush-Location
pwdGet-Location
rInvoke-History
rmRemove-Item
rmdirRemove-Item
echoWrite-Output
clsClear-Host
chdirSet-Location
copyCopy-Item
delRemove-Item
dirGet-ChildItem
eraseRemove-Item
moveMove-Item
rdRemove-Item
renRename-Item
setSet-Variable
typeGet-Content

Node.JS rest api Tutorials