Using modulo in my program

Hello,

I am writing a program which basically creates a 3 letter lock, say CAT for example, then the lock can be opened, closed, reset, and letters can be set individually. I am using only uppercase letters A - Z, (65 - 90 in unicode). The trick to this is to wrap the letters in a circle. Meaning after Z comes A, and before A comes Z. Just like a circular lock would have in real life. A lock can also be set with a tolerance, say the tolerance is 1, the lock combo of CAT can be opened with CAT, CBT, BAT, DAT etc.... the tolerance doesn't have a limit so it could be 5. Another example, a combination is ZAZ, and the tolerance is 5, this can be opened with ZAZ, AZA, ZAD, etc.. you get the point. I am having trouble with the wrapping of the code, I got the tolerance to work for most of the combos, but I am getting that this is the wrong combo when I try, the combo is ZAZ, the tolerance is 5, and ZAD will not open, which it should. Here is my method which determines whether the lock will open..

Code:

`public void pull()`

{

isOpen = (((Math.abs(letter1 - input1) % CHAR_RANGE)

+ (Math.abs(letter2 - input2) % CHAR_RANGE)

+ (Math.abs(letter3 - input3) % CHAR_RANGE)) <= tolerance);

}

where letterx is the letter which was used to set the lock. e.g. CAT, C = letter1, A = letter2, T = letter3. (The real lock combo)

where inputx is the letter which was used as an attempt by the user to open the lock. (The combo attempted to open the lock)

where CHAR_RANGE is 'Z' - 'A' which is 90 - 65 = 25.

and tolerance is the tolerance as explained in the paragraph above.

Anyways i have been over my head and tried nearly everything, I am still fairly new so if anyone can guide me in the right direction that would be great.

Re: Using modulo in my program

Where are your notes that shows the values that the code returns when it is executed?

Make a spread sheet using some letters and write down the value returned by the - operator and then by the abs() method and then the value returned by the % operator.

Re: Using modulo in my program

Code:

`public void pull()`

{

isOpen = (((Math.abs(letter1 - input1) % CHAR_RANGE)

+ (Math.abs(letter2 - input2) % CHAR_RANGE)

+ (Math.abs(letter3 - input3) % CHAR_RANGE)) <= tolerance);

}

Ok so say we have the combo ZAZ, and we set the tolerance to 5. If we try the combo ZAD is should work since tolerance is 4 and 4 <= 5.

Here is why it does not work;

Math.abs(Z - Z) = 0, 0 % 25 = 0.

Math.abs(A - A) = 0, 0 % 25 = 0.

Math.abs(Z - D) = (90 - 68) = 22, 22 % 25 = 22, when I want it to equal 4, since Z -> A -> B -> C -> D = off by 4.

I have no clue how to fix this. I've tried shifting all the letters down so A = 1 and Z = 26, like in the alphabet but then i can not wrap it properly so that after Z comes A and vice versa..

Re: Using modulo in my program

Quote:

Z -> A -> B -> C -> D = off by 4.

I am not sure I understand what you are computing.

Given two characters: Z and D, are you saying the distance between them is 4. Is that what you are trying to compute: 4 given the chars: 'Z' and 'D'?

Why are there three letterN and three inputN (N=1 or 2 or 3)?

Re: Using modulo in my program

Quote:

Originally Posted by

**Norm** I am not sure I understand what you are computing.

Given two characters: Z and D, are you saying the distance between them is 4. Is that what you are trying to compute: 4 given the chars: 'Z' and 'D'?

Why are there three letterN and three inputN (N=1 or 2 or 3)?

What Z and D demonstrate are the tolerance. in this case Z is the actual letter which is used to set the combo, in this case ZAZ, and since it is the last letter it would go into the variable letter3. Whereas D is the letter used to attempt to open the lock. In order to open the lock you need to input ZAZ, although since we set the tolerance to 5, the lock can be opened with ZAZ +- 5 letters, so something like ZBA will open it, since Z and Z remain the same therefore tolerance = 0, A and B is Math.abs(65-66)%25 which is 1, therefore tolerance is 1, and finally Z and A need also to be a tolerance of one since it is one letter off. I am not sure how else to describe this.

Re: Using modulo in my program

If 'z' to 'd' is 22 and you want to get 4, what arithmetic can be done with the 22 to get 4?

When would that method be needed vs when getting the distance from 'a' to 'c'?

Re: Using modulo in my program

Quote:

Originally Posted by

**Norm** If 'z' to 'd' is 22 and you want to get 4, what arithmetic can be done with the 22 to get 4?

When would that method be needed vs when getting the distance from 'a' to 'c'?

'Z' TO 'D' could be done by doing the number of letters in the alphabet (26)... 26 % 22 = 4

Although this would not work if there was no tolerance, for example, A - A = 65 - 65 = 0, 26 % 0 = undefined.

this is my problem, one of my solutions work one way and not the other, and the other does vice verca..

Re: Using modulo in my program

Re: Using modulo in my program

I see what you are saying, but what if the letter is B and the input is also B. This would mean B - B = 0. And according to your equation, we must do 26 - 0 = 26. When realistically i need it to equal 0.

Re: Using modulo in my program

There are two cases: 'z' to 'd' and 'a' to 'b'

They need to be handled differently.

Re: Using modulo in my program

I finally got it to work, thanks to your help. My final code for the pull method is this.

Code:

`public void pull()`

{

int tolerance1 = Math.abs(letter1 - input1);

int tolerance2 = Math.abs(letter2 - input2);

int tolerance3 = Math.abs(letter3 - input3);

if (tolerance != 0 && tolerance1 > 13)

{

tolerance1 = 26 - tolerance1;

}

if (tolerance != 0 && tolerance2 > 13)

{

tolerance2 = 26 - tolerance2;

}

if (tolerance != 0 && tolerance3 > 13)

{

tolerance3 = 26 - tolerance3;

}

isOpen = (tolerance1 + tolerance2 + tolerance3) <= tolerance;

}

And it works as intended. Thanks for the tips.

Re: Using modulo in my program

I finally realized that if all the letters are on a ring, there are two distances between each two letters: one going clockwise and the other going counter-clockwise. The sum of the two distances is 26.

Re: Using modulo in my program

Yep, i managed to overcome that issue.