Created
August 26, 2023 15:00
-
-
Save Mastersam07/67319a947a977d2fbd6e61be677ad310 to your computer and use it in GitHub Desktop.
Array challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Have the function ArrayChallenge(strArr) | |
| // read the array of strings stored in strArr, which will contain 2 elements: | |
| // the first element will be a sequence of characters, | |
| // and the second element will be a long string of comma-separated words, in alphabetical order, | |
| // that represents a dictionary of some arbitrary length. | |
| // For example: strArr can be: ["hellocat", "apple,bat,cat,goodbye,hello,yellow,why"]. | |
| // Your goal is to determine if the first element in the input can be split into two words, | |
| // where both words exist in the dictionary that is provided in the second input. | |
| // In this example, the first element can be split into two words: | |
| // hello and cat because both of those words are in the dictionary. | |
| // Your program should return the two words that exist in the dictionary separated by a comma. | |
| // So for the example above, your program should return hello,cat. | |
| // There will only be one correct way to split the first element of characters into two words. | |
| // If there is no way to split string into two words that exist in the dictionary, return the string not possible. | |
| // The first element itself will never exist in the dictionary as a real word. | |
| // Once your function is working, take the final output string and concatenate it with your ChallengeToken, | |
| // and then replace every third character with an X. | |
| // Your ChallengeToken: pq7kbawue985 | |
| // Examples | |
| // Input: ["baseball", "a,all,b,ball,bas,base,cat,code,d,e,quit,z"] | |
| // Output: base,ball | |
| // Final Output: baXe,XalXpqXkbXwuX98X | |
| // Input: ["abcgefd", "a,ab,abc,abcg,b,c,dog,e,efd,zzzz"] | |
| // Output: abcg,efd | |
| // Final Output: abXg,XfdXq7XbaXueX85 | |
| String arrayChallenge(List<String> strArr) { | |
| String word = strArr[0]; | |
| List<String> dictionary = strArr[1].split(","); | |
| final dictionarySet = dictionary.toSet(); | |
| final prefixes = <String>{}; | |
| for (int i = 1; i < word.length; ++i) { | |
| String prefix = word.substring(0, i); | |
| if (dictionarySet.contains(prefix)) { | |
| prefixes.add(prefix); | |
| } | |
| } | |
| for (String prefix in prefixes) { | |
| String suffix = word.substring(prefix.length); | |
| if (dictionarySet.contains(suffix)) { | |
| return '$prefix,$suffix'; | |
| } | |
| } | |
| return "not possible"; | |
| } | |
| String generateFinalOutput(String output, String token) { | |
| String combinedValue = output + token; | |
| List<String> characters = combinedValue.split(''); | |
| for (int i = 2; i < characters.length; i += 3) { | |
| characters[i] = 'X'; | |
| } | |
| return characters.join(''); | |
| } | |
| void main() { | |
| String challengeToken = "pq7kbawue985"; | |
| print(generateFinalOutput( | |
| arrayChallenge(["baseball", "a,all,b,ball,bas,base,cat,code,d,e,quit,z"]), | |
| challengeToken)); | |
| print(generateFinalOutput( | |
| arrayChallenge(["abcgefd", "a,ab,abc,abcg,b,c,dog,e,efd,zzzz"]), | |
| challengeToken)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment