--On the drawing of teddy-bears
/*
I have been gently reproached for making my blog too developer-oriented, and geeky; thereby making managers feel neglected. We therefore introduce Teddy, our foray into unashamed nostalgia in the days of 80x25 character terminals. In those days, immense effort was expended in drawing pictures in this unpromising medium
*/
Create procedure spTeddy
@teddy varchar(8000)=null output as
Select @teddy='
(.)_(.)
{ . . }
\_*_/
/ . . \
{_} {_}
| |
(_}- -{_}
'
go
--which we can then display with....
Declare @teddy varchar(8000)
Execute spTeddy @teddy=@Teddy output
Select @Teddy
/* at this point I was going to introduce a competition to turn teddy upside down, but in experimenting with the concept I decided it was far too simple for our razor-sharp audience.
You may think... */
Declare @teddy varchar(8000)
Execute spTeddy @teddy=@Teddy output
Select reverse(@Teddy)
/* will do the trick. Well almost, but the results need tidying up
So we introduce a helping function that reverses some of the characters*/
Create function dbo.ufsReplaceChars
(
@String varchar(8000),--the string within which you want the characters substituted
@change varchar(200), --the characters
@ReplaceWith Varchar(200)--the substitutes
)
RETURNS varchar(8000) AS
BEGIN
Declare @ModifiedString Varchar(8000)
if len(@replaceWith)<Len(@Change)
Select @Change=@Change+space(Len(@Change)-len(@replaceWith))
IF (coalesce(@String,'') ='' )
select @ModifiedString=@String
else
WHILE len(@string)>0
IF CHARINDEX (substring (@String,1,1), @change) = 0
BEGIN
SELECT @ModifiedString = coalesce(@ModifiedString,'')
+substring (@String,1,1)
select @String=substring (@String,2,len(@String)-1)
END
else
begin
SELECT @ModifiedString = coalesce(@ModifiedString,'')
+substring (@ReplaceWith,CHARINDEX (substring (@String,1,1), @change),1)
select @String=substring (@String,2,len(@String)-1)
end
RETURN (@ModifiedString)
END
Declare @teddy varchar(8000)
Execute spTeddy @teddy=@Teddy output
Select replace(dbo.ufsReplaceChars(reverse(@Teddy),'_)({}.','¯()}{''')
,char(10)+char(13),'
')
/*
{¯}- -{¯)
| |
{¯} {¯}
\ ' ' /
/¯*¯\
{ ' ' }
(')¯(')
All very satisfying.
ASCII art is one of the more esoteric parts of Computer science. Since Wikipedia has covered the subject in excellent detail, I need only refer you there http://en.wikipedia.org/wiki/ASCII_art. Someone has even recoded the entire Matrix movie as ASCII.
You'll wonder, as I did, whether one could introduce some compression into the storage of ASCII art. Some ASCII art can get pretty big in size.
Then I got to thinking that I hadn't actually seen a good string compression function around for SQL server
ASCII art generally uses a tiny subset of the ASCII range so storing it in standard ASCII is very uneconomic. . In fact the Teddy is only 14 characters
*/
character freq.
---------- -----------
' ' 111
char(10) 9
char(13) 9
'_' 7
'.' 6
'}' 5
'{' 4
'(' 3
')' 2
'/' 2
'\' 2
'|' 2
'-' 2
'*' 1
/* we can determine this using another helper function that looks at the character count */
create FUNCTION dbo.uftvCharacterFrequency
/* produces a character count of all the characters in a string
as a frequency table*/
(
@String varchar(8000)
)
RETURNS
@Results TABLE
(
[character] varchar(10),
[freq.] int
)
AS
begin
declare @ii int
Declare @iiMax int
Declare @temp table (Thechar char(1))
Select @ii=1, @iiMax=len( @String)
while @ii<=@iiMax
Begin
insert into @temp select substring( @String,@ii,1)
Select @ii=@ii+1
end
insert into @Results
select [character]=
case
when ascii(TheChar)>=32 then ''''+TheChar+''''
else 'char('+convert(Varchar(3),ascii(TheChar))+')' end,
[freq.]= count(*) from @temp group by theChar order by count(*) desc
return
end
/* we can then see what the character frequency of Teddy is with the following code */
Declare @teddy varchar(8000)
Execute spTeddy @teddy=@Teddy output
Select * from dbo.uftvCharacterFrequency(@Teddy)
/* So The competition here is to provide the best compression/Decompression algorithm for reducing the storage requirements for typical ASCII art such as teddy.
This doesn't have to be complex. Even replacing every group of five spaces with a special character reduces Teddy's storage by 38.8%
*/