palindrome

How does this PCRE pattern detect palindromes?

耗尽温柔 提交于 2019-11-27 21:16:46
This question is an educational demonstration of the usage of lookahead, nested reference, and conditionals in a PCRE pattern to match ALL palindromes, including the ones that can't be matched by the recursive pattern given in the PCRE man page. Examine this PCRE pattern in PHP snippet: $palindrome = '/(?x) ^ (?: (.) (?= .* ( \1 (?(2) \2 | ) ) $ ) )* .? \2? $ /'; This pattern seems to detect palindromes, as seen in this test cases ( see also on ideone.com ): $tests = array( # palindromes '', 'a', 'aa', 'aaa', 'aba', 'aaaa', 'abba', 'aaaaa', 'abcba', 'ababa', # non-palindromes 'aab', 'abab',

Recursive Function palindrome in Python [closed]

白昼怎懂夜的黑 提交于 2019-11-27 20:18:34
I need help writing a recursive function which detects whether a string is a palindrome. But i can't use any loops it must be recursive. Can anyone help show me how this is done. I need to learn this for an upcoming midterm. Im using Python. def ispalindrome(word): if len(word) < 2: return True if word[0] != word[-1]: return False return ispalindrome(word[1:-1]) And here is the best one liner def ispalindrome(word): return word == word[::-1] From a general algorithm perspective, the recursive function has 3 cases: 1) 0 items left . Item is a palindrome , by identity. 2) 1 item left . Item is a

Check if a string is a palindrome

心不动则不痛 提交于 2019-11-27 19:09:21
I have a string as input and have to break the string in two substrings. If the left substring equals the right substring then do some logic. How can I do this? Sample: public bool getStatus(string myString) { } Example: myString = "ankYkna" , so if we break it into two substring it would be: left-part = "ank" , right-part = "ank" (after reversal). ionden public static bool getStatus(string myString) { string first = myString.Substring(0, myString.Length / 2); char[] arr = myString.ToCharArray(); Array.Reverse(arr); string temp = new string(arr); string second = temp.Substring(0, temp.Length /

Create palindrome from existing string by removing characters

我的未来我决定 提交于 2019-11-27 18:48:24
问题 How do i determine the length of the longest palindrome you can get from a word by removing zero or more letters. for eg : amanQQQapl12345anacaZZZnalpaXXXna67890ma longest palindrome will be of 21 digits. 回答1: This can be solved by dynamic programming. Define d[i, j] as the length of longest palindrome in the original string. If s[i] = s[j], d[i, j] = max(d[i+1, j-1] + 2, d[i, j-1], d[i+1, j]). Otherwise d[i, j] = max(d[i, j-1], d[i+1, j]). 回答2: The longest palindrome in the word W is the

Recursive Prolog predicate for reverse / palindrome

柔情痞子 提交于 2019-11-27 15:34:40
Can I get a recursive Prolog predicate having two arguments, called reverse, which returns the inverse of a list: Sample query and expected result: ?- reverse([a,b,c], L). L = [c,b,a]. A recursive Prolog predicate of two arguments called palindrome which returns true if the given list is palindrome. Sample query with expected result: ?- palindrome([a,b,c]). false. ?- palindrome([b,a,c,a,b]). true. Ad 1: It is impossible to define reverse/2 as a ( directly edit thx to @repeat: tail) recursive predicate - unless you permit an auxiliary predicate. Ad 2: palindrome(X) :- reverse(X,X). But the

Creating a recursive method for Palindrome

[亡魂溺海] 提交于 2019-11-27 13:49:37
I am trying to create a Palindrome program using recursion within Java but I am stuck, this is what I have so far: public static void main (String[] args){ System.out.println(isPalindrome("noon")); System.out.println(isPalindrome("Madam I'm Adam")); System.out.println(isPalindrome("A man, a plan, a canal, Panama")); System.out.println(isPalindrome("A Toyota")); System.out.println(isPalindrome("Not a Palindrome")); System.out.println(isPalindrome("asdfghfdsa")); } public static boolean isPalindrome(String in){ if(in.equals(" ") || in.length() == 1 ) return true; in= in.toUpperCase(); if

How does this Java regex detect palindromes?

江枫思渺然 提交于 2019-11-27 13:34:26
This is the third part in a series of educational regex articles. It follows How does this regex find triangular numbers? (where nested references is first introduced) and How can we match a^n b^n with Java regex? (where the lookahead "counting" mechanism is further elaborated upon). This part introduces a specific form of nested assertion, which when combined with nested references allows Java regex to match what most people believe is "impossible": palindromes!! The language of palindromes is non- regular ; it's actually context-free (for a given alphabet). That said, modern regex

Palindrome detection efficiency

橙三吉。 提交于 2019-11-27 13:15:21
问题 I got curious by Jon Limjap's interview mishap and started to look for efficient ways to do palindrome detection. I checked the palindrome golf answers and it seems to me that in the answers are two algorithms only, reversing the string and checking from tail and head. def palindrome_short(s): length = len(s) for i in xrange(0,length/2): if s[i] != s[(length-1)-i]: return False return True def palindrome_reverse(s): return s == s[::-1] I think neither of these methods are used in the

How to write palindrome in JavaScript

。_饼干妹妹 提交于 2019-11-27 03:43:20
I wonder how to write palindrome in javascript, where I input different words and program shows if word is palindrome or not. For example word noon is palindrome, while bad is not. Thank you in advance. tnanoba function palindrome(str) { var len = str.length; var mid = Math.floor(len/2); for ( var i = 0; i < mid; i++ ) { if (str[i] !== str[len - 1 - i]) { return false; } } return true; } palindrome will return if specified word is palindrome, based on boolean value (true/false) UPDATE: I opened bounty on this question due to performance and I've done research and here are the results: If we

How to check if the binary representation of an integer is a palindrome?

删除回忆录丶 提交于 2019-11-27 02:58:36
问题 How to check if the binary representation of an integer is a palindrome? 回答1: Since you haven't specified a language in which to do it, here's some C code (not the most efficient implementation, but it should illustrate the point): /* flip n */ unsigned int flip(unsigned int n) { int i, newInt = 0; for (i=0; i<WORDSIZE; ++i) { newInt += (n & 0x0001); newInt <<= 1; n >>= 1; } return newInt; } bool isPalindrome(int n) { int flipped = flip(n); /* shift to remove trailing zeroes */ while (!