ATTENTION ALL FANS!!! THIS BLOG HAS MOVED!!!
go to: http://www.taotekaching.com

Tuesday, July 22, 2008

A Programming Job Interview Challenge #13 - Brackets, and Me...

Ok, I've been doing the programming quizzes here and have a solution...

In Perl:


$FILE = "expressions.txt";
open(FILE) or die("Could not open expressions file.");
foreach $OLINE (<FILE>)
{
$LINE = $OLINE;
if ($LINE =~ m/(^[\]}\)>])|([\[{\(<]$)/)
{ print "bad : $OLINE" }
else
{
while ($LINE =~ m/(\[\])|({})|(\(\))|(<>)/)
{ $LINE =~ s/(\[\])|({})|(\(\))|(<>)//g }
$strLen = length($LINE);
if ($strLen > 1)
{ print "bad : $OLINE" }
else
{ print "good : $OLINE" }
}
}

And a C# console app to generate the "sample" strings (i.e. the "expressions.txt" file used above):




using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Creator();
//Console.ReadKey();
}
static void Creator()
{
Random r = new Random((DateTime.Now.DayOfYear + DateTime.Now.Millisecond) * (DateTime.Now.Second + DateTime.Now.Minute + 1));
string encs = "[{(<>)}]";

string sb = "";
for (int i = 0; i < 100; i++)
{
sb = "";
bool good = (r.Next(0, 100) > 50) ? true : false;
int strlen = r.Next(1, 21) * 2;
for (int j = 0; j < strlen; j++)
{
if (!good)
sb = String.Format("{0}{1}", sb, encs[r.Next(0, encs.Length)]);
else
{
int p = r.Next(0, encs.Length / 2);
int q = r.Next(0, 3);
if (q == 0)
sb = String.Format("{0}{1}{2}", encs[p], sb, encs[encs.Length - p - 1]);
else if (q == 1)
sb = String.Format("{0}{1}{2}", sb, encs[p], encs[encs.Length - p - 1]);
else
sb = String.Format("{0}{1}{2}", encs[p], encs[encs.Length - p - 1], sb);
}
}
//Console.WriteLine(String.Format("{0}: {1}", good, sb.ToString()));
Console.WriteLine(sb);
}
}
}
}

I couldn't spend that much time on this, as I am at work. Ergo, I'm a little disappointed with the Perl, although it was my first Perl program ever!


I'm particularly curious about how I could have done some sort of recursive check. Please give any suggestions to the Perl script that would make it as close to a one-liner regex check.


~simon

Submit this story to DotNetKicks

Wednesday, July 09, 2008

Conway's Game of Life, Java, Smoking, and Me...

Ok, I've done it again. Another Conway's Game of Life, this time in Java:

The source code can be had here. It's called Gola (Game Of Life Applet).

So, I've quit smoking. I started July 6 at 11:27pm (my last cigarette). It is going much better this time.  This is my third time.  The first two times I quit for 2+ years each time.  I know this time is my final one.  Why?  I had a "moment" where I just didn't want them anymore.

That feeling stayed, even after I failed, so I set a quit date, got the patch, got lots of pretzel sticks, and made sure I didn't sit around and drink coffee in the morning.  This is the third day, and I had to stay home today.  I felt like I had the flu and slept until 1:00pm.  I've been a vegetable and sleeping on and off since.  It's 5:33pm now, and I'm finally getting a little energy back.  I wrote a little C# program for watching the "progress" of quitting.  I was going to include the health benefits stages as stages that would appear below the timer, but I slacked.  Here's the code and benefits list.

Submit this story to DotNetKicks

Thursday, July 03, 2008

Conway's Game of Life in Javascript, and Me...

Ok, I went and done it...

With my inability to purge my rotting brain of Conway's Game of Life, I have produced none-other than a super double-buffered javascript only Conway's Game of Life!

Hopefully I can sleep now. Below is it in all it's glory. You can get the javascript to run it here.

NOTE: ARRRRRRGGGGHHH!!! Ok, trying to get javascript to run in blogger, but it'll take a little work. Here's so you can see it works

ADDENDUM: In Blogspot, IE doesn't like this script so well, nor does Firefox. I've removed it from the page but you can still see it at the link above.

ADDENDUM REDUX: Well, IE does not like this script at all. Performance is lousy and display is f-ed up. Firefox runs it like a champ.

Submit this story to DotNetKicks