palindrome

  • sqlserver12345 (10/20/2013)


    I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanks

    That's exactly what we're doing by showing you the right way! Assuming this is homework, you should be challenging your instructor by delivering outstanding results.

    Easy if you happen to have a pattern-based string splitter in your toolbox.

    WITH SampleData (palindrome) AS

    (

    SELECT 'Madam I''m Adam'

    UNION ALL SELECT 'ABC'

    UNION ALL SELECT 'ABBA'

    UNION ALL SELECT 'A man, a plan, a canal - Panama!'

    UNION ALL SELECT 'Madam in Eden, I''m Adam'

    UNION ALL SELECT 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod'

    UNION ALL SELECT 'Never odd or even'

    ),

    SplitStrings AS

    (

    SELECT palindrome

    ,RemovePuncuation=

    (

    SELECT Item + ''

    FROM SampleData b

    CROSS APPLY dbo.PatternSplitCM(palindrome, '[A-Za-z]')

    WHERE a.palindrome = b.palindrome AND [Matched] = 1

    ORDER BY ItemNumber

    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'

    )

    FROM SampleData a

    )

    SELECT palindrome

    ,IsPalindrome=CASE WHEN REVERSE(RemovePuncuation) = RemovePuncuation THEN 1 ELSE 0 END

    FROM SplitStrings;

    The PatternSplitCM FUNCTION can be found in the fourth link in my signature.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (10/20/2013)


    sqlserver12345 (10/20/2013)


    I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanks

    That's exactly what we're doing by showing you the right way! Assuming this is homework, you should be challenging your instructor by delivering outstanding results.

    Easy if you happen to have a pattern-based string splitter in your toolbox.

    WITH SampleData (palindrome) AS

    (

    SELECT 'Madam I''m Adam'

    UNION ALL SELECT 'ABC'

    UNION ALL SELECT 'ABBA'

    UNION ALL SELECT 'A man, a plan, a canal - Panama!'

    UNION ALL SELECT 'Madam in Eden, I''m Adam'

    UNION ALL SELECT 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod'

    UNION ALL SELECT 'Never odd or even'

    ),

    SplitStrings AS

    (

    SELECT palindrome

    ,RemovePuncuation=

    (

    SELECT Item + ''

    FROM SampleData b

    CROSS APPLY dbo.PatternSplitCM(palindrome, '[A-Za-z]')

    WHERE a.palindrome = b.palindrome AND [Matched] = 1

    ORDER BY ItemNumber

    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'

    )

    FROM SampleData a

    )

    SELECT palindrome

    ,IsPalindrome=CASE WHEN REVERSE(RemovePuncuation) = RemovePuncuation THEN 1 ELSE 0 END

    FROM SplitStrings;

    The PatternSplitCM FUNCTION can be found in the fourth link in my signature.

    The problem is, you used REVERSE which the OP said he couldn't do. Heh... you also forget that a Palindrome can contain numeric digits. 😀

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (10/20/2013)


    The problem is, you used REVERSE which the OP said he couldn't do. Heh... you also forget that a Palindrome can contain numeric digits. 😀

    Nah... didn't forget. Ignored is more like it. We still haven't heard a good reason why REVERSE is not allowed.

    Change the pattern matching string to [a-zA-Z0-9] to include digits. 😀


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • nothing just ...just learning some coding

  • sqlserver12345 (10/21/2013)


    nothing just ...just learning some coding

    In that case, I strongly recommend that you drop the whole idea of using a WHILE loop and not using REVERSE. Might as well learn the right way to do something like this. 🙂

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (10/21/2013)


    In that case, I strongly recommend that you drop the whole idea of using a WHILE loop ...

    Just in case the OP can't, I suppose there are lessons to be learned in doing things the wrong way...

    DECLARE @Count INT = 1

    ,@String VARCHAR(8000) = 'ABBAXABBA';

    SET @String = RTRIM(LTRIM(@String));

    WHILE @Count <= LEN(@String) / 2 AND

    RIGHT(LEFT(@String, @Count), 1) = LEFT(RIGHT(@String, @Count), 1)

    SET @Count = @Count + 1;

    IF @Count < LEN(@String)/ 2 PRINT 'Is NOT Palindrome';

    ELSE PRINT 'Is Palindrome';

    Obviously this ignores punctuation.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 6 posts - 16 through 20 (of 20 total)

You must be logged in to reply to this topic. Login to reply