{"id":367,"date":"2022-07-03T21:18:42","date_gmt":"2022-07-03T21:18:42","guid":{"rendered":"https:\/\/funwith.dev\/?page_id=367"},"modified":"2023-07-04T16:04:41","modified_gmt":"2023-07-04T16:04:41","slug":"what-are-palindromes","status":"publish","type":"page","link":"https:\/\/funwithdev.com\/?page_id=367","title":{"rendered":"Palindromes"},"content":{"rendered":"\n<h4 class=\"wp-block-heading has-text-align-center has-primary-color has-subtle-background-background-color has-text-color has-background has-medium-font-size\">What are palindromes?<\/h4>\n\n\n\n<p>The formal definition of a palindrome is a word that reads the same way forwards or backwards. Some examples would be the words \u201cpop\u201d, \u201ctot\u201d or \u201ccivic\u201d. Numbers, names, words, magic word squares, dates, musical and variations, such as ignoring punctuation or other characters can also be palindromes. When researching this topic I was surprised to learn how rich the history of this was and I learned that it dates back to the ancient Greeks because the Greek word <em>palin<\/em> means back and <em>dromos<\/em> means direction [2], [3].<\/p>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-center has-primary-color has-subtle-background-background-color has-text-color has-background has-medium-font-size\">How is this relevant to my life?<\/h4>\n\n\n\n<p>If you are a developer you may have run into this type of interview question already. If you are new to this field it can be intimidating if you have not prepared for a code interview like the ones I am referring to. Tech companies seem to delight in putting candidates through multiple mind game style interview sessions because I guess it is quite the ego trip. I myself have encountered this one on almost every interview I have been to. It is important to know how to explain what this is, how to implement it on the white board and in a virtual coding interview. In the medical field palindromes are seen inside of DNA which have nucleotides sequences which read the same from 5\u2032-end to the 3\u2032-end [4]. Other than what I described above knowing about this is probably not useful unless it came up on some game show for a substantial amount of money or some kind of small talk at get togethers or dinner parties.<\/p>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-center has-subtle-background-background-color has-background has-medium-font-size\">Complexity Analysis<\/h4>\n\n\n\n<p>As simple as a palindrome seems, we can actually analyze how long an iterative algorithm implementation would run. If you had N characters from a word you can complete it in the worst case in O(N). The reason for this is you would only need one for loop as explained above and work your way from the ends towards the middle.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-center has-primary-color has-subtle-background-background-color has-text-color has-background has-medium-font-size\">Real Examples<\/h4>\n\n\n\n<p>As I was preparing for this article I found it interesting how many ways a developer could implement an algorithm to determine if a word is a palindrome or not. The following are just a few of the ones I either figured out on my own or adapted from various sources.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Trivial example in C# for lulz<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted has-primary-color has-subtle-background-background-color has-text-color has-background\" style=\"font-size:12px\">bool isPalindrome = false;\nvar word1 = \"tot\";\nvar word2 = word1.Reverse();\n\nif(word1.SequenceEqual(word2))\n{\n   isPalindrome = true;\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Interface implementation variation 1 <\/h4>\n\n\n\n<ul class=\"wp-block-social-links is-layout-flex wp-block-social-links-is-layout-flex\"><li class=\"wp-social-link wp-social-link-github  wp-block-social-link\"><a href=\"https:\/\/github.com\/rsdev30\/FunWithDev\/blob\/main\/Palindromes\/Fun.With.Dev.Palindromes.Manager\/Strategies\/CheckPalindromeReverseMethod.cs\" class=\"wp-block-social-link-anchor\"><svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" version=\"1.1\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M12,2C6.477,2,2,6.477,2,12c0,4.419,2.865,8.166,6.839,9.489c0.5,0.09,0.682-0.218,0.682-0.484 c0-0.236-0.009-0.866-0.014-1.699c-2.782,0.602-3.369-1.34-3.369-1.34c-0.455-1.157-1.11-1.465-1.11-1.465 c-0.909-0.62,0.069-0.608,0.069-0.608c1.004,0.071,1.532,1.03,1.532,1.03c0.891,1.529,2.341,1.089,2.91,0.833 c0.091-0.647,0.349-1.086,0.635-1.337c-2.22-0.251-4.555-1.111-4.555-4.943c0-1.091,0.39-1.984,1.03-2.682 C6.546,8.54,6.202,7.524,6.746,6.148c0,0,0.84-0.269,2.75,1.025C10.295,6.95,11.15,6.84,12,6.836 c0.85,0.004,1.705,0.114,2.504,0.336c1.909-1.294,2.748-1.025,2.748-1.025c0.546,1.376,0.202,2.394,0.1,2.646 c0.64,0.699,1.026,1.591,1.026,2.682c0,3.841-2.337,4.687-4.565,4.935c0.359,0.307,0.679,0.917,0.679,1.852 c0,1.335-0.012,2.415-0.012,2.741c0,0.269,0.18,0.579,0.688,0.481C19.138,20.161,22,16.416,22,12C22,6.477,17.523,2,12,2z\"><\/path><\/svg><span class=\"wp-block-social-link-label screen-reader-text\">GitHub<\/span><\/a><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted has-primary-color has-subtle-background-background-color has-text-color has-background\" style=\"font-size:12px\">\/\/Interface defined in a different file but provided as \n\/\/reference\n\n public interface IPalindromeInputCheck<TInput>\n {\n     bool IsPalindrome(TInput input);\n }\n\n\/\/We are implementing the interface as part of the Strategy\n\/\/Design pattern.\npublic class CheckPalindromeReverseMethod : IPalindromeInputCheck<string>\n    {\n        public bool IsPalindrome(string input)\n        {\n            if (input is null)\n            {\n                throw new ArgumentNullException(nameof(input));\n            }\n\n            var isPalindrome = false;\n            \n            var word2 = input?.Reverse();\n\n            if (input.SequenceEqual(word2))\n            {\n                isPalindrome = true;\n            }\n\n            return isPalindrome;\n        }\n    }<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Extension method alternative variation 1<\/h4>\n\n\n\n<ul class=\"wp-block-social-links is-layout-flex wp-block-social-links-is-layout-flex\"><li class=\"wp-social-link wp-social-link-github  wp-block-social-link\"><a href=\"https:\/\/github.com\/rsdev30\/FunWithDev\/blob\/main\/Palindromes\/Fun.With.Dev.Palindromes.Manager\/Strategies\/Extensions.cs\" class=\"wp-block-social-link-anchor\"><svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" version=\"1.1\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M12,2C6.477,2,2,6.477,2,12c0,4.419,2.865,8.166,6.839,9.489c0.5,0.09,0.682-0.218,0.682-0.484 c0-0.236-0.009-0.866-0.014-1.699c-2.782,0.602-3.369-1.34-3.369-1.34c-0.455-1.157-1.11-1.465-1.11-1.465 c-0.909-0.62,0.069-0.608,0.069-0.608c1.004,0.071,1.532,1.03,1.532,1.03c0.891,1.529,2.341,1.089,2.91,0.833 c0.091-0.647,0.349-1.086,0.635-1.337c-2.22-0.251-4.555-1.111-4.555-4.943c0-1.091,0.39-1.984,1.03-2.682 C6.546,8.54,6.202,7.524,6.746,6.148c0,0,0.84-0.269,2.75,1.025C10.295,6.95,11.15,6.84,12,6.836 c0.85,0.004,1.705,0.114,2.504,0.336c1.909-1.294,2.748-1.025,2.748-1.025c0.546,1.376,0.202,2.394,0.1,2.646 c0.64,0.699,1.026,1.591,1.026,2.682c0,3.841-2.337,4.687-4.565,4.935c0.359,0.307,0.679,0.917,0.679,1.852 c0,1.335-0.012,2.415-0.012,2.741c0,0.269,0.18,0.579,0.688,0.481C19.138,20.161,22,16.416,22,12C22,6.477,17.523,2,12,2z\"><\/path><\/svg><span class=\"wp-block-social-link-label screen-reader-text\">GitHub<\/span><\/a><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted has-primary-color has-subtle-background-background-color has-text-color has-background\" style=\"font-size:12px\">public static class PalindromeExtensions\n{\n   public static bool IsPalindrome(this string input) \n   {\n        if (input is null)\n        {\n            throw new ArgumentNullException(nameof(input));\n        }\n\n        var isPalindrome = false;\n\n        var word2 = input?.Reverse();\n\n        if (input.SequenceEqual(word2))\n        {\n           isPalindrome = true;\n        }\n\n        return isPalindrome;\n    }\n}\n\n......\n\/\/this allows you to use the IsPalindrome method on types such \n\/\/as the string input \n\/\/without having to instantiate an interface.\n\nstring input = \"abc\";\n\n\/\/ Act\nvar result = input.IsPalindrome();<\/pre>\n\n\n\n<p id=\"anagram\">[1] Anonymous, \u201cAnagrams\u201d retrieved from <a rel=\"noreferrer noopener\" href=\"https:\/\/www.yourdictionary.com\/anagram\" data-type=\"URL\" data-id=\"https:\/\/www.yourdictionary.com\/anagram\" target=\"_blank\">https:\/\/www.yourdictionary.com\/anagram<\/a> <\/p>\n\n\n\n<p id=\"anagram\">        Accessed 4 July 2023<\/p>\n\n\n\n<p>[2] Anonymous, \u201cPalindromes\u201d retrieved from <a rel=\"noreferrer noopener\" href=\"https:\/\/www.yourdictionary.com\/palindrome\" target=\"_blank\">https:\/\/www.yourdictionary.com\/palindrome<\/a><\/p>\n\n\n\n<p>       Accessed 4 July 2023<\/p>\n\n\n\n<p>[3] Britannica, The Editors of Encyclopedia. \u201cpalindrome\u201d. Encyclopedia Britannica, 27 Jun. 2019, <a rel=\"noreferrer noopener\" href=\"https:\/\/www.britannica.com\/art\/palindrome\" target=\"_blank\">https:\/\/www.britannica.com\/art\/palindrome<\/a>. Accessed 2 July 2022.<\/p>\n\n\n\n<p>[4] Giel-Pietraszuk M, Hoffmann M, Dolecka S, Rychlewski J, Barciszewski J. Palindromes in proteins. J Protein Chem. Retrieved from <a rel=\"noreferrer noopener\" href=\"https:\/\/pubmed.ncbi.nlm.nih.gov\/12760415\/\" target=\"_blank\">https:\/\/pubmed.ncbi.nlm.nih.gov\/12760415\/<\/a>  2003 Feb;22(2):109-13. doi: 10.1023\/a:1023454111924. PMID: 12760415.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>What are palindromes? The formal definition of a palindrome is a word that reads the same way forwards or backwards. Some examples would be the words \u201cpop\u201d, \u201ctot\u201d or \u201ccivic\u201d. Numbers, names, words, magic word squares, dates, musical and variations, such as ignoring punctuation or other characters can also be palindromes. When researching this topic [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-367","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/funwithdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=367"}],"version-history":[{"count":7,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/367\/revisions"}],"predecessor-version":[{"id":490,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/367\/revisions\/490"}],"wp:attachment":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}