-5

I have an issue with this:

When John was a little kid he didn't have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could... he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.

Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it. The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn't care about whether letters are uppercase or lowercase, so that doesn't affect the beauty of a letter. (Uppercase 'F' is exactly as beautiful as lowercase 'f', for example.)

You're a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?

Input sample:

Your program should accept as its first argument a path to a filename. Each line in this file has a sentence. E.g.

ABbCcc
Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please :)
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves

Output sample:

Print out the maximum beauty for the string. E.g.

152
754
491
729
646

And here is my code in C#:

static void Main(string[] args)
{
    StreamReader myR = new StreamReader(args[0]);
    List<string> myL = new List<string>();


    while (!myR.EndOfStream)
    {
        myL.Add(myR.ReadLine());
    }

    foreach (string item in myL)
    {
        int maxBeautifulNum = 0;
        string temp = item.ToLower();

        char[] myC = temp.ToCharArray();

        string[] tempS = new string[myC.Length];
        string tempCount;

        for (int i = 0; i < myC.Length; i++)
        {
            if ((myC[i] >= 'a' && myC[i] <= 'z') || (myC[i] >= 'A' && myC[i] <= 'Z'))
            {
                tempS[i] = myC[i].ToString();
            }

        }

        tempS = tempS.Where(w => !string.IsNullOrWhiteSpace(w)).ToArray();

        List<string> myCounterList = new List<string>();

        while(tempS.Length >= 1)
        {
            int count = 0;
            tempCount = tempS[0];

            for (int j = 0; j < tempS.Length; j++)
            {
                if (tempS[j] == tempCount)
                {
                    count++;
                }
            }

            myCounterList.Add(count.ToString());

            tempS = tempS.Where(w => w != tempCount).ToArray();
        }

        string[] myCounterString = myCounterList.ToArray();
        Array.Sort(myCounterString);
        Array.Reverse(myCounterString);

        for (int i = 0; i < myCounterString.Length; i++)
        {
            maxBeautifulNum += (26 - i) * int.Parse(myCounterString[i]);
        }
        Console.WriteLine(maxBeautifulNum);
    }
}

It run right with only some tests, but I can't find out else test for it runs failed. Any one can show me test for my code run failed.

Doctor Jones
  • 21,196
  • 13
  • 77
  • 99
mrdlf
  • 15
  • 10
  • 3
    This is the strangest thing I have read on the internet this morning... – Brian Oct 15 '13 at 15:54
  • It's related to the [FAcebook hacker cup 2013](https://www.google.fr/search?q=When+John+was+a+little+kid+he+didn't+have+much+to+do.+There+was+no+internet%2C+no+Facebook%2C+and+no+programs+to+hack+on.+So+he+did+the+only+thing+he+could...+he+evaluated+the+beauty+of+strings+in+a+quest+to+discover+the+most+beautiful+string+in+the+world.&oq=When+John+was+a+little+kid+he+didn't+have+much+to+do.+There+was+no+internet%2C+no+Facebook%2C+and+no+programs+to+hack+on.+So+he+did+the+only+thing+he+could...+he+evaluated+the+beauty+of+strings+in+a+quest+to+discover+the&aqs=chrome..69i57&sourceid=chrome) – Steve B Oct 15 '13 at 15:58
  • The problem is poorly worded. Are you attempting to find the mapping of letters to beauty which yields the highest possible total beauty for a given string? If so, I'd suggest using an `int[26]` to tally the number of A's, B's, etc. directly from the input string (subtract 'A' from each character; if greater than 32, subtract 32. If the result is 0..25, bump the appropriate array element), and then think about how to compute the beauty given those tallies. Your code as written is confusing, and it's unclear what it's computing. – supercat Oct 15 '13 at 15:59
  • If you're wondering where your program might fail, though, I'd suggest the string "AAAAAAAAAABB", whose beauty should if I'm understanding the problem correctly be 310 (26*10+25*9). I would guess your program might output 302. – supercat Oct 15 '13 at 16:03
  • 2
    So, where do I get what you smoke? – G.Y Oct 15 '13 at 16:04
  • Beauty is the sum of the beauty of characters? This must be part of some kind of "big is beautiful" campaign. But I'm not afraid to say that there is beauty in succinctness. Not that there is anything wrong with people who are into character count, of course. – nmclean Oct 15 '13 at 16:36
  • I only mean I can't find out test case for failure. Thank to supercat, I had found my false. – mrdlf Oct 16 '13 at 15:43

1 Answers1

1

Here's a basic LINQPad program that calculates the "beauty" of a string:

void Main()
{
    string[] inputs =
    {
        "a",
        "z",
        "A test",
        "A TEST",
        "a test"
    };

    foreach (var input in inputs)
    {
        var beauty =
            (from c in input
             let C = char.ToUpper(c)
             where C >= 'A' && C <= 'Z'
             select (int)(C - 'A') + 1).Sum();
        beauty.Dump(input);
    }
}

All the magic is in that single LINQ statement inside the loop there.

Also, to read all the lines from a file, use this:

string[] lines = File.ReadAllLines(fileName);
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • I've never seen the Dump method and can't seem to find it in help. Can you explain what that does, just for my own curiosity? Or it's namespace so I can look it up? – Harrison Oct 15 '13 at 16:10
  • 1
    It's a [LINQPad](http://stackoverflow.com/questions/2699466/linqpad-dump-extension-method-i-want-one) extension method. It's not built into .NET. – Lasse V. Karlsen Oct 15 '13 at 16:19
  • Really thank you Lasse V.Karlsen. I'm newbie in C#, so I have not had much knowledge to practice for elegant. – mrdlf Oct 16 '13 at 15:44