http://oflannabhra.com/ Tue, 04 Aug 2015 23:33:57 -0400 Tue, 04 Aug 2015 23:33:57 -0400 Jekyll v2.5.3 Xcode Tabs <p>I was getting pretty frustrated with Xcode 6 the other day. Specifically, I was getting irritated by constantly having to fiddle with my layout continually getting adjusted. </p> <h2>The Problem</h2> <p>Like many IDEs, Xcode&#39;s layout is divided into panes.</p> <ul> <li>Navigator (left side pane)</li> <li>Editor (center)</li> <li>Utilities (right side pane)</li> <li>Debug/Console (bottom pane)</li> </ul> <p>Each of these have quite a few different contextual modes</p> <p><img src="http://oflannabhra.com/assets/img/navigator.png" alt="The Navigator Pane selction menu"></p> <h3>Navigator Pane</h3> <ul> <li>Project Navigator (<code>⌘ 1</code>)</li> <li>Symbol Navigator (<code>⌘ 2</code>) </li> <li>Find Navigator (<code>⌘ 3</code>)</li> <li>Issue Navigator (<code>⌘ 4</code>)</li> <li>Test Navigator (<code>⌘ 5</code>)</li> <li>Debug Navigator (<code>⌘ 6</code>)</li> <li>Breakpoint Navigator (<code>⌘ 7</code>)</li> <li>Report Navigator (<code>⌘ 8</code>)</li> </ul> <p><img src="http://oflannabhra.com/assets/img/utility.png" alt="The Utility / Inspector Pane selection menu"></p> <h3>Utility Pane</h3> <ul> <li>File Inspector (<code>Opt ⌘ 1</code>)</li> <li>Quick Help Inspector (<code>Opt ⌘ 2</code>)</li> <li>Identity Inspector (<code>Opt ⌘ 3</code>)</li> <li>Attributes Inspector (<code>Opt ⌘ 4</code>)</li> <li>Size Inspector (<code>Opt ⌘ 5</code>)</li> <li>Connections Inspector (<code>Opt ⌘ 6</code>)</li> </ul> <p><img src="http://oflannabhra.com/assets/img/editor.png" alt="The Editor Pane selection menu"></p> <h3>Editor</h3> <ul> <li>Standard Editor (<code>⌘ Ret</code>)</li> <li>Assistant Editor (<code>Opt ⌘ Ret</code>)</li> <li>Version Editor (<code>Opt Shft ⌘ Ret</code>)</li> </ul> Fri, 10 Oct 2014 00:00:00 -0400 http://oflannabhra.com/code/2014/10/10/xcode-tabs.html http://oflannabhra.com/code/2014/10/10/xcode-tabs.html code Trello Hash <p>Our team was looking into project / client management solutions the other day and I came across <a href="http://www.trello.com">Trello</a>. While looking around their site, I stumbled upon their <a href="https://trello.com/jobs/developer">developer</a> job listing, which included a challenge to solve as part of the application process. It looked pretty fun, so I tackled it when I got home. </p> <h2>The Challenge</h2> <p>The challenge, in a nutshell, is to reverse engineer a sample hash function. The give pseudocode for the function is as follows:</p> <figure class='code-highlight-figure'><div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="n">Int64</span> <span class="nf">hash</span> <span class="p">(</span><span class="n">String</span> <span class="n">s</span><span class="p">)</span> <span class="p">&#x7b;</span> </div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="n">Int64</span> <span class="n">h</span> <span class="p">=</span> <span class="m">7</span> </div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="n">String</span> <span class="n">letters</span> <span class="p">=</span> <span class="s">&quot;acdegilmnoprstuw&quot;</span> </div></div><div data-line='4' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">for</span><span class="p">(</span><span class="n">Int32</span> <span class="n">i</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&lt;</span> <span class="n">s</span><span class="p">.</span><span class="n">length</span><span class="p">;</span> <span class="n">i</span><span class="p">++)</span> <span class="p">&#x7b;</span> </div></div><div data-line='5' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="n">h</span> <span class="p">=</span> <span class="p">(</span><span class="n">h</span> <span class="p">*</span> <span class="m">37</span> <span class="p">+</span> <span class="n">letters</span><span class="p">.</span><span class="n">indexOf</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="p">]))</span> </div></div><div data-line='6' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="p">&#x7d;</span> </div></div><div data-line='7' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">return</span> <span class="n">h</span> </div></div><div data-line='8' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="p">&#x7d;</span></div></div></pre></div></figure> <p>The function should work as follows: <code>hash(&quot;leepadg&quot;)</code> returns <code>680131659347</code>. The question is, what string returns <code>956446786872726</code>?</p> <h2>My solution</h2> <p>To really get an understanding of the pseudocode, I decided to implement it in <code>php</code>. Essentially a one-to-one translation is the following:</p> <figure class='code-highlight-figure'><div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="k">function</span> <span class="nf">hash2</span><span class="p">(</span><span class="nv">$s</span><span class="p">)</span> </div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="p">&#x7b;</span> </div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="nv">$h</span> <span class="o">=</span> <span class="mi">7</span><span class="p">;</span> </div></div><div data-line='4' class='code-highlight-row numbered'><div class='code-highlight-line'> </div></div><div data-line='5' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="nv">$letters</span> <span class="o">=</span> <span class="s2">&quot;acdegilmnoprstuw&quot;</span><span class="p">;</span> </div></div><div data-line='6' class='code-highlight-row numbered'><div class='code-highlight-line'> </div></div><div data-line='7' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">for</span> <span class="p">(</span><span class="nv">$i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="nv">$i</span> <span class="o">&lt;</span> <span class="nb">strlen</span><span class="p">(</span><span class="nv">$s</span><span class="p">);</span> <span class="nv">$i</span><span class="o">++</span><span class="p">)</span> <span class="p">&#x7b;</span> </div></div><div data-line='8' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="nv">$pos</span> <span class="o">=</span> <span class="nb">strpos</span><span class="p">(</span><span class="nv">$letters</span><span class="p">,</span> <span class="nv">$s</span><span class="p">[</span><span class="nv">$i</span><span class="p">]);</span> </div></div><div data-line='9' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">if</span> <span class="p">(</span><span class="nv">$pos</span> <span class="o">===</span> <span class="k">false</span><span class="p">)</span> <span class="p">&#x7b;</span> </div></div><div data-line='10' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="nv">$char</span> <span class="o">=</span> <span class="nv">$s</span><span class="p">[</span><span class="nv">$i</span><span class="p">];</span> </div></div><div data-line='11' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="nb">unset</span><span class="p">(</span><span class="nv">$pos</span><span class="p">);</span> </div></div><div data-line='12' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">throw</span> <span class="k">new</span> <span class="nx">ErrorException</span><span class="p">(</span><span class="s2">&quot;The character </span><span class="si">&#x7b;</span><span class="nv">$char</span><span class="si">&#x7d;</span><span class="s2"> does not exist in the string </span><span class="si">&#x7b;</span><span class="nv">$letters</span><span class="si">&#x7d;</span><span class="s2">&quot;</span><span class="p">);</span> </div></div><div data-line='13' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="p">&#x7d;</span> <span class="k">else</span> <span class="p">&#x7b;</span> </div></div><div data-line='14' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="nv">$h</span> <span class="o">=</span> <span class="p">(</span><span class="nv">$h</span> <span class="o">*</span> <span class="mi">37</span> <span class="o">+</span> <span class="nv">$pos</span><span class="p">);</span> </div></div><div data-line='15' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="p">&#x7d;</span> </div></div><div data-line='16' class='code-highlight-row numbered'><div class='code-highlight-line'> </div></div><div data-line='17' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="p">&#x7d;</span> </div></div><div data-line='18' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">return</span> <span class="nv">$h</span><span class="p">;</span> </div></div><div data-line='19' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="p">&#x7d;</span></div></div></pre></div></figure> <h3>Reimplementing the function recursively</h3> <p>While going through this, it seemed to me that this would be pretty easy to implement recursively, and I definitely felt that to reverse the hash, I&#39;d have to do it recursively. So, I rewrite the hash function using recursion. To do this, we need to also know how deep to recurse (so that we don&#39;t create an infinite recursion loop). So, as a second parameter, we are going to pass <code>strlen($s)</code>.</p> <figure class='code-highlight-figure'><div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="k">function</span> <span class="nf">recursive_hash2</span><span class="p">(</span><span class="nv">$s</span><span class="p">,</span> <span class="nv">$j</span><span class="p">)</span> </div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="p">&#x7b;</span> </div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="nv">$letters</span> <span class="o">=</span> <span class="s2">&quot;acdegilmnoprstuw&quot;</span><span class="p">;</span> </div></div><div data-line='4' class='code-highlight-row numbered'><div class='code-highlight-line'> </div></div><div data-line='5' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">if</span> <span class="p">(</span><span class="nv">$j</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">&#x7b;</span> </div></div><div data-line='6' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">return</span> <span class="mi">7</span><span class="p">;</span> </div></div><div data-line='7' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="p">&#x7d;</span> <span class="k">else</span> <span class="p">&#x7b;</span> </div></div><div data-line='8' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">return</span> <span class="nx">recursive_hash2</span><span class="p">(</span><span class="nv">$s</span><span class="p">,</span> <span class="o">--</span><span class="nv">$j</span><span class="p">)</span> <span class="o">*</span> <span class="mi">37</span> <span class="o">+</span> <span class="nb">strpos</span><span class="p">(</span><span class="nv">$letters</span><span class="p">,</span> <span class="nv">$s</span><span class="p">[</span><span class="nv">$j</span><span class="p">]);</span> </div></div><div data-line='9' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="p">&#x7d;</span> </div></div><div data-line='10' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="p">&#x7d;</span></div></div></pre></div></figure> <h3>Reversing the function</h3> <p>So, in thinking about reversing the hash, I knew that we&#39;d be able to at least get the last letter of the final string (ie, the <code>strpos($letters, $s[$j])</code>) by <code>$number % 37</code>. Then, we could reduce the number by reversing the math with <code>$number - ($number % 37) / 37</code>. I tested this out manually, and verified that it worked, and knew this result is what we would recurse on. But, we&#39;d need to kill recursion at some point, right? That math would just go into negative numbers. So, we&#39;d need a test to kill the recursion. That led me to this final solution:</p> <figure class='code-highlight-figure'><div class='code-highlight'><pre class='code-highlight-pre'><div data-line='1' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="k">function</span> <span class="nf">reverse_hash2</span><span class="p">(</span><span class="nv">$number</span><span class="p">)</span> </div></div><div data-line='2' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="p">&#x7b;</span> </div></div><div data-line='3' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="nv">$letters</span> <span class="o">=</span> <span class="s2">&quot;acdegilmnoprstuw&quot;</span><span class="p">;</span> </div></div><div data-line='4' class='code-highlight-row numbered'><div class='code-highlight-line'> </div></div><div data-line='5' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="nv">$reduced</span> <span class="o">=</span> <span class="p">(</span><span class="nv">$number</span> <span class="o">-</span> <span class="p">(</span><span class="nv">$number</span> <span class="o">%</span> <span class="mi">37</span><span class="p">))</span> <span class="o">/</span> <span class="mi">37</span><span class="p">;</span> </div></div><div data-line='6' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">if</span> <span class="p">(</span><span class="nv">$number</span> <span class="o">&lt;</span> <span class="mi">16</span><span class="p">)</span> <span class="p">&#x7b;</span> </div></div><div data-line='7' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">return</span> <span class="k">null</span><span class="p">;</span> </div></div><div data-line='8' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="p">&#x7d;</span> <span class="k">else</span> <span class="p">&#x7b;</span> </div></div><div data-line='9' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="k">return</span> <span class="nx">reverse_hash2</span><span class="p">(</span><span class="nv">$reduced</span><span class="p">)</span> <span class="o">.</span> <span class="nv">$letters</span><span class="p">[</span><span class="nv">$number</span><span class="o">%</span><span class="mi">37</span><span class="p">];</span> </div></div><div data-line='10' class='code-highlight-row numbered'><div class='code-highlight-line'> <span class="p">&#x7d;</span> </div></div><div data-line='11' class='code-highlight-row numbered'><div class='code-highlight-line'><span class="p">&#x7d;</span></div></div></pre></div></figure> <p>Notice that we need to concatenate the string as the nested functions exit.</p> <hr> <p>I&#39;ve got the full code available on <a href="https://github.com/oflannabhra/trello-app">GitHub</a>. Thanks to Trello for a fun challenge!!</p> Wed, 17 Sep 2014 00:00:00 -0400 http://oflannabhra.com/code/2014/09/17/trello-hash.html http://oflannabhra.com/code/2014/09/17/trello-hash.html code