Пример #1
0
        private void IsPalindromeExtraCharactersTest(IPalindromeChecker palindromeChecker)
        {
            int  length = 80;
            var  list   = GeneratePalindromeList(length);
            bool result = palindromeChecker.IsPalindrome(list);

            Assert.AreEqual(true, result);
        }
Пример #2
0
        private List <PalindromeResult> SearchAlgorithm(string input, int max, List <PalindromeResult> result)
        {
            //loop down for search length
            for (int i = input.Length; i > 1; i--)
            {
                //loop up for starting index
                for (int j = 0; j <= input.Length - i; j++)
                {
                    //specific section
                    var subString = input.Substring(j, i);

                    //check if the selected string is a palindrome
                    if (palindromeChecker.IsPalindrome(subString))
                    {
                        //check if the new found palindrome is inside another existing palindrome
                        //alternatively can store the index and range of all the previously found palindromes in an array and skip them the nex time
                        //using LINQ though improves readability and do not need to save the index to another array
                        if (!result.Any(r => (r.Index < j || j == 0) && (r.Index + r.Length) > j))
                        {
                            var distinctResultLengths = result.Select(r => r.Length).Distinct();

                            //only add to result if the result has not exceed the max number of longest unique palindromes want to get
                            //or there are nultiples palindromes with same length
                            if (distinctResultLengths.Count() <= max)
                            {
                                //add to result list if its an unique palindrome
                                result.Add(new PalindromeResult
                                {
                                    Text   = subString,
                                    Index  = j,
                                    Length = i
                                });
                            }
                        }
                    }
                }

                //return results if the results list already reaches the max number of top longest unique palindromes want to get to improve speed
                if (result.Select(r => r.Length).Distinct().Count() == max)
                {
                    break;
                }
            }

            return(result);
        }
        public async Task <IActionResult> IsPalindrome([FromBody, Required] Palindrome palindrome)
        {
            if (_palindromeContext.Palindromes.Any(x => string.Equals(x.Value, palindrome.Value,
                                                                      StringComparison.OrdinalIgnoreCase)))
            {
                return(Ok(true));
            }

            var palindromeCheckResult = await _palindromeChecker.IsPalindrome(palindrome.Value);

            if (palindromeCheckResult)
            {
                _palindromeContext.Palindromes.Add(new Palindrome {
                    Value = palindrome.Value
                });
                await _palindromeContext.SaveChangesAsync();
            }

            return(Ok(palindromeCheckResult));
        }
Пример #4
0
 public async Task must_return_true_for_all_palindrome_strings(string inputString)
 {
     Assert.True(await _palindromeChecker.IsPalindrome(inputString));
 }
Пример #5
0
        public void GIVEN_an_emptystring_WHEN_IsPalindrome_is_called_THEN_should_return_false()
        {
            //Arrange
            var input = "";

            //Act
            var response = checker.IsPalindrome(input);

            //Assert
            Assert.AreEqual(false, response);
        }