Register | Login

Stacking Code

public interface IBlog { string Dump(Stream consciousness); }

Dave Thomas' Code Kata 4, Part One

Friday, 25 February, 2011 @ 7:39 PM < Adam Boddington
Tags: C#, Kata, Linq

A team of Microsoft developers in IT were recently challenged by their solution architect to complete Part One of Dave Thomas' Code Kata 4. Not having done one of these before, I thought I'd join in without being invited. ;)

I'm not exactly sure what Dave is asking for in this kata. It sounds to me like he just wants to find the day with the smallest temperature range. Talking to some of the other developers, however, some think he's after all the temperature ranges. Ah the joy of written specifications... If only we could talk to the client.

In the end I spent two hours putting my solution together. Only ten minutes of that was spent writing and testing code for the problem. I spent the rest of the time wrapping it up in a console application, adding options, writing build scripts, and throwing in some shortcut PowerShell scripts. I think I may have gone overboard. :(

But here's my answer (the relevant part).

[FixedLengthRecord(FixedMode.AllowMoreChars), IgnoreFirst(4)]
public class WeatherDay
{
    [FieldFixedLength(4)]
    public int Day;

    [FieldFixedLength(6), FieldTrim(TrimMode.Right, ' ', '*')]
    public decimal MaximumTemperature;

    [FieldFixedLength(6), FieldTrim(TrimMode.Right, ' ', '*')]
    public decimal MinimumTemperature;

    public decimal TemperatureRange
    {
        get { return MaximumTemperature - MinimumTemperature; }
    }
}

In my tiny model I'm using Marcos Meli's FileHelpers to describe the data I'm interested in. In the console application, I just need to read the file with a FileHelperEngine, run some Linq, and I'm virtually done.

private void Execute()
{
    var file = new FileHelperEngine<WeatherDay>();
    IEnumerable<WeatherDay> weatherDays = file.ReadFile(_path, 30).AsEnumerable();

    WeatherDay weatherDay = weatherDays
        .OrderBy(wd => wd.TemperatureRange)
        .First();

    IEnumerable<WeatherDay> similarWeatherDays = weatherDays
        .Where(wd => wd.Day != weatherDay.Day)
        .Where(wd => wd.TemperatureRange == weatherDay.TemperatureRange);

    Console.WriteLine();
    Console.WriteLine("                 Rows Read: {0}", weatherDays.Count());
    Console.WriteLine("Smallest Temperature Range: {0}", weatherDay.TemperatureRange);
    Console.WriteLine("Day of Smallest Temp Range: {0}", weatherDay.Day);
    Console.WriteLine("   Days w/ Same Temp Range: {0}", string.Join(", ", similarWeatherDays.Select(wd => wd.Day).ToArray()));
    Console.WriteLine();
}

The end result...

Weather Days

It feels a little like cheating pulling in FileHelpers, but it didn't make sense to me parsing out weather.dat myself. I hope my interpretation of the requirements was correct.

There are 0 comments.


Comments

Leave a Comment

Please register or login to leave a comment.


Older
Running a Mercurial Server on IIS 7.5

Newer
Windows Installer Woes

Older
Running a Mercurial Server on IIS 7.5

Newer
Windows Installer Woes

browse with Pivot


About


Projects

Building Neno


RSS
Recent Posts

Codility Nitrogenium Challenge
OS X Lock
HACT '13
Codility Challenges
Priority Queue


Tags

Architecture (13)
ASP.NET (2)
ASP.NET MVC (13)
Brisbane Flood (1)
Building Neno (38)
C# (4)
Challenges (3)
Collections (1)
Communicator (1)
Concurrency Control (2)
Configuration (1)
CSS (5)
DataAnnotations (2)
Database (1)
DotNetOpenAuth (2)
Entity Framework (1)
FluentNHibernate (2)
Inversion of Control (5)
JavaScript (1)
jQuery (4)
Kata (2)
Linq (7)
Markdown (4)
Mercurial (5)
NHibernate (20)
Ninject (2)
OpenID (3)
OS X (1)
Pivot (6)
PowerShell (8)
Prettify (2)
RSS (1)
Spring (3)
SQL Server (5)
T-SQL (2)
Validation (2)
Vim (1)
Visual Studio (2)
Windows Forms (3)
Windows Service (1)


Archives


Powered by Neno, ASP.NET MVC, NHibernate, and small furry mammals. Copyright 2010 - 2011 Adam Boddington.
Version 1.0 Alpha (d9e7e4b68c07), Build Date Sunday, 30 January, 2011 @ 11:37 AM