6.Conversational AI bots can answer a variety of questions, and even write solutions to trivial problems. In fact, we put a prompt in ChatGPT and got a pretty thorough response. Based on the...




6.


Conversational AI bots can answer a variety of questions, and even write solutions to trivial problems. In fact, we put a prompt in ChatGPT and got a pretty thorough response. Based on the following prompt, ChatGPT gave the user the subsequent solution:



During software development, engineers often must break up data to process it much more efficiently. In this scenario, a team in the office has their dataset in a long string of text which is great for their use, but not for our servers to do anything meaningful with! To do that, we need a function that takes the string and separates them into chunks or "words" as we will call it. Given a string of any length, output an array of the string as "words" of the given length.






Before getting started, some assumptions you should consider in your solution include:






Assume whitespaces could be present and remove them






The input string may contain any character from: a-z, A-Z, or 0-9






The shortest word must appear at the end of the output array






Some edge cases exist that are not disclosed. In your solution, consider some possible inputs that fall outside the normal bounds of the question.



Example 1: Input of "abcdefghijklmnopqrstuvwxyz" "4" should output ["abcd", "efgh", "ijkl" "mnop", "qrst", "uvwx", "yz"]



Example 2: Input of "hellomywonderfulfriend2000" "5" should output ["hello", "mywon", "derfu", "lfrie", "nd200", "0"]






Some data outputs needed by the server are more complex and require some extra manipulation. To fit the data model, they use a range to represent the alternating sizes of the words in the output string. Modify your solution to work in both scenarios.






Example 1: Input of "abcdefghijklmnopqrstuvwxyz" "4-6" should output ["abcd", "efghij", "klmn", "opqrst", "uvwx", "yz"]



Example 2: Input of "hellomywonderfulfriend2000" "1-5" should output ["h", "ellom", "y", "wonde", "r", "fulfr", "i" "end20", "0", "0"]






Not everyone is perfect, including machines. Some datasets need to be sorted so that they can be understood correctly by certain programs. Modify your solution such that the "words" in the output array are sorted using a self-written algorithm. Assume standard sorting of numbers. Uppercase before lowercase.






Example 1: Input of "abcdEfghijklmnoPqrsTuvwxyz" "4-6" should output ["Efghij", "abcd", "klmn", "oPqrsT", "uvwx", "yz"]



Example 2: Input of "hellomywonderfulfriend2000" "1-5" should output ["0", "0", "ellom", "end20", "fulfr", "h", "i", "r", "wonde", "y"]






#include



#include













using namespace std;













vector split_string(string str, string range_str) {






vector result;






int start_len, end_len;






sscanf(range_str.c_str(), "%d-%d", &start_len, &end_len);
















// Remove whitespaces from the string






str.erase(remove(str.begin(), str.end(), ' '), str.end());
















int pos = 0;






while (pos < str.length())="">






// Determine the length of the next word






int len = rand() % (end_len - start_len + 1) + start_len;
















// Make sure we don't go past the end of the string






if (pos + len > str.length()) {






len = str.length() - pos;






}
















// Add the word to the result array






string word = str.substr(pos, len);






result.push_back(word);
















pos += len;






}
















// Sort the words using a custom comparison function






sort(result.begin(), result.end(), [](const string& a, const string& b) {






string::const_iterator ita = a.begin(), itb = b.begin();






while (ita != a.end() && itb != b.end()) {






if (isdigit(*ita) && !isdigit(*itb)) {






return true;






} else if (!isdigit(*ita) && isdigit(*itb)) {






return false;






} else if (isupper(*ita) && !isupper(*itb)) {






return true;






} else if (!isupper(*ita) && isupper(*itb)) {






return false;






} else if (*ita < *itb)="">






return true;






} else if (*ita > *itb) {






return false;






} else {






++ita;






++itb;






}






}






return a.length() <>






});
















return result;



}













int main() {






string str = "abcdEfghijklmnoPqrsTuvwxyz";






string range_str = "4-6";






vector result = split_string(str, range_str);






for (const auto& word : result) {






cout < word="">< "="">






}






cout <>






return 0;



}










Mar 06, 2023
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here