开发者

Check even/odd for Palindrome?

Is it a good idea to check for odd/even length of a palindrome number/string? Most snippets I came across don't do this basic test. If length is even, it can't be a palindrome, no?

if len(var) % 2 != 0:
  # could be a palindrome, continue...
else:
  break

Or is it just better (i.e f开发者_开发技巧aster) to start comparing the first and last numbers/letters directly?

Edit: Okay, stupid question, should've thought twice! :)


ABBA - an example of palindrome of four letters meaning even length.

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward...


The easiest way to check for a palindrome is to simply compare the string against it's reverse:

def ispalindrome(s):
   return s == s[::-1]

This uses extended slices with a negative step to walk backwards through s and get the reverse.


baab = palindrome and has length of 4 which is even


Try this:

is_palindrome = lambda s : all(s1==s2 for s1,s2 in zip(s[:len(s)/2],s[-1:-(len(s)+1)/2:-1]))

only checks the front half with the back half, and short-circuits as soon as a mismatch is found.


Simple case: aa.

More complicated case: aaaa.

And so on.


If string.length is even Then : All chars count should be even, so we can not have a char with odd count.

If string.length is odd Then: One char count must be odd, so not all chars' count should be even.

--------------- I implemented the following JavaScript for the follow up roles :

function isStrPermutationOfPalindrome(_str) { // backward = forward
    var isPermutationOfPalindrome = true;
    var _strArr = [..._str];
    var _strArrLength = _strArr.length;
    var counterTable = getCharsTabularFrequencies(_str);
    var countOdd = 0;
    var countEven = 0;
    for (let [ky, val] of counterTable) {
        if (val % 2 == 0) {
            countEven = countEven + 1;
        } else {
            countOdd = countOdd + 1;
        }
    }
    if (_strArrLength % 2 == 0) {
        //Even count of all characters,otherwise false.
        //so can not have a character with odd count.
        if (countOdd != 0) {
            isPermutationOfPalindrome = false;
        }

    } else {
        //Odd count of 1 character
        //so not all chars with even count, only one char of odd count.

        if (countOdd > 1 || countOdd == 0) { //no odd, or more than one odd [ only one odd should be to return true]
            isPermutationOfPalindrome = false;
        }
    }
    return isPermutationOfPalindrome;
}


function getCharsTabularFrequencies(str) {
    str = str.toLowerCase();
    var arr = Object.assign([], str);
    var oMap = new Map();
    var _charCount = 0;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] === ' ') {
            continue;
        }
        _charCount = 0;
        for (let j = 1; j < arr.length; j++) {
            {
                if (arr[i] === arr[j]) {
                    _charCount = _charCount + 1;
                }
            }
        }
        if (i == 0)
            _charCount = _charCount + 1;
        if (!oMap.has(arr[i]))
            oMap.set(arr[i], _charCount)
    }
    return oMap;
}

let _str = 'tactcoapapa';
console.log("Is a string of '" + _str + "' is a permutation of a palindrome ? ANSWER => " + isStrPermutationOfPalindrome(_str));


Even length strings can be palindromes too. Wikipedia doesn't say anything about this restriction.


n=raw_input("Enter a string==>")
n=int(n)

start=0
term=n

while n>0:
    result=n%10
    start=start*10+result
    n=n/10

print start

if term==start:
    print "True"
else:
    print "False"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