So - We have a code Kata email every friday - usually posed by the boolean frog (pascal.nationality == french)
I think the interaction on our list around these is awsome so I think it is worth sharing - and here is why
most Friday's Pascal posts a problem and we all attempt to solve it - Mostly in C Sharp but sometimes in F sharp ruby etc (Am on a mac and do you think I can find the effing sharp key :-( )
Anyway
This weeks puzzle was
Excel can solve this Kata very easily, wonder how easy it is in C#/Ruby/F#/JS.
Shortest (correct) answer wins!
/*
Given a list of nullable decimals, calculate the average of each consecutive pair of decimal values.
Example:
{6, 2, 0, 4, 3, null, 2, 6, 8, 4} => {4, 2, 1.5, 4, 6}
{6, 2, 0, 4, 3, null, 2, 6, 8, 4, 99} => {4, 2, 1.5, 4, 6, 99}
*/
void Main()
{
var rand = new Random();
int max = rand.Next(0, 48);
IEnumerable serie = Enumerable.Range(1, max).Select (i => (decimal?)rand.Next(0, 10));
var result = CalculateAverages(serie);
result.Dump();
}
decimal[] CalculateAverages(IEnumerable serie)
{
// YOUR CODE HERE
return new decimal[]{};
}
So the question is how do you solve it??
Being the resident ruby geek my solution was - ahem - in ruby
there were some nice other solutions but how do you solve it???
Before you try some things to note
- Null values are treated as 0 in the list passed in as parameters.
- The list passed in can be made of either integers, decimals (floats in Ruby) or null values.
- If all values are null, then 0 will be the average for all 2 consecutive value pairs.
- The list can have 0, 1 or more numbers.
- The list can have either an even or odd count.
- The result should not contain any null values.
I'll post some of the answers here tomorrow - But if you want to try to win (Remember the shortest answer to the Calculate average method is what we are after) then feel free to leave a comment and if you win you will get - ummm - some kudos - -)
good luck and answers here (Not quite the quoted tomorrow - Ah well )
Mal
5-08-2011