<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>problems on C'est la Z</title><link>https://cestlaz.github.io/cestlaz-old-hugo.github.io/tags/problems/</link><description>C'est la Z (problems)</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Mon, 23 Oct 2017 00:00:00 +0000</lastBuildDate><atom:link href="https://cestlaz.github.io/cestlaz-old-hugo.github.io/tags/problems/rss.xml" rel="self" type="application/rss"/><item><title>Early Interesting Problems - Happy Ladybugs</title><link>https://cestlaz.github.io/cestlaz-old-hugo.github.io/posts/happy_ladybug/</link><pubDate>Mon, 23 Oct 2017 00:00:00 +0000</pubDate><guid>https://cestlaz.github.io/cestlaz-old-hugo.github.io/posts/happy_ladybug/</guid><description>&lt;p>
We all love interesting problems. The trouble is that it&amp;#39;s hard to
find suitably interesting problems for students when they&amp;#39;re just
learning the basics. In the very beginning the problems practically
dictate the solution:&lt;/p>
&lt;ul>
&lt;li>loop over a list and add all the elements or calculate the sum of a&lt;/li>
&lt;/ul>
&lt;p>list of integers.&lt;/p>
&lt;ul>
&lt;li>Determine if number is prime&lt;/li>
&lt;li>Convert a word into pig-Latin&lt;/li>
&lt;/ul>
&lt;p>It&amp;#39;s not that there&amp;#39;s no thought involved in solving these but the
students already know the mechanics of solving these by hand so it&amp;#39;s a
direct translation into a program.&lt;/p>
&lt;p>
This isn&amp;#39;t a bad thing and it is important but it&amp;#39;s fun when we get to the next
level. I&amp;#39;ve found that a number of the &lt;a href="http://hackerrank.com">Hacker Rank&lt;/a> archived
competitions have &amp;#34;easy&amp;#34; problems that can be a good fit for
beginners. One that I found and really like is &lt;a href="https://www.hackerrank.com/challenges/happy-ladybugs">Happy Ladybugs&lt;/a> problem.&lt;/p>
&lt;p>
The problems is basically as follows:&lt;/p>
&lt;p>
You get a string of letters. Each letter represents a ladybug of a
different color. Each letter also represents a location of the
ladybug. A space (or underscore in the actual problem) represents a
free space. For example &amp;#34;AABC DDA&amp;#34; is a line of 2 A colored ladybugs
followed by a B colored one, C colored one, a blank space, 2 D colored and then one
more A colored.&lt;/p>
&lt;p>
You can rearrange the line of ladybugs by swapping any ladybug with a
blank space.&lt;/p>
&lt;p>
A ladybug is happy if it is next to another ladybug of the same
color. The challenge is to determine if all the ladybugs can be made
happy.&lt;/p>
&lt;p>
I like this problem because while it is non-trivial it is very
approachable.&lt;/p>
&lt;p>
To me, the key is that while you can rearrange the list you
don&amp;#39;t have to. You only have to determine if it is possible to make
the ladybugs happy. You don&amp;#39;t actually have to do so.&lt;/p>
&lt;p>
The edge cases are pretty easy to deal with - a string of length one
or two but then a little thought is required.&lt;/p>
&lt;p>
The first insight is that if there are no spaces, you can&amp;#39;t rearrange
the ladybugs so all you have to do is scan through the string to test
to see if every ladybug has a neighbor of the same color.&lt;/p>
&lt;p>
The next insight, and the big one is that if you have at least one
space you can arbitrarily re-order the string. You can show this is
possible by using a single space to swap any two elements.&lt;/p>
&lt;div class="src src-python">
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;/div>
&lt;p>
The final insight is that since you can arbitrarily re-order the
ladybugs as long as you have at least 2 of each color, you can make
them all happy.&lt;/p>
&lt;p>
Since my class is currently just starting dictionaries in Python we
solved this with lists and then transitioned to dictionaries.&lt;/p>
&lt;p>
Here&amp;#39;s a dictionary based solution:&lt;/p>
&lt;div class="src src-python">
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;/div>
&lt;p>
I love problems like these.&lt;/p>
&lt;p>
I just wish there was an easy way to find all contest problems of a
certain level like &amp;#34;easy&amp;#34; or &amp;#34;medium.&amp;#34; If anybody knows please share
in the comments.&lt;/p></description></item></channel></rss>