{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/how-often-does-the-senate-vote-in-palindromes","result":{"data":{"markdownRemark":{"id":"407725fd-9529-56c6-9187-f798a22e1034","html":"<p>Every Friday, <a href=\"https://fivethirtyeight.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">five thirty eight</a> comes out with two logic, math, or probability based puzzles - one quick to solve and one that takes a long time to solve. Although I don’t always get a chance to partake, thinking about them is always fun.</p>\n<p>This week’s quick puzzle related to a problem I’m trying to solve in the office, so I thought I would give this one a try. Taken from <a href=\"https://fivethirtyeight.com/features/how-often-does-the-senate-vote-in-palindromes/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">their site</a>, the puzzle goes like this:</p>\n<blockquote>\n<p>On Monday, <a href=\"https://www.nytimes.com/interactive/2018/01/22/us/politics/live-senate-vote-government-shutdown2.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">the Senate voted</a> 81-18 to end the government shutdown. This naturally grabbed the Riddler’s attention: It’s a palindrome! The vote tally reads the same forward and backward. This specific tally was made possible by the absence of John McCain. But do senators need to be absent to create palindrome tallies? If so, what numbers of absences will do the trick?</p>\n</blockquote>\n<blockquote>\n<p><em>Extra credit:</em> How many palindromic Senate votes have occurred in the <a href=\"https://www.senate.gov/legislative/votes.htm\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">past three decades</a>?</p>\n</blockquote>\n<p>In addition to potentially helping solve a problem in the office, I figure this is a good excuse to work on experimenting with <a href=\"https://en.wikipedia.org/wiki/Object-oriented_programming\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">object oriented programming</a> and writing functions to work with <a href=\"http://adv-r.had.co.nz/OO-essentials.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">S3 methods</a>. As you will see, the function I wrote is more for fun than anything else, but I think it does the trick in showing how s3 methods work. In addition, the first solution to the puzzle I propose is verbose. At the end I show how I solved the problem in an even quicker</p>\n<h2 id=\"lets-begin\" style=\"position:relative;\"><a href=\"#lets-begin\" aria-label=\"lets begin permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Let’s begin!</h2>\n<center>\n<p><img src=\"https://res.cloudinary.com/chrissfriedman/image/upload/v1517458326/drwho_allons_y_ruxxvw.gif\"></p>\n</center>\n<p>The first thing I did when approaching this puzzle was to re-frame my question into something I can write code for.</p>\n<h5 id=\"what-are-the-possible-combinations-of-votes-that-can-result-in-a-palindrome\" style=\"position:relative;\"><a href=\"#what-are-the-possible-combinations-of-votes-that-can-result-in-a-palindrome\" aria-label=\"what are the possible combinations of votes that can result in a palindrome permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What are the possible combinations of votes that can result in a palindrome?</h5>\n<p>When thinking about palindromes, the first thing I need to do is think about what the reversible part of the palindrome is. With senate votes, the first part of the palindrome can be considered the number of “yea” votes while the second part of it can be considered the number of “nay” votes.</p>\n<p>Considering the number of seats in the senate, the number of yea votes can be any number between 0 and 100.</p>\n<p>Considering the problem of palindromes will narrow down <code class=\"language-text\">0:100</code> in two ways.</p>\n<p>First, a unanimous vote in the senate is impossible! Kidding aside, I also know that a unanimous vote can’t result in a palindrome, so the case where there are 100 yea votes can be discarded. In addition, we won’t look at cases were no senators vote!</p>\n<p>Second, Looking at votes between 1 and 99 is doubling the amount of work. Votes in the yea column greater than or equal to 50 are palindromes of the number of votes below 50. So it’s only necessary to look at yea votes where the number of votes is between 1 and 49.</p>\n<p>For illustrative purposes, let’s put that vector in a data frame</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">library(dplyr)\n\nvotes &lt;- data_frame(yea = 1:49)</code></pre></div>\n<p>Now that we have a vector of all of the numbers between 0 and 49, we’ll build a vector of their palindromes and put them in a new column for all of the “nay\"\" votes.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">library(stringr)\nlibrary(purrr)\n\nvotes &lt;- votes %&gt;%\n  mutate(nay = formatC(yea, width = 2, format = &quot;d&quot;, flag = &quot;0&quot;) %&gt;%\n           str_split(&quot;&quot;) %&gt;%\n           map(rev) %&gt;%\n           map_chr(paste, collapse = &quot;&quot;) %&gt;%\n           as.numeric())</code></pre></div>\n<p>In the above code chunk, I introduced a function it seems a lot of people don’t get to play with very often, <a href=\"https://www.rdocumentation.org/packages/base/versions/3.4.3/topics/formatC\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">formatC</code></a>, an interface to the c function <code class=\"language-text\">printf</code>. Basically, it allows users to format a text input. Here, it’s used to add leading zeros.</p>\n<p>Above, there’s also a call to <a href=\"https://www.rdocumentation.org/packages/stringr/versions/1.1.0/topics/str_split\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">str_split</code></a> from the <a href=\"http://stringr.tidyverse.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">stringr</code></a> package. It’s used here to split the two digits so that they can then be reversed.</p>\n<p><code class=\"language-text\">str_split</code> outputs a list of character vectors where each element is the two characters. To operate on this list, <a href=\"https://www.rdocumentation.org/packages/purrr/versions/0.2.4/topics/map\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">map</code></a> from the <a href=\"http://purrr.tidyverse.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">purrr</code></a> package is used, to operate on each element in the list. In this instance <code class=\"language-text\">map</code> applies the <a href=\"https://www.rdocumentation.org/packages/base/versions/3.4.3/topics/rev\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">rev</code></a> function to reverse each item in each element in the list so that <code class=\"language-text\">&quot;8&quot; &quot;1&quot;</code> becomes <code class=\"language-text\">&quot;1&quot; &quot;8&quot;</code>.</p>\n<p>Similarly, <a href=\"http://purrr.tidyverse.org/reference/index.html#section-map-family\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"language-text\">map_chr</code></a> pastes the items in each element together (changing <code class=\"language-text\">&quot;1&quot; &quot;8&quot;</code> to <code class=\"language-text\">&quot;18&quot;</code>) and uses the <code class=\"language-text\">_chr</code> modifier to return a character vector that is then converted into a numeric one.</p>\n<p>After computing the nay votes, we need to narrow down our list of yea’s and nay’s to votes that are possible in the 100 seat senate.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">votes &lt;- votes %&gt;%\n  mutate(vote_count = yea + nay) %&gt;%\n  # Filter our impossible vote counts\n  filter_at(vars(vote_count), all_vars(. &lt;= 100))</code></pre></div>\n<p>If you’ve been playing along at home, you will notice that 10 of the vote counts were dropped.</p>\n<p>Now we have all of the possible vote tallies that are palindromic!</p>\n<h2 id=\"do-senators-need-to-be-absent-to-create-palindrome-tallies\" style=\"position:relative;\"><a href=\"#do-senators-need-to-be-absent-to-create-palindrome-tallies\" aria-label=\"do senators need to be absent to create palindrome tallies permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Do senators need to be absent to create palindrome tallies?</h2>\n<p>This question is answered by seeing if any of the items in <code class=\"language-text\">votes$vote_count</code> equal 100. If they do, then the answer to the question is no.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">any(votes$vote_count == 100)</code></pre></div>\n<p>YES! Senators DO need to be absent for a palindromic vote count!</p>\n<p>What are the number of absences that will do the trick?</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">100 - votes$vote_count %&gt;%\n  unique()</code></pre></div>\n<p>Above, I show the absences needed for a vote to be palindromic. That said, if 50 senators are absent, a quorum is not present, so business isn’t being conducted.</p>\n<p>In actuality, the number of absences can be 49, 34, 23, 12, or 1.</p>","fields":{"slug":"/posts/how-often-does-the-senate-vote-in-palindromes","tagSlugs":["/tag/five-thirty-eight/","/tag/riddler/","/tag/dplyr/","/tag/stringr/","/tag/purrr/"]},"frontmatter":{"date":"2018-01-31","description":"Every Friday, five thirty eight comes out with two logic, math, or probability based puzzles - one quick to solve and one that takes a long time to solve. Although I don't always get a chance to partake, thinking about them is always fun.","tags":["five_thirty_eight","riddler","dplyr","stringr","purrr"],"title":"How often does the Senate Vote In Palindromes?","socialImage":null}}},"pageContext":{"slug":"/posts/how-often-does-the-senate-vote-in-palindromes"}}}