In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to build a string wrapper function". In daily operation, I believe many people have doubts about how to build a string wrapper function. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "how to build a string wrapper function"! Next, please follow the editor to study!
Problem description
We want to build a string handler that will handle the strings and parameters given the input.
We will first define an array of strings in which each element is stored as a word, and we will give an integer type parameter. Your method will operate on the two parameters entered above, adding a character "-" between each word and each word, and the string length of each element of the newly generated array or List will not exceed the length of the given string.
If your newly generated element ends with a horizontal bar, then you need to delete the horizontal bar.
Sample
An example of this problem is given below for your reference.
Input parameter 1 input parameter 2 output words1 = ["The", "day", "began", "as", "still", "as", "the", "night", "abruptly", "lighted", "with", "brilliant", "flame"] 13 ["The-day-began", "as-still-as", "the-night", "abruptly", "lighted-with", "brilliant" "flame"] words1 = ["The", "day", "began", "as", "still", "as", "the", "night", "abruptly", "lighted", "with", "brilliant", "flame"] 20 ["The-day-began-as", "still-as-the-night", "abruptly-lighted" "with-brilliant-flame"] words2 = ["Hello"] 5 ["Hello"] words3 = ["Hello", "world"] 5 ["Hello", "world"] words4 = ["Well", "Hello", "world"] 5 ["Well", "Hello", "world"] words5 = ["Hello", "HelloWorld", "Hello", "Hello"] 20 ["Hello-HelloWorld", "Hello-Hello"]
The example given above is for testing, where the input parameters words1 to 5 are just defined variable names and do not require too much attention.
Ideas comments and source code
This topic is still quite difficult, especially when there are no development tools to compile.
This question is an online interview test for indeed.com. The company's online interview test uses an evaluation tool provided by a third-party company, and the person conducting the interview may be someone who doesn't know much about the technology, or someone who knows the technology better, and we don't know the details.
But in the interview process, they only focus on the output of the program and the results of the interview. As for your thoughts or thoughts, the person hosting the interview may not pay much attention to you and will not listen to your explanation at the same time. A lot of times you're talking to yourself.
On the whole, it feels that there is little interaction in the interview, and it is more likely that you are talking to the screen.
In the beginning, my idea was to first iterate through the given array, add a crossbar after the first element, and then compare it with the length. if the length exceeds the given length, delete the crossbar and press it into the list that needs to be returned.
If the length is less than the returned length, then take out the next element, and then add the crossbar at the end to judge, and then determine the crossbar processing.
The main problem with this topic is the handling of horizontal bars, sometimes at the end and sometimes at the beginning, you need to interpret them one by one.
In the subsequent tests, I found that the horizontal bars were not handled well, resulting in not fully passing the final test, which is the second line of the test case above.
Idea 2
Because the time for this topic is limited, and we can't use StringUtils for some fast string processing, I didn't finish all the tests within the specified time. At the end of the interview, I thought carefully about the question and found that there were other ways to do it.
I used the following ideas and finished modifying the code.
First, we need to change the input array into a long string, with words separated by horizontal bars. For example, ["Hello", "world"] will become a string of: Hello-world.
After completing the above operation, we need to use a while loop to do this.
First read in the while loop that the length of the entire string is less than the given length, which requires a direct return, and then interrupts the loop.
Next, truncate the string from the beginning to the given length to get the substring, and then judge the substring. If the substring ends with a horizontal bar, delete the horizontal bar and press into the array to be returned. Then update the string to be truncated to the rest of the string.
If you truncate according to the given length and the last character is not a crossbar, then we know that you truncated the word, found the last horizontal bar in the obtained substring, and then got the ID of the index. After getting the ID of the index, the string to be processed is truncated according to the index ID.
Then delete the last horizontal bar and press it into the list to be returned.
What you may encounter in the rest of the string is that currently you will be at the beginning of the horizontal bar, so you also need to remove the beginning and end of the remaining string.
Continue the above processing until the length of the string to be processed is less than the given length and the loop is interrupted.
The above figure is the result of testing the algorithm in idea 2 above, from which you can see the expected output that meets the need.
At this point, the study on "how to build a string wrapper function" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.