-2

I'm new to C# and C-like languages, and can't find the error in my code. Description of exercise:

Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half.

Given a ticket number n, determine if it's lucky or not.

Example

For n = 1230, the output should be isLucky(n) = true; For n = 239017, the output should be isLucky(n) = false. Input/Output

[execution time limit] 3 seconds (cs)

[input] integer n

A ticket number represented as a positive integer with an even number of digits.

Guaranteed constraints: 10 ≤ n < 106.

[output] boolean

true if n is a lucky ticket number, false otherwise.

My code:

bool isLucky(int n)
{
    string ticket = n.ToString();
    int firstSum = 0;
    int secondSum = 0;
    string[] ticketStrArr = ticket.Split("");
    int lenHalf = ticketStrArr.Length / 2;

    for(int i = 0; i < lenHalf; i++)
    {
        firstSum = firstSum + Int32.Parse(ticketStrArr[i]);
        secondSum = secondSum + Int32.Parse(ticketStrArr[i + lenHalf]);
    }

    return (firstSum == secondSum);
}
2
  • 7
    This is a great opportunity to begin using a debugger. With a debugger you can step through the code line by line as it executes, observing the exact runtime behavior and changing values of the variables. When you do this, which operation first produces an unexpected result? What were the values used in the operation? What was the result? What result was expected? Why? Commented Jun 10, 2020 at 19:27
  • 1
    It seems like a good step debugger would be useful in solving this. Try this one Commented Jun 10, 2020 at 19:28

1 Answer 1

0

C# strings are considered char arrays, so there is really no need to split at all.

This should do the trick, and as other comments have said, very useful to use debugger.

Edit - as madreflection stated in comment below use implicit conversion from char to int to get the right value instead of int.Parse() or Convert.ToInt32();

    bool isLucky(int n)
    {
        string ticket = n.ToString();
        int firstSum = 0;
        int secondSum = 0;
        //string[] ticketStrArr = ticket.Split();
        int lenHalf = ticket.Length / 2;

        for (int i = 0; i < lenHalf; i++)
        {
            firstSum += ticket[i] - '0';
            secondSum += ticket[i + lenHalf] - '0';
        }

        return (firstSum == secondSum);
    }
Sign up to request clarification or add additional context in comments.

4 Comments

No need to do Convert.ToInt32(), either... just use implicit conversion of char to int. firstSum += ticket[i] - '0';
Also, .NET Core 2.0+ has the Split overload used in the question.
Well, Convert.ToInt32(ticket[i]) is wrong because Convert.ToInt32('0') returns 48, not 0. That's why I include - '0' in my comment.
@madreflection Thank you for the .NET Core 2.0+ reference, i was not aware that is what was being used. I updated the answer to correct the Convert.ToInt32() bit as well

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.