patternjavaModerate
Finding the Mth to last Element
Viewed 0 times
lasttheelementmthfinding
Problem
Challenge:
Write a program which determines the Mth to last element in a list.
Specifications:
The first argument is a path to a file.
The file contains series of space delimited characters
followed by an integer.
The integer represents an index in the list (1-based), one per line.
Print out the Mth element from the end of the list, one per line.
If the index is larger than the number of elements in the list,
ignore that input.
Solution:
This passes my tests, I tried to account for some edge cases, is there anything I missed or something I can improve?
For anyone interested, this challenge, and many others like it are available here.
Write a program which determines the Mth to last element in a list.
Specifications:
The first argument is a path to a file.
The file contains series of space delimited characters
followed by an integer.
The integer represents an index in the list (1-based), one per line.
Print out the Mth element from the end of the list, one per line.
If the index is larger than the number of elements in the list,
ignore that input.
Solution:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MToLast {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File(args[0]));
while (input.hasNextLine()) {
retrieveAndPrintMToLast(input.nextLine());
}
}
private static void retrieveAndPrintMToLast(String line) {
int targetIndex = Integer.parseInt(line.replaceAll("\\D", ""));
String sanitized = line.replaceAll("[^a-zA-Z]", "");
int limit = sanitized.length();
if (targetIndex <= limit) {
System.out.println(sanitized.charAt(limit - targetIndex));
}
}
}This passes my tests, I tried to account for some edge cases, is there anything I missed or something I can improve?
For anyone interested, this challenge, and many others like it are available here.
Solution
Your solution looks nice and compact, but it's not necessarily how I would go about it. I come from a background where you have to anticipate faulty input, and so I raise my eyebrows at the first two lines of your method where you blindly start replacing the input.
What happens if you read a line of characters like
The problem specification is straightforward, but you're trying to attack it with regexes instead of something more appropriate. As soon as you transform your input, you are working with data that is manipulated based on your assumptions. This will lead to strange bugs later on that are hard to track down.
I would also suggest changed the method from returning
Finally, make sure you're testing all the bases. What happens when there's an empty string? A string with just one character, or just one integer. What if the last integer is less than 0?
What happens if you read a line of characters like
A B 3 D 5 1? Do you still get A as a result? Numeric characters are still characters. Also, \n is char. The class is much more than what is captured by your [^a-zA-Z] pattern. The problem specification is straightforward, but you're trying to attack it with regexes instead of something more appropriate. As soon as you transform your input, you are working with data that is manipulated based on your assumptions. This will lead to strange bugs later on that are hard to track down.
I would also suggest changed the method from returning
void to char, then let whatever calling method take care of the output.Finally, make sure you're testing all the bases. What happens when there's an empty string? A string with just one character, or just one integer. What if the last integer is less than 0?
Context
StackExchange Code Review Q#83601, answer score: 11
Revisions (0)
No revisions yet.