Home > OS >  Create a unique id from x and y integers and back
Create a unique id from x and y integers and back

Time:10-26

I'm working on a program, where the developer decides to create unique ids from x and y values. The id is calculated this way:

`id = toInteger(toString(x)   toString(x y)   toString(y))`

Any idea how I could get the x and y values out of such ids?

Thanks in advance.

CodePudding user response:

The number of digits in x is between 1 and (number of digits of id) - 2. The number of digits in y is between 1 and (number of digits in id) - (number of digits in x) - 1. The number of digits in x y is the rest.

This method returns the x and y pairs in a 2-element array of integers. and returns null if not found.

static int[] originalXandY(int id) {
    String idStr = Integer.toString(id);
    int idLen = idStr.length();
    for (int xLen = 1; xLen <= idLen - 2;   xLen) {
        int x = Integer.parseInt(idStr.substring(0, xLen));
        for (int yLen = 1; yLen <= idLen - xLen - 1;   yLen) {
            int y = Integer.parseInt(idStr.substring(idLen - yLen));
            if (x   y == Integer.parseInt(idStr.substring(xLen, idLen - yLen)))
                return new int[] {x, y};
        }
    }
    return null;
}

and

System.out.println(Arrays.toString(originalXandY(121)));
System.out.println(Arrays.toString(originalXandY(124634)));
System.out.println(Arrays.toString(originalXandY(1346345)));
System.out.println(Arrays.toString(originalXandY(123412439)));
System.out.println(Arrays.toString(originalXandY(123456)));

output:

[1, 1]
[12, 34]
[1, 345]
[1234, 9]
null

Caution: If id is 22220 there are two solutions x = 22, y = 0 and x = 2, y = 20. Note that this method returns only one solution.

CodePudding user response:

Use Random class to generate a random number inside a given range. This example prompts the user to enter the lower and upper limits for the randomly generated numbers. Then, use your snippet to generate the ID. I used Long to hold the value in case your upper limit is large.

public class RandomInRange {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the lower limit value: " );
        int lowerLimit = input.nextInt();
        System.out.print("Enter the upper limit value: " );
        int upperLimit = input.nextInt();
        input.close();
        Random random = new Random();
        int x = random.nextInt(lowerLimit, upperLimit   1);
        int y = random.nextInt(lowerLimit, upperLimit   1);
        Long id = Long.valueOf(String.valueOf(x)   String.valueOf(x y)   String.valueOf(y));

        System.out.println("x = "   x   "; y = "   y);
        System.out.println("ID: "   id);
    }
}

The output of one of my runs:

Enter the lower limit value: 43
Enter the upper limit value: 87
x = 43; y = 85
ID: 4312885

As you can see, the ID is 43 - 128 - 85. 128 is 85 43. The "43" was equal to the lower limit by coincidence.

Another sample run:

Enter the lower limit value: 43
Enter the upper limit value: 87
x = 53; y = 66
ID: 5311966

Since the upper bound of the randomizing function is exclusive, I added 1 to the number entered by the user to make the upper limit value inclusive.

  • Related