views:

409

answers:

3
+2  A: 
var count = male.Length + female.Length - male.Intersect( female ).Count();

while (flames.Length > 1)
{
     flames = string.Join( '', flames.Where( (c,i) => i != (count % flames.Length) -1 ).ToArray() );
}
tvanfosson
Thank you tvanfosson
This neither compiles, nor gives me the letter "E" when trying with count=1, as in the example.
Simon Svensson
I didn't try to compile it since I only have my Macintosh at present. I suspect the difference may be zero-based indices versus one-based indices in the example, though I thought I had accounted for that. I'll look at it when I get to the office.
tvanfosson
+2  A: 

That last calculation part that iterates through the flames-letters and removing one after another can be precalculated.

public static Char GetChar(int diff) {
    var idx = (diff - 1) % 60;
    return "efefmeaelmaafmfaflefefeemsasamfmfallslslesmsasmmaelmlaslslfs"[idx];
}

Some things can be done without linq... unless I totally messed something up.

Simon Svensson
Thank you very much simon
How did you figure out that string? I can't see the pattern
CasperT
@CasperT - took me some time, too. There is no pattern, it's pre calculated - s/he ran it for 60 possible values, and these are the results. I looked for a pattern, but couldn't simplify the loop away.
Kobi
Yes, that's the 60 possible values that will repeat itself over and over again. You could probably just do a precalculation of the first thousand results if you fancy a brute force solution, and hope noone finds two names of 500 characters each... ;)
Simon Svensson
but isn't a a simple loop better than some magic string?You'd only need to run the loop once and then save the pattern.
CasperT
I ran the loop once, and that's the output. Running the loop once in code would mean you need to consume cpu resources on something that can be determined before or at compile time. Also, it would need to be calculated by every instance of the program, consuming more cpu time in total than it took for me to run the loop locally.
Simon Svensson
+2  A: 

Step1 and Step2

var firstLookup = firstName.ToLookup(c => c.ToLower());
var secondLookup = secondName.ToLookup(c => c.ToLower());
var allChars = firstLookup.Keys.Union(secondLookup.Keys);
int count =
(
  from c in allChars
  let firstCount = firstLookup[c].Count()
  let secondCount = secondLookup[c].Count()
  select
    firstCount < secondCount ? secondCount - firstCount :
    firstCount - secondCount
).Sum()

Step3 (untested)

List<char> word = "FLAMES".ToList();

while (word.Count > 1)
{
  int wordCount = word.Count;
  int remove = (count-1) % wordCount;
  word =
    word.Select( (c, i) => new {c, i =
      i == remove ? 0 :
      i < remove ? i + wordCount + 1 : 
      i})
    .OrderBy(x => x.i)
    .Select(x => x.c)
    .Skip(1)
    .ToList();
}

char result = word.Single();
David B
Thanks David,I suspect step three is the critical part,is won't it?