##
Introduction

###
What is Code Hunt?

On May 15th, as seen

on Reddit the Microsoft Research Team

announced Code Hunt, an online "game" that claims to help you learn to code. Not too sure about that, but it is certainly an interesting way to pass the time and may even teach you a few things along the way. Inevitably, though you get stuck and need hints.

###
This post

The purpose of this post is **not** to give you all the answers to Code Hunt. If you're looking for that, you're in the wrong place! What it will do is explain how to find all the answers and why your answers are not optimal. With the information given here, you should be able to receive full marks **and** learn something along the way. We only use C# here, but the information can equally be applied to Java.

###
Resources

You may find use for:

##
Walkthrough

###
Sign up

First things first, sign up and log in to ensure that you keep your progress between sessions.

###
General approach

The problems do not come with any specification of what the tested method should achieve, so the first thing is to reverse engineer the specification from the input->output expectations presented by the system. One approach to achieving this is to use concatenated ternary operators using the tested input parameters as follows:

Once you have reverse engineered the method's intended purpose, you should apply documentation comments with triple-slashes. Always document your code!

Finally, implement the function as efficiently as you can:

If Microsoft agree that your code is both correct and succinct, you get maximum points!

###
00 Training

####
00.02 Inky fingers

Look at the input value. Look at the output value. Not much difference is there?

####
00.03 Doubly tricky

Output is a multiple of input.

####
00.04 Nonplussed?

Output combines inputs.

###
01 Arithmetic

####
01.01 Are you positive?

Output is a simple function of input.

####
01.02 A bit more different

Add a constant to the input value.

####
01.03 Don't be so square

A higher order function.

####
01.04 Magic numbers

A lower order function

####
01.05 Divide and conquer

Magic!

####
01.06 More over?

Sounds like...

####
01.07 What's the difference?

Why fewer?

####
01.08 Why try more?

...than x. First order.

####
01.09 You star!

Go forth...

####
01.10 Tricky

The first one, the second third.

####
01.11 Get this one over with

The other goes down under.

####
01.12 A modern approach

Read up on the modulo operator (the percent sign).

####
01.13 Even more modern

Similar to the previous puzzle. But more. so.

####
01.14 WHAT?!

I must confess, originally I only managed to get the following:

It then took a LOT of staring to realise that this was some sort of modulo, but I didn't get it. Thanks and kudos go to MegjelenőNév for pointing out that this one requires x both before AND after the modulo. Spoiler: answer in comments below.

####
01.14 Of Average difficulty

You know what I mean.

###
02 Loops

Now the loops can all be achieved using regular loops, but in this modern age of LINQ, who uses loops? I have provided all my Linq-based answers. Please work out WHY these are correct (though not necessarily the highest-scoring) answers...

####
02.01 Simple Range

Read up on The LINQ Range method. It may not do what you think!

####
02.02 Squares

####
02.03 More Squares

####
02.04 Add 'em up

This one is a little weird and requires a pair of casts...

####
02.05 One fewer squares

####
02.06 Count the a's

####
02.07 Count the input chars

###
03 Loops 2

####
03.01 Ternary operator

For this one, I AM going to directly give you the right answer, and it's a single line. Read up on the C# ternary operator to find out how this works...

return power==0?1:power==1?number:number*Puzzle(number, power-1);

Here's how to read the above...

Return the following value:

- If power is 0 then 1 otherwise
- If the power is 1 then number otherwise
- The number multiplied by a recursive call to this function against the number but for one lower power.

####
03.02 LINQ Aggregate

For this one, again, I am going to give you the answer. Read up on the LINQ Aggregate function, which recursively multiplies. You should then be able to see why this is an acceptable solution:

return Enumerable.Range(1,i).Aggregate(1, (x,y) => x * y);

####
03.03 More Range, More Aggregate

Now that you have the answer to 03.02, you should be able to solve 03.03. Of course, you read up on exactly how LINQ Range works didn't you? No? Shame! Go read up on EXACTLY what each term does.

Now, can you explain why this works?:

return Enumerable.Range(lowerBound,upperBound-lowerBound).Aggregate(upperBound, (soFar,newValue) => soFar * newValue);

####
03.04 Wot no LINQ?

No LINQ needed for this one really... Half one fewer by half one more.

####
03.05 Non-optimal LINQ

Enumerable.Range(1, upperBound).Sum(a=>a%2==upperBound%2?a*a:0);

