99 Problems part 3

I’ve finally got around to doing some more of the fun (??) “99 Problems”. I’m using the Scala version of the problems but am solving them in C#.

Problems 13 through to 17 are now done. All fairly simple (probably due to my naive solutions).

Problem 13 was of particular interest with the problem requiring some run length encoding to be done. The problem stated is, given a list of ‘a’,’a’,’a’,’a’,’b’,’c’,’c’,’a’,’a’,’d’,’e’,’e’,’e’,’e’  calling the solution should result in an answer of 4:’a’, 1:’b’, 2:’c’, 1:’d’, 4:’e’  (ie the # of times the letter appears contiguously.

Unless the LINQ solution is obvious to me, I prefer to code up a naive foreach/while answer, confirm I understand the problem then “LINQ-ify” it. (I’ve found that so far most of the 99 problems are better solved with LINQ). My naive solution was:

 

var res = new List<Tuple<int, char>>();

var count = 0;
char ch = ' ';

foreach (var i in l)
{
if (i != ch)
{
if (ch == ' ')
{
ch = i;
count = 1;
}
else
{

res.Add(new Tuple<int, char>(count, ch));
ch = i;
count = 1;
}
}
else
{
count++;
}

}
res.Add( new Tuple<int, char>(count,ch));

Yes, it could be shortened but you get the idea. Loop through, keeping track of last char etc. Nothing earth shattering here. Once I’d completed this version I tried for ages to come up with something that was shorter and LINQ based. One thing I did NOT want to do though is use LINQ but end up with (to me) unreadable/confusing code.

Simply, I could not. If anyone else can, I’d be interested in hearing about it.

btw, looking on StackOverflow I found an answer by Jon Skeet where he basically did the same as I did (but more elegantly). So am thinking maybe there is no neat LINQ way?

Advertisements

AzureCopy 0.15 out!

Another day another version of AzureCopy is out (usual github, nuget, command links)

This version has dependencies updated to the latest and greatest (AWS Client lib, Azure Storage lib etc). One drawback to the updating of the AWS client lib is that now whenever we’re copying to/from S3 we need to supply the AWS Region the account is linked too. I haven’t found a way to programmatically do that (yet) so for now the regions have to be entered into the App.config (or equiv).

 

So 3 new paramers:

AWSRegion (eg. “us-west-1”).

SrcAWSRegion (source specific version)

TargetAWSRegion (target specific version).

 

I’ll try and remove the need to know this as soon as I can.

 

The other change is that copying between different Azure accounts now works properly (had broken at some stage). So now transferring between accounts is nice and quick.