Generate sequential key based on sum of digits

Asked

Viewed 304 times

3

I have a cyclic function where a string may have any value in the checker digit, but all future submissions receive a pre-calculated value.

In this role, I receive a string (which will always be a number) and in this method, I remove the last digit and sum the remainder. In the end, I return this number + 1 and conclude with the sum. Complicated as I explained, no?

Explaining it better would be this: I receive the string 10000 and the return should be 10011. Where 1000 added + 1 and the next last digit is the sum of the rest (1 + 0 + 0 + 0 = 1).

My method is as follows:

public static int GenerateKey(string s)
        {           
            s = s.Remove(s.Length - 1);

            var i = Convert.ToInt32(s);
            var sum = 0;

            while (i != 0)
            {
                sum += i % 10;
                i /= 10;
            }

            var somatorio = Convert.ToInt32(s) + 1;

            var id = Convert.ToString(somatorio);

            if (sum.ToString().Length > 1)
            {
                sum = sum % 10;
            }

            id = id += sum;

            return Convert.ToInt32(id);
        } 

first: This method needs improvements and/or is "misspelled"?

2nd: Using this method it is possible that the return is repeated with one already used before? Remembering that the next time you execute the method the number will be the Return of the previously executed method.

Ex: if in started with 10000, the return will be 10011 then the next time the method is run to string will have the value of 10011, and so on.

1 answer

2


Badly written is not (in my opinion), I would change some small details, but is cosmetic:

using static System.Console;
using static System.Convert;

public class Program {
    public static void Main() {
        WriteLine(GenerateKey("10000"));
        WriteLine(GenerateKey("10011"));
        WriteLine(GenerateKey("10100"));
        WriteLine(GenerateKey("00000"));
        WriteLine(GenerateKey("999999999"));
    }
    public static int GenerateKey(string s) {           
        s = s.Remove(s.Length - 1);
        var i = ToInt32(s);
        var sum = 0;
        while (i != 0) {
            sum += i % 10;
            i /= 10;
        }
        var somatorio = ToInt32(s) + 1;
        var id = somatorio.ToString();
        if (sum.ToString().Length > 1) {
            sum %= 10;
        }
        id += sum;
        return ToInt32(id);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I haven’t looked deeply at any errors, but it doesn’t seem to be. The line id = id += sum; it’s not enough to make a mistake in this case, but it’s pretty weird to do this.

I can’t prove it to you mathematically, but it looks like it won’t repeat under normal conditions. Of course, you’d need to see what happens when there’s inconsistency in the number, if it’s too big. You may be able to ensure that this does not occur elsewhere in the code or has full control of the data used. Have you thought about what might happen if you send a "00000"? Or a "0"? Or a "99999"? OR "999999999"? Or "9999999999"?

Obviously I can’t guarantee if you do what you want. It seems like a strange algorithm, but there must be a reason for it to exist.

  • These numbers you mentioned are dealt with before. My real fear was that I might have some problem (which I didn’t see). This algorithm was requested by our technicians to meet a customization. I particularly find it a "gambiarra" for the proposed functionality, but that’s beside the point. Thank you very much for the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.