Now make it optimal.

####
03.06 LINQ Repeat

Here's part of the answer. Now if only there were a way of JOINing STRINGs...

Enumerable.Repeat('_', word.Length)

####
03.07 Casting to int

####

Here's a framework.

return new string(s.ToCharArray().Select(c=>(char)(MATHS!).ToArray());

Hint: you can cast a char as an int.

####
03.08 I am not a number!

I'm a free string!

####
04 Conditionals

####
04.01 OR

####
04.02 AND

####
04.03 LESS THAN

####
04.04 LESS THAN again

####
04.05 i<0?-1MYSTERYCHARACTERi>0?1:0

####
04.06 MORE THAN

####
04.07 i<WHAT?NUM:ANOTHERNUM

####
04.08 Modulo ternary string / string

####
04.09 (i%NUM!=0?STRING1:STRING2) + STRING3

One is empty - which one?

####
04.10 Similar to the previous

####
04.11 Redouble?

####
04.12 i<NUM1?NUM2:i<NUM3?NUM4:NUM5

####
05 Conditionals 2

####
05.01 Oh, simple ternary

####
05.02 LINQ distinct count

####
05.03 What sort of triangle?

####
05.04 Show us those abdominals!

####
05.05 Square equality

###
06 Strings

####
06.01 I did not write elegant code.

But then I didn't need to!?

####
06.02 Non-optimal - make it better

####

var charArray = s.ToCharArray();

return Enumerable.Range(0,charArray.Length).Select(n=>n%2==0?charArray[n].ToString().ToUpper():charArray[n].ToString()).Aggregate((a,b)=>a+b);

####
06.03 Reverse, split, uppercasefirst, join, reverse

####
06.04 s at x

####

####
06.05 one plus two

####
06.06 second half

####
06.07 upper lesser second half + second half

####
06.08 which is longer?

####
06.09 the longer or both

####
06.10 third

####
06.11 WHAT?! again

See Andrew Furdell's hint below.

####
06.12 string + reverse

###
07 Strings 2

####
07.01 two three one one three two

####
07.02 first half

####
07.03 replace b with c

####
07.04 reverse

####
07.05 stringbuilder every other use mod

####
07.06 use string.Replace()

####
07.07 Vwls r bd m'ky?

####
07.08 Everything seems to be in order

Use IndexOf

####
07.09 Enumerable Repeat string Join

Do it all in one line

####
07.10 Hmm

I have the following:

var alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";

return string.Join(" ",Enumerable.Range(0,t).Select(n=>n!=t-1?alphabet:alphabet.Substring(0,50-n*2) + "z").ToArray());

Now how to improve?

###
08 Nested Loops

OK, so everyone can write nested for loops. But we're better than that now aren't we? ;-) Unfortunately, these efforts are not appreciated by Microsoft so most of these are considered non-optimal answers. IF that is the case, there is something wrong either with their compiler optimisations, or with their code evaluation algorithm.

####
08.01 Factorial sums

Now Microsoft have just got this one plain wrong. The following code is a quite reasonable attempt to sum the factorials between i and j:

return Enumerable.Range(i, j-i+1).Sum(x=>Enumerable.Range(1,x).Aggregate((a, b) => b * a));

However, this get rejected for two really weird cases:

For input values of i=12, j=13, Microsoft give the answer -1883912192. Clearly wrong. 12! + 13! is 6706022400.

For input values of i=3, j=64, Microsoft gives the answer null. Null?! Null is not even a legitimate return value. Don't bother with this one, it can't be done.

UPDATE: Kudos to MW, who (SPOILER) has given a full 3-star answer in the comments, below.

####
08.02 Hashes

This one is easy to do with nested loops or new string('#', x), but can you do it with LINQ?
Here's the kind of code you should be writing by now...:

return string.Join("", Enumerable.Range(1,n).Select(a=>new string('#', a) + " "))

####
08.03 Alpha underscores

Easy to see what they are wanting here, and easy to lazily implement in for loops.

Microsoft didn't give full marks to the following, so I don't feel bad about sharing the following:

return string.Join("", Enumerable.Range(0,n+1).Select(a=>string.Join("",Enumerable.Range(0,n+1).Select(x=>x>a?"_":x.ToString())) + " "));

####
08.04 Dash on N

More LINQ awesomeness:

return string.Join(" ",Enumerable.Range(1,n).Select(a=>string.Join("", Enumerable.Range(1,n).Select(y=>y!=a?b:"-"))));

####
08.05 Underscore at N

Blah blah LINQ blah:

return string.Join(" ",Enumerable.Range(0,a.Length).Select(b=>string.Join("", Enumerable.Range(0,a.Length).Select(y=>y!=b?a.Substring(y,1):"_"))));

####
08.06 Have LINQ hammer. Everything is a nail.

return string.Join(" ",Enumerable.Range(1,size).Select(b=>string.Join("", Enumerable.Range(1,size).Select(y=>b!=1&&b!=size&&y!=1&&y!=size?"_":"$"))));

####
08.07 LINQ -> Noughts and Crosses

return string.Join(" ",Enumerable.Range(0,height).Select(heightPos=>string.Join("", Enumerable.Range(0,width).Select(widthPos=>(widthPos+heightPos)%2==0?"x":"o"))));

####
08.08 OK, I admit, not pretty, but I'm committed to it now...

I will tidy this one up later with Math.Abs()...

return

string.Join(" ",Enumerable.Range(1,number).Select(index=>string.Join("", Enumerable.Range(0,index).Select(a=>index.ToString()))))

+ " " +

string.Join(" ",Enumerable.Range(1,number-1).Select(index=>string.Join("", Enumerable.Range(0,number - index).Select(a=>(number-index).ToString()))));

###
09 1D Arrays

###

####
09.01 list at i

###

####
09.02 First and last

###

####
09.03 LINQ Contains()

###

####
09.04 Enumerable.Range

###

####
09.05 Enumerable.Range again

###

####
09.06 And again...

####
09.06 And again...

####
09.07 ToCharArray then LINQ then back to String

####

####
09.08 Enumerable.Range again

####

####
09.09 Select a=> -a

####
09.10 Array.Reverse()

####
09.11 To CharArray, Reverse and Join

####
09.12 Buggy test?

My answer here is

return Enumerable.Range(numbers.Length-amount, numbers.Length).Select(a=>numbers[a%numbers.Length]).ToArray();

This results in...

I have NO idea.

UPDATE: Kudos to MW, who (SPOILER) has given a full 3-star answer in the comments, below.

####
09.13 Coming soon...

####
09.14 Enumerable.Range with Math.Max and Select

###
10 Jagged Arrays

###

All the following can be used to get 3 stars

####
10.01 List at i,j

####
10.02 Three star answer...

###

You need to use Enumerable.Repeat<T>.

####
10.03 Same as previous

####
10.04 Nested projections

####
10.05 Same as before, but use Sum() with a ternary

####
10.06 Same as before, but use Max()

####
10.07 Coming soon...

####
10.08 Larger of the sum of the last or sum of the first

###
11 Arrays 2

####
11.01 Zip, Concat and Skip

####
11.02 SelectMany() flattens

###

####
11.03 This one makes me cross..

Can someone explain to me why this does not work?!

return

Enumerable.Range(0, input.Length).Sum(arrayIndex=>(long)input[arrayIndex][arrayIndex])

==

Enumerable.Range(0, input.Length).Sum(arrayIndex=>(long)input[arrayIndex][input.Length-1-arrayIndex])

;

###

####
11.04 The modern way is Linq

####
11.05 string + condition?string:string.Empty

####
11.06 Linq t?"E":r?"N":a?"R":"Y"

####
11.07 Linq to Pivot using Enumerable.Range and [y][x]

###
12 Search Sort

####
12.01 .Count ==

####
12.02 Same again

####
12.03 Cleverness gives 1*

Use FirstOrDefault()

list.Select((item, index) => new { Item = item, Index = index })

####
12.04 Same but LastOrDefault() instead

####
12.05 Same but ToArray()

####
12.06 Simple Select()

####
12.07 Pairs sum

####
12.08 Coming soon...

####
12.09 Ever increasing

####
12.10 Distinct Count

####
12.11 Array.Sort()

####
12.12 Yes, it works for strings too!

###
13 Cyphers

####
13.01 int cast +7 %26 -97

####
13.02 Different mod, different base

####
13.03 Different offset per character

####
13.04 nn mm nineteen nineteen

###
14 Puzzles

###

####
14.01 Sum

####
14.02 6003

####
14.03 Coming soon...

####
14.04 Coming soon...

###

####

####

####

###