怎么能这样的Java code加以改进,以找到子字符串中的一个?字符串、Java、code

2023-09-11 02:02:51 作者:伤感爱情大全

我最近被要求提交解决一个问题的工作。

问题:查找子字符串中的一个

 输入:小星的深盘比萨肯定是太棒了。
搜索:深盘比萨
输出:小星的[[精彩]深盘比萨[ENDHIGHLIGHT]肯定是太棒了。
 

请注意,该荧光笔不必对本实施例完全相同的结果,由于你的正在定义什么好片段是与术语突出查询返回最相关的片段。

最重要的要求是写它,因为我会写的生产code

我的解决方案是不能接受的。我怎么能提高呢?我知道,我可以用:

克努特 - 莫里斯 - 普拉特算法 在正则表达式(可能吗?)

我的问题:

做什么高科技公司考虑到,当他们审阅code一份工作。我提交的code在同一天,这是否帮助以任何方式?

微软终于公布了这个神秘的VS Code产品路线图

在注释中的一个,它指出,它看起来像一个学校code比产量code。怎么样?有什么建议?

我的解决办法: FindSubString.java

  / **
 * FindSubString.java:查找子串在一个给定的查询
 *
 * @author zengr
 * @version 1.0
 * /

公共类FindSubstring {
    私有静态最后弦乐startHighlight =[[精彩片段];
    私有静态最后弦乐endHighlight =[[ENDHIGHLIGHT];

    / **
     *找到子串在给定的查询
     *
     *参数inputQuery:字符串数据类型(输入查询)
     *参数highlightDoc:字符串数据类型(模式匹配)
     * @返回inputQuery:字符串数据类型。
     * /
    公共字符串findSubstringInQuery(字符串inputQuery,字符串highlightDoc){
        尝试 {

            highlightDoc = highlightDoc.trim();

            如果(inputQuery.toLowerCase()的indexOf(highlightDoc.toLowerCase())> = 0){
                如果准确的文档存在//更新查询
                inputQuery = updateString(inputQuery,highlightDoc);
            }

            其他 {
                //如果准确的文档不在查询,然后打破它
                的String [] docArray = highlightDoc.split();

                的for(int i = 0; I< docArray.length;我++){
                    如果(inputQuery.toLowerCase()的indexOf(docArray [I] .toLowerCase())大于0){
                        inputQuery = updateString(inputQuery,docArray [I]);
                    }
                }
            }
        }赶上(NullPointerException异常前){
            //理想的情况下登录该异常
            的System.out.println(空指针异常捕获:+ ex.toString());
        }

        返回inputQuery;
    }

    / **
     *更新与突出显示的文档查询
     *
     *参数inputQuery:字符串数据类型(查询,更新)
     *参数highlightDoc:字符串数据类型(围绕其更新模式)
     * @返回inputQuery:字符串数据类型。
     * /
    私人字符串updateString(字符串inputQuery,字符串highlightDoc){
        INT的startIndex = 0;
        INT endIndex的= 0;

        串lowerCaseDoc = highlightDoc.toLowerCase();
        串lowerCaseQuery = inputQuery.toLowerCase();

        //获取的话来彰显指数
        在startIndex = lowerCaseQuery.indexOf(lowerCaseDoc);
        endIndex的= lowerCaseDoc.length()+在startIndex;

        //获取高亮显示的文档
        字符串resultHighlightDoc = highlightString(highlightDoc);

        //更新原始查询
        返回inputQuery = inputQuery.substring(0,在startIndex  -  1)+ resultHighlightDoc + inputQuery.substring(endIndex的,inputQuery.length());
    }

    / **
     *突出显示文档
     *
     *参数inputString:字符串数据类型(被突出显示值)
     * @返回highlightedString:字符串数据类型。
     * /
    私人字符串highlightString(字符串inputString){
        字符串highlightedString = NULL;

        highlightedString =+ startHighlight + inputString + endHighlight;

        返回highlightedString;
    }
}
 

TestClass.java

  / **
 * TestClass.java:JUnit测试类来测试FindSubString.java
 *
 * @author zengr
 * @version 1.0
 * /

进口junit.framework.Test;
进口的junit.framework.TestCase;
进口junit.framework.TestSuite;

公共类识别TestClass扩展的TestCase
{
    私人FindSubstring simpleObj = NULL;
    私人字符串originalQuery =我喜欢吃鱼,小星的深盘比萨肯定的是梦幻般的狗是有趣的。

    公众识别TestClass(字符串名称){
        超(名称);
    }

    公共无效设置(){
        simpleObj =新FindSubstring();
    }

    公共静态测试套件(){

        TestSuite的套装=新的TestSuite();
        suite.addTest(新的TestClass(findSubstringtNameCorrect1Test));
        suite.addTest(新的TestClass(findSubstringtNameCorrect2Test));
        suite.addTest(新的TestClass(findSubstringtNameCorrect3Test));
        suite.addTest(新的TestClass(findSubstringtNameIncorrect1Test));
        suite.addTest(新的TestClass(findSubstringtNameNullTest));

        返回套房;
    }

    公共无效findSubstringtNameCorrect1Test()抛出异常
    {
        字符串expectedOutput =我喜欢吃鱼,小星的深[[精彩]菜比萨饼[ENDHIGHLIGHT]肯定的是梦幻般的狗是有趣的。
        的assertEquals(expectedOutput,simpleObj.findSubstringInQuery(originalQuery,菜比萨));
    }

    公共无效findSubstringtNameCorrect2Test()抛出异常
    {
        字符串expectedOutput =我喜欢吃鱼小明星的[[精彩]深盘比萨[ENDHIGHLIGHT]肯定的是梦幻般的狗是有趣的。
        的assertEquals(expectedOutput,simpleObj.findSubstringInQuery(originalQuery,深盘比萨));
    }

    公共无效findSubstringtNameCorrect3Test()抛出异常
    {
        字符串expectedOutput =你好[[精彩]如何[ENDHIGHLIGHT]是[[精彩]你[ENDHIGHLIGHT] R';
        的assertEquals(expectedOutput,simpleObj.findSubstringInQuery(你好你怎么样了?,如何));
    }

    公共无效findSubstringtNameIncorrect1Test()抛出异常
    {
        字符串expectedOutput =我喜欢吃鱼,小星的深盘比萨肯定的是梦幻般的狗是有趣的。
        的assertEquals(expectedOutput,simpleObj.findSubstringInQuery(originalQuery,我爱红宝石太));
    }

    公共无效findSubstringtNameNullTest()抛出异常
    {
        字符串expectedOutput =我喜欢吃鱼,小星的深盘比萨肯定的是梦幻般的狗是有趣的。
        的assertEquals(expectedOutput,simpleObj.findSubstringInQuery(originalQuery,NULL));

    }
}
 

解决方案

几点意见;

您只突出的第一的搜索字符串的次数。 您认为小写匹配是好的。除非这被指定为尊重情况下忽略大小写的要求可能会更好,提供两个方法,一个又一个。 我可能会检查给定参数,并抛出一个NPE,如果其中任何一个为空的。这将是第一件事,我的方法做。我清楚地记录在Javadoc这种行为。 您的方法MAME是坏的; findSubstringInQuery 的主要任务不是发现,这是为了突出和 inQuery 部分是superflous。只要调用方法亮点或者 highlightIgnoreCase 如果你将有一个亮点尊重情况。 您的方法参数名称是坏的。我看着你的方法签名10倍,还是要看看方法体,以提醒自己在arg为搜索词,这是搜索的文本。给他们打电话搜索关键词文本。 在制作code不使用默认的包。 在制作code不使用的System.out.println()。 您的javadoc需要改进,需要告诉用户的所有的,他们需要了解的code。 我会考虑使用静态方法的一类,没有类变量。 在我还考虑允许用户指定自己的起点和终点高亮标记(我不会用静态方法,如果我这样做)。 我不会修剪()除非被指定为一项要求。如果我这样做的话,显然这种行为将被记录在Javadoc中。

我就不会担心用于搜索的算法,克努特 - 莫里斯 - 普拉特看起来不错,但他们不应该指望你了解它,实现它,除非作业规范的具体要求在字符串搜索经验/专业知识。

I was recently asked to submit a solution to a problem for a job.

Problem: Find a sub-string in a string.

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."

Note that the highlighter doesn't have to have the exact same result on this example, since you are defining what a good snippet is and return the the most relevant snippet with the query terms highlighted.

The most important requirement was to write it as I would write a production code.

My solution was not accepted. How could I have improved it? I know, I could have used:

Knuth–Morris–Pratt algorithm Regex (could I?)

My QUESTION:

What do tech companies take into consideration when they review a code for a job. I submitted the code the same day, does that help in any way?

In one of the comments, it pointed out, it looks like a school code than production code. How? Any suggestions?

My solution: 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));

    }
}

解决方案

A few comments;

You only highlight the first occurance of the search string. You assume that lower case matching is fine. Unless this was specified as a requirement it might be better to provide two methods, one that respects case and one that ignores case. I would probably check the given parameters and throw a NPE if either of them were null. This would be the first thing my method did. I would clearly document this behaviour in the javadoc. Your method mame is bad; findSubstringInQuery's main task isn't to find, it is to highlight and the inQuery part is superflous. Just call the method highlight or maybe highlightIgnoreCase if you are going to have a highlight that respects case. Your method parameter names are bad. I've looked at your method signature 10 times and still have to look at the method body to remind myself which arg is the search term and which is the text to search. Call them searchTerm and text. Production code doesn't use the default package. Production code doesn't use System.out.println(). Your javadoc need improving, it needs to tell the user everything they need to know about the code. I would consider using static methods for a class with no class variables. I would also consider allowing the user to specify their own start and end highlighting markers (I wouldn't use static methods if I did this). I wouldn't trim() unless this was specified as a requirement. If I did, then obviously this behaviour would be documented in the javadoc.

I wouldn't worry about the algorithm used for searching, Knuth-Morris-Pratt looks good but they shouldn't expect you to know about it and implement it unless the job spec specifically asked for experience/expertise in string searching.