|  | 
|  | 1 | +using System; | 
|  | 2 | + | 
|  | 3 | +namespace Coderbyte_CSharp.Easy_Challenges | 
|  | 4 | +{ | 
|  | 5 | +    class QuestionMarkSum | 
|  | 6 | +    { | 
|  | 7 | +        // Have the function QuestionsMarks(str) take the str string parameter, which  | 
|  | 8 | +        // will contain single digit numbers, letters, and question marks, and check  | 
|  | 9 | +        // if there are exactly 3 question marks between every pair of two numbers  | 
|  | 10 | +        // that add up to 10. If so, then your program should return the string true,  | 
|  | 11 | +        // otherwise it should return the string false.If there aren't any two numbers  | 
|  | 12 | +        // that add up to 10 in the string, then your program should return false as well. | 
|  | 13 | + | 
|  | 14 | +        // For example : if str is "arrb6???4xxbl5???eee5" then your program should return  | 
|  | 15 | +        // true because there are exactly 3 question marks between 6 and 4, and 3 question  | 
|  | 16 | +        // marks between 5 and 5 at the end of the string. | 
|  | 17 | +        public string QuestionsMarks(string str) | 
|  | 18 | +        { | 
|  | 19 | +            string result    = String.Empty; | 
|  | 20 | +            bool   pass      = false; | 
|  | 21 | +            int    firstPos  = -1; | 
|  | 22 | +            int    secondPos = -1; | 
|  | 23 | +            int    index     = 0; | 
|  | 24 | + | 
|  | 25 | +            while (index < str.Length) | 
|  | 26 | +            { | 
|  | 27 | +                if (FindNumbersInString(str, index, out firstPos, out secondPos)) | 
|  | 28 | +                { | 
|  | 29 | + | 
|  | 30 | +                    int first  = (int)Char.GetNumericValue(str[firstPos]); | 
|  | 31 | +                    int second = (int)Char.GetNumericValue(str[secondPos]); | 
|  | 32 | + | 
|  | 33 | + | 
|  | 34 | +                    // 2 integers must equal 10 | 
|  | 35 | +                    if ((first + second) == 10) | 
|  | 36 | +                    { | 
|  | 37 | +                        // There must be exactly 3 question marks between 2 integers | 
|  | 38 | +                        pass = IsQuestionMarksExist(str, firstPos, secondPos); | 
|  | 39 | +                    } | 
|  | 40 | + | 
|  | 41 | +                    index = secondPos + 1; | 
|  | 42 | +                } | 
|  | 43 | + | 
|  | 44 | +                // No numbers found in string | 
|  | 45 | +                else | 
|  | 46 | +                { | 
|  | 47 | +                    pass = false; | 
|  | 48 | +                    break; | 
|  | 49 | +                } | 
|  | 50 | +            } | 
|  | 51 | + | 
|  | 52 | +            result = pass ? "true" : "false"; | 
|  | 53 | + | 
|  | 54 | +            return result; | 
|  | 55 | +        } | 
|  | 56 | + | 
|  | 57 | +        protected bool FindNumbersInString(string str, int start, out int first, out int second) | 
|  | 58 | +        { | 
|  | 59 | +            bool    result = false; | 
|  | 60 | +                    first = -1; | 
|  | 61 | +                    second = -1; | 
|  | 62 | + | 
|  | 63 | +            if (start == str.Length - 1) | 
|  | 64 | +            { | 
|  | 65 | +                return false; | 
|  | 66 | +            } | 
|  | 67 | + | 
|  | 68 | +            for (int index = start; index < str.Length; index++) | 
|  | 69 | +            { | 
|  | 70 | +                if (Char.IsDigit(str[index])) | 
|  | 71 | +                { | 
|  | 72 | +                    if (first == -1) | 
|  | 73 | +                    { | 
|  | 74 | +                        first = index; | 
|  | 75 | +                    } | 
|  | 76 | + | 
|  | 77 | +                    else | 
|  | 78 | +                    { | 
|  | 79 | +                        second = index; | 
|  | 80 | +                        break; | 
|  | 81 | +                    } | 
|  | 82 | +                } | 
|  | 83 | +            } | 
|  | 84 | + | 
|  | 85 | +            result = (first != -1 && second != -1); | 
|  | 86 | + | 
|  | 87 | +            return result; | 
|  | 88 | +        } | 
|  | 89 | + | 
|  | 90 | +        protected  bool IsQuestionMarksExist(string str, int start, int end) | 
|  | 91 | +        { | 
|  | 92 | +            bool result = false; | 
|  | 93 | + | 
|  | 94 | +            int num1  = start + 1; | 
|  | 95 | +            int num2  = end; | 
|  | 96 | +            int count = 0; | 
|  | 97 | + | 
|  | 98 | +            if (num2 - num1 >= 3) | 
|  | 99 | +            { | 
|  | 100 | +                for (int index = num1; index < num2; index++) | 
|  | 101 | +                { | 
|  | 102 | +                    if (str[index] == '?') | 
|  | 103 | +                    { | 
|  | 104 | +                        count++; | 
|  | 105 | +                    } | 
|  | 106 | +                } | 
|  | 107 | + | 
|  | 108 | +                result = (count == 3); | 
|  | 109 | +            } | 
|  | 110 | + | 
|  | 111 | + | 
|  | 112 | +            return result; | 
|  | 113 | +        } | 
|  | 114 | +    } | 
|  | 115 | +} | 
0 commit comments