【发布时间】:2011-04-25 18:29:29
【问题描述】:
我最近被要求提交一份工作问题的解决方案。
问题:在字符串中查找子字符串。
Input: "Little star's deep dish pizza sure is fantastic."
Search: "deep dish pizza"
Output: "Little star's [[HIGHLIGHT]]deep dish pizza[[ENDHIGHLIGHT]] sure is fantastic."
请注意,在此示例中,荧光笔不必具有完全相同的结果,因为 您 正在定义什么是好的 sn-p 并返回最相关的 sn-p查询词突出显示。
最重要的要求是像编写生产代码一样编写它。
我的解决方案未被接受。我怎么能改进它?我知道,我可以使用:
- Knuth–Morris–Pratt algorithm
- 正则表达式(可以吗?)
我的问题:
科技公司在审查工作代码时会考虑哪些因素。我在同一天提交了代码,这有什么帮助吗?
它指出,在其中一个 cmets 中,它看起来像学校代码而不是生产代码。如何?有什么建议吗?
我的解决方案:
FindSubString.java
/**
* FindSubString.java: Find sub-string in a given query
*
* @author zengr
* @version 1.0
*/
public class FindSubstring {
private static final String startHighlight = "[[HIGHLIGHT]]";
private static final String endHighlight = "[[ENDHIGHLIGHT]]";
/**
* Find sub-string in a given query
*
* @param inputQuery: A string data type (input Query)
* @param highlightDoc: A string data type (pattern to match)
* @return inputQuery: A String data type.
*/
public String findSubstringInQuery(String inputQuery, String highlightDoc) {
try {
highlightDoc = highlightDoc.trim();
if (inputQuery.toLowerCase().indexOf(highlightDoc.toLowerCase()) >= 0) {
// update query if exact doc exists
inputQuery = updateString(inputQuery, highlightDoc);
}
else {
// If exact doc is not in the query then break it up
String[] docArray = highlightDoc.split(" ");
for (int i = 0; i < docArray.length; i++) {
if (inputQuery.toLowerCase().indexOf(docArray[i].toLowerCase()) > 0) {
inputQuery = updateString(inputQuery, docArray[i]);
}
}
}
} catch (NullPointerException ex) {
// Ideally log this exception
System.out.println("Null pointer exception caught: " + ex.toString());
}
return inputQuery;
}
/**
* Update the query with the highlighted doc
*
* @param inputQuery: A String data type (Query to update)
* @param highlightDoc: A String data type (pattern around which to update)
* @return inputQuery: A String data type.
*/
private String updateString(String inputQuery, String highlightDoc) {
int startIndex = 0;
int endIndex = 0;
String lowerCaseDoc = highlightDoc.toLowerCase();
String lowerCaseQuery = inputQuery.toLowerCase();
// get index of the words to highlight
startIndex = lowerCaseQuery.indexOf(lowerCaseDoc);
endIndex = lowerCaseDoc.length() + startIndex;
// Get the highlighted doc
String resultHighlightDoc = highlightString(highlightDoc);
// Update the original query
return inputQuery = inputQuery.substring(0, startIndex - 1) + resultHighlightDoc + inputQuery.substring(endIndex, inputQuery.length());
}
/**
* Highlight the doc
*
* @param inputString: A string data type (value to be highlighted)
* @return highlightedString: A String data type.
*/
private String highlightString(String inputString) {
String highlightedString = null;
highlightedString = " " + startHighlight + inputString + endHighlight;
return highlightedString;
}
}
TestClass.java
/**
* TestClass.java: jUnit test class to test FindSubString.java
*
* @author zengr
* @version 1.0
*/
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestClass extends TestCase
{
private FindSubstring simpleObj = null;
private String originalQuery = "I like fish. Little star's deep dish pizza sure is fantastic. Dogs are funny.";
public TestClass(String name) {
super(name);
}
public void setUp() {
simpleObj = new FindSubstring();
}
public static Test suite(){
TestSuite suite = new TestSuite();
suite.addTest(new TestClass("findSubstringtNameCorrect1Test"));
suite.addTest(new TestClass("findSubstringtNameCorrect2Test"));
suite.addTest(new TestClass("findSubstringtNameCorrect3Test"));
suite.addTest(new TestClass("findSubstringtNameIncorrect1Test"));
suite.addTest(new TestClass("findSubstringtNameNullTest"));
return suite;
}
public void findSubstringtNameCorrect1Test() throws Exception
{
String expectedOutput = "I like fish. Little star's deep [[HIGHLIGHT]]dish pizza[[ENDHIGHLIGHT]] sure is fantastic. Dogs are funny.";
assertEquals(expectedOutput, simpleObj.findSubstringInQuery(originalQuery, "dish pizza"));
}
public void findSubstringtNameCorrect2Test() throws Exception
{
String expectedOutput = "I like fish. Little star's [[HIGHLIGHT]]deep dish pizza[[ENDHIGHLIGHT]] sure is fantastic. Dogs are funny.";
assertEquals(expectedOutput, simpleObj.findSubstringInQuery(originalQuery, "deep dish pizza"));
}
public void findSubstringtNameCorrect3Test() throws Exception
{
String expectedOutput = "Hello [[HIGHLIGHT]]how[[ENDHIGHLIGHT]] are [[HIGHLIGHT]]you[[ENDHIGHLIGHT]]r?";
assertEquals(expectedOutput, simpleObj.findSubstringInQuery("Hello how are your?", "how you"));
}
public void findSubstringtNameIncorrect1Test() throws Exception
{
String expectedOutput = "I like fish. Little star's deep dish pizza sure is fantastic. Dogs are funny.";
assertEquals(expectedOutput, simpleObj.findSubstringInQuery(originalQuery, "I love Ruby too"));
}
public void findSubstringtNameNullTest() throws Exception
{
String expectedOutput = "I like fish. Little star's deep dish pizza sure is fantastic. Dogs are funny.";
assertEquals(expectedOutput, simpleObj.findSubstringInQuery(originalQuery, null));
}
}
【问题讨论】:
-
@zengr:您是否在没有 Internet 访问的情况下编写了解决方案?如果是这样,天哪,他们应该雇用你!
-
我没有。这是我写的。
-
我认为它没有被接受,因为你忘了先检查 highlightDoc.length() 是否小于 inputQuery
-
我不会捕获 NullPointerException - 我肯定会检查先决条件。这可能是他们做出决定的原因吗?
-
想法:主要的 findSubstringInQuery 方法可能是静态的;如果没有指定 trim() 和不区分大小写,我就不会包含它们;它看起来更像是一所学校而不是生产代码——也许他们正在寻找优雅?但是,我喜欢单元测试覆盖率。我会雇用你的!