Created
September 28, 2021 16:44
-
-
Save LeBaleiro/957250da5bf90140dc8c7f06b8d61de4 to your computer and use it in GitHub Desktop.
Questions Marks
This file contains 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 QuestionsMarks(str) take the str string parameter, which will contain single digit numbers, letters, and question marks, and check if there are exactly 3 question marks between every pair of two numbers that add up to 10. If so, then your program should return the string true, otherwise it should return the string false. If there aren't any two numbers that add up to 10 in the string, then your program should return false as well. | |
// For example: if str is "arrb6???4xxbl5???eee5" then your program should return true because there are exactly 3 question marks between 6 and 4, and 3 question marks between 5 and 5 at the end of the string. | |
// Examples | |
// Input: "aa6?9" | |
// Output: false | |
// Input: "acc?7??sss?3rr1??????5" | |
// Output: true | |
// Input: "bbb???2sa??aa?8" | |
void main() { | |
bool isANumber(String character) { | |
final succed = int.tryParse(character); | |
return succed != null; | |
} | |
List<int> getNumbersIndexes(String word) { | |
var indexOfNumberList = <int>[]; | |
for (int i = 0; i < word.length; i++) { | |
final isNumber = isANumber(word[i]); | |
if (isNumber) { | |
indexOfNumberList.add(i); | |
} | |
} | |
return indexOfNumberList; | |
} | |
List<String> getSubstrings(String word) { | |
final List<int> indexesOfNumbers = getNumbersIndexes(word); | |
final subStrings = <String>[]; | |
for (int i = 0; i < indexesOfNumbers.length - 1; i++) { | |
final newString = | |
word.substring(indexesOfNumbers[i], indexesOfNumbers[i + 1] + 1); | |
subStrings.add(newString); | |
} | |
return subStrings; | |
} | |
bool hasThreeQuestionsMarks(String word) { | |
final regex = RegExp(r"\?.*?\?.*?\?"); | |
return regex.hasMatch(word); | |
} | |
bool meetsRequirements(String word) { | |
final List<String> subStrings = getSubstrings(word); | |
bool appliesToAllRequirements = false; | |
for (String subString in subStrings) { | |
final hasQuestionMarks = hasThreeQuestionsMarks(subString); | |
final firstNumber = int.parse(subString[0]); | |
final lastNumber = int.parse(subString[subString.length - 1]); | |
int numberSum = firstNumber + lastNumber; | |
final sumIsTen = numberSum == 10; | |
appliesToAllRequirements = | |
appliesToAllRequirements || (sumIsTen && hasQuestionMarks); | |
} | |
return appliesToAllRequirements; | |
} | |
String stringToTest = "bbb???2sa??aa?8"; | |
final hasQuestionMarksAndAddUptoTen = meetsRequirements(stringToTest); | |
print(hasQuestionMarksAndAddUptoTen); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment