Wednesday 13 June 2007

TopCoder

Distancs between 2 points in space:

double distance3d(int x1, int y1, int z1, int x2, int y2, int z2)
{
int x = x1 - x2, y = y1 - y2, z = z1 - z2;
return Math.Sqrt(x * x + y * y + z * z);
}

double distance2d(int x1, int y1, int x2, int y2)
{
int x = x1 - x2, y = y1 - y2;
return Math.Sqrt(x * x + y * y);
}

Triangle area:

double triangleArea(double a, double b, double c)
{
double eps = 1.0e-9;
double s = (a + b + c) / 2.0;
if (s - a <= 0 || s - b <= 0 || s - c <= 0 ) return 0;
if (Math.Abs(a + b - c) < eps || Math.Abs(b + c - a) < eps || Math.Abs(a + c - b) < eps) return 0;
return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
}

The Fibonacci number on position pos
double fibonacci(double pos)
{
return (Math.Pow(1 + Math.Sqrt(5),
pos) - Math.Pow(1 - Math.Sqrt(5), pos)) / (Math.Pow(2, pos) * Math.Sqrt(5));
}


More: http://mathworld.wolfram.com/

Tuesday 5 June 2007

Fast searching for strings in files.

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
[/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
strings [[drive:][path]filename[ ...]]

/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename
Specifies a file or files to search.

Monday 14 May 2007

Deadlock tracing

To trace deadlocks and report it to sql server ERRORLOG:
DBCC TRACEON (3604) -- report to log file
DBCC TRACEON (1204) -- trace deadlock

To switch it off:
DBCC TRACEOFF (3604)
DBCC TRACEOFF (1204)

To check which flags are on:
dbcc tracestatus(-1)

To cycle sql error log:
sp_cycle_errorlog

Wednesday 2 May 2007

Change type of a replicated column in SQL Server

I've created stored procedure to siplify type change of the column which is being replicated.

CREATE PROCEDURE dbo.uspChangeReplColType
@tblName SYSNAME,
@colName SYSNAME,
@newType VARCHAR(20),
@repName VARCHAR(20)
AS

DECLARE
@colNameTmp SYSNAME,
@ret INT

--SET @tblName = 'tbMessages'
--SET @colName = 'To'
--SET @newType = 'VARCHAR(20)'
--SET @repName = 'SomeRepl'
SET @colNameTmp = @colName + right(cast(rand() as varchar(30)),5)
SET @ret =0

BEGIN TRAN

PRINT('1/6 Tmp column adding ...')
EXEC('alter table [' + @tblName +'] add [' + @colNameTmp + '] '+ @newType + ' NULL')
IF @@error <> 0 OR @ret <> 0 goto FAILURE
PRINT('1/6 Tmp column added')

PRINT('2/6 Tmp column populating ...')
EXEC('update [' + @tblName + '] set [' + @colNameTmp + '] = [' + @colName +']')
IF @@error <> 0 OR @ret <> 0 goto FAILURE
PRINT('2/6 Tmp column populated')

PRINT('3/6 Old column dropping ...')
EXEC @ret = dbo.sp_repldropcolumn
@source_object = @tblName, @column = @colName;
IF @@error <> 0 OR @ret <> 0 goto FAILURE
PRINT('3/6 Old column droped')

PRINT('4/6 New column adding ...')
EXEC @ret = sp_repladdcolumn @source_object = @tblName
, @column = @colName
, @typetext = @newType
, @publication_to_add = @repName
IF @@error <> 0 OR @ret <> 0 goto FAILURE
PRINT('4/6 New column added')

PRINT('5/6 New column updating ...')
EXEC('update [' + @tblName + '] set [' + @colName + '] = [' + @colNameTmp + ']')
IF @@error <> 0 OR @ret <> 0 goto FAILURE
PRINT('5/6 New column updated')

PRINT('6/6 Tmp column dropping ...')
EXEC('alter table [' + @tblName + '] drop COLUMN [' + @colNameTmp + ']')
IF @@error <> 0 OR @ret <> 0 goto FAILURE
PRINT('6/6 Tmp column droped')

COMMIT
return(0)

FAILURE:
if @@trancount = 1 ROLLBACK
return (1)
GO