patternjavaMinor
Calculating the distance between two letters
Viewed 0 times
thedistancecalculatingbetweentwoletters
Problem
I completed a sample programming challenge and wanted to find out how I could make it more efficient. I'm trying to get better at writing more efficient code, so I would be interested in hearing ideas on how I can make the code more efficient. I ran it, and it works, just curious if there are inefficiencies that some of the seasoned developers could point out.
One thing I know seems wasteful is the continuous polling of the before or after to determine if I should increment or decremented the distance, but couldn't think of a better way right off hand. Tried a ternary statement, but I couldn't get it to work.
```
public class Main {
/*Write a method to calculate the distance between two letters (A-Z, a-z,
case insensitive). The input to the method is two char primitives.
If either char is not A-Za-z, throw an AlphabetException. Distance in this case is defined
as the number of letters between firstChar and secondChar.
For example, the distance between 'a' and 'c' is 2, and the distance
between 'c' and 'a' is -2. Counting is done alphabet wise, so the
distance between 'a' and 'z' is 25, while the distance between 'z' and 'a'
is -25. The distance calculation should be case insensitive, so calclating
the distance between 'A' and 'c' yields the same result as the distance
between 'a' and 'c'. If the firstChar is equal to the secondChar, the
distance is considered 0.
*/
public int getDistanceBetweenTwoLetters(char firstChar, char secondChar) throws AlphabetException {
if(!isLetter(firstChar)){
throw new AlphabetException(firstChar+" is not part of the alphabet");
}
if(!isLetter(secondChar)){
throw new AlphabetException(secondChar+" is not part of the alphabet");
}
String firstStringValue = String.valueOf(firstChar).toUpperCase();
Character firstCharacter = firstStringValue.charAt(0);
String secondStringValue = String.valueOf(s
One thing I know seems wasteful is the continuous polling of the before or after to determine if I should increment or decremented the distance, but couldn't think of a better way right off hand. Tried a ternary statement, but I couldn't get it to work.
```
public class Main {
/*Write a method to calculate the distance between two letters (A-Z, a-z,
case insensitive). The input to the method is two char primitives.
If either char is not A-Za-z, throw an AlphabetException. Distance in this case is defined
as the number of letters between firstChar and secondChar.
For example, the distance between 'a' and 'c' is 2, and the distance
between 'c' and 'a' is -2. Counting is done alphabet wise, so the
distance between 'a' and 'z' is 25, while the distance between 'z' and 'a'
is -25. The distance calculation should be case insensitive, so calclating
the distance between 'A' and 'c' yields the same result as the distance
between 'a' and 'c'. If the firstChar is equal to the secondChar, the
distance is considered 0.
*/
public int getDistanceBetweenTwoLetters(char firstChar, char secondChar) throws AlphabetException {
if(!isLetter(firstChar)){
throw new AlphabetException(firstChar+" is not part of the alphabet");
}
if(!isLetter(secondChar)){
throw new AlphabetException(secondChar+" is not part of the alphabet");
}
String firstStringValue = String.valueOf(firstChar).toUpperCase();
Character firstCharacter = firstStringValue.charAt(0);
String secondStringValue = String.valueOf(s
Solution
Way too complicated
Everything after the two
Everything after the two
isLetter() checks is extremely complicated and unnecessary. You can just subtract the two character values, like this:public int getDistanceBetweenTwoLetters(char firstChar, char secondChar) throws AlphabetException {
if(!isLetter(firstChar)){
throw new AlphabetException(firstChar+" is not part of the alphabet");
}
if(!isLetter(secondChar)){
throw new AlphabetException(secondChar+" is not part of the alphabet");
}
return Character.toLowerCase(secondChar) - Character.toLowerCase(firstChar);
}Code Snippets
public int getDistanceBetweenTwoLetters(char firstChar, char secondChar) throws AlphabetException {
if(!isLetter(firstChar)){
throw new AlphabetException(firstChar+" is not part of the alphabet");
}
if(!isLetter(secondChar)){
throw new AlphabetException(secondChar+" is not part of the alphabet");
}
return Character.toLowerCase(secondChar) - Character.toLowerCase(firstChar);
}Context
StackExchange Code Review Q#105137, answer score: 4
Revisions (0)
No revisions yet.