Showing posts with label quiz. Show all posts
Showing posts with label quiz. Show all posts

Thursday, April 16, 2009

my solution to Eric Lippert's quiz

Eric posted a quiz a few days ago, it generated quite a number of responses, so I thought I would answer with the shortest possible C# answer (with my previous team we used to kinda compete on refactoring), so here it is (Justin and Paul, bring it on!):
The problem:
Write me a function that takes a non-null IEnumerable and returns a string with the following characteristics:

(1) If the sequence is empty then the resulting string is "{}".
(2) If the sequence is a single item "ABC" then the resulting string is "{ABC}".
(3) If the sequence is the two item sequence "ABC", "DEF" then the resulting string is "{ABC and DEF}".
(4) If the sequence has more than two items, say, "ABC", "DEF", "G", "H" then the resulting string is "{ABC, DEF, G and H}". (Note: no Oxford comma!)


My solution:

static string JoinStrings(IEnumerable<string> strings) {

int len = strings.Count();

return "{"+(

(len > 1) ?

strings.Take(len - 1)

.Aggregate((string head, string tail) => head+", "+tail)+

" and " +strings.Last()

: (len == 1) ?

strings.First()

: "")+

"}";

}

Saturday, September 27, 2008

string format gotcha

here's a simple, inoffensive looking piece of code:

...
LogError(string.Format("some message, some variable value {0}" + ex.Message, varValue));
...

Can you spot the problem with this code?

Thursday, January 24, 2008

Puzzle

Justin came up with this problem as part of an actual project and it puzzled me the simplicity of the problem and definition of the solution in your head, yet the complexity of the expressiveness in code for the solution, maybe another language would be better suited to solve this problem?

you have an array of the Doc class which has a DocIndex property, for which the property is not set, some elements of the array may be null

//*** small version of the Doc class, just for the puzzle
class Doc {
public int docIndex;
}

Doc[] docs = new Doc[] { new Doc(), new null, null, new Doc() };

then you have an array of ints that represent the indexes that are already taken, e.g.

new int[] { 2, 3, 5, 7, 9 }

your job is to assign the indexes to the Doc[] that are not "taken" (exist in the array)

so, for these examples:



The expected output would be:
existing :
2 3 5 7 9
docs :
0 1 4 6 8

existing :
0 1 2 3 4
docs :
5 6 7 8 9

existing :
5 6 7 8 9
docs :
0 1 2 3 4

what do you think of this problem for a code interview?

Saturday, June 09, 2007

C# Quiz #1: overloads, strings, nullable types

*This is also a test of the latest Windows Live Writer

given this class definition:

public class Foo {
public void Bar(string x) {
Console.WriteLine("string x was called");
}
public void Bar(int? x) {
Console.WriteLine("int? x was called");
}
}

what will be output to the console with the following code:

Foo f = new Foo();
try {
f.Bar(null);
}
catch {
Console.WriteLine("no method was called");
}

*update: I messed up the code while trying to upload another post, I recovered the original post from my RSS feed