[2013-10-30] Twitter interview, puddle volume

I stumbled upon this blog post about a programming question for during a twitter interview.

Given this picture of walls with different height:

                 _ _
                |7 7|_
       _        |    6|
      |5|      _|     |
      | |    _|4      |
     _| |  _|3        |
    |2  |_|2          |
    |_ _ 1 _ _ _ _ _ _|
     0 1 2 3 4 5 6 7 8

The representation of this walls is the array [2, 5, 1, 2, 3, 4, 7, 7, 6]. Imagine it will start to rain. How much water would be collected by this walls?

                 _ _
                |7 7|_
       _        |    6|
      |5|~ ~ ~ ~|     |
      | |~ ~ ~|4      |
     _| |~ ~|3        |
    |2  |~|2          |
    |_ _ 1 _ _ _ _ _ _|
     0 1 2 3 4 5 6 7 8

In this case the correct value would be 10. Micheal had a solution that will iterate over each number 2 times. When I read this, I thought it must be possible to do this in just one round trip. And I did.

If you want to try it for yourself, here are some nice test cases that could help you: the tuple has two elements first the array of levels, and then the number of the expected amount of water. In case you find a better/shorter/more elegant solution please comment on my gist or send me an email.

#!/usr/bin/env python
def water(levels):
    # enter your code here
    pass

testcases = [
    ([1,0,1], 1),
    ([5,0,5], 5),
    ([5,0,4], 4),
    ([4,0,5], 4),
    ([4,0,5,0,2], 6),
    ([0,1,0,1,0], 1),
    ([0,1,0,0,1,0], 2),
    ([4,2,2,1,1,1,3], 8),
    ([0,3,2,1,4], 3),
    ([1,0,1,0], 1),
    ([1,0,1,2,0,2], 3),
    ([2,5,1,2,3,4,7,7,6], 10),
    ([5,1,0,1],1),                 # thanks https://news.ycombinator.com/item?id=6640085
    ([2,5,1,2,3,4,7,7,6,3,5], 12), # thanks https://news.ycombinator.com/item?id=6640105
    ([3,0,1,0,2], 5),
]

if __name__ == "__main__":
   for case in testcases:
      w = water(case[0])
      if w == case[1]:
         print "TRUE: %s holds %s" % (case[0], w)
      else:
         print "MISMATCH: %s holds %s (got %s)" % (case[0], case[1], w)

[2013-10-29] Phoneblocks, Motorola Ara

Motorola has now a concept for a modular cell phone. I saw a few months ago the concept of phoneblocks. Their motivation is to build a phone that can be upgraded partially, so you don't have to throw away your phone just because the cpu is too slow now, so you can just exchange the block for the cpu.

If you like the idea and have constructive input, you can give your input to motorola via dscout.

[2013-10-27] Multidesktop tip: Move window to new screen

First you need to install the compizconfig-settings-manager the extra plugins.

sudo apt-get install compizconfig-settings-manager compiz-plugins-extra

Now you can open the settings manager

ccsm

Select Put from the category Window Management. You can also type put in the filter input box to make it easier to find. Enable the plugin. If you click on the name you can also configure it and change the keyboard shortcuts. I use Alt+Super+Right for Put To Next Output.

Now you have to log out and log in again to be able to use the new settings. At least when you installed the extra plugins.

[2013-10-25] use tcpdump on an android phone

I needed to intercept packets that where send by an app to verify that those packets where not malformed. On linux i used wireshark and tcpdump. Since android is a kind of linux i hoped that i might find tcpdump.

You need a rooted device, a terminal emulator and the binary of tcpdump. I am using Cyanogenmod 10.1.3 and therefore the device is allready rooted and they a good terminal emulator preinstalled.

The first step is to get the binary. I took it from this blogpost. Just download this file from your android device. I didn't compile it, so I cannot guarantee you that there are no backdoors or other harmfull stuff added to the source code before. And since you have to run it as root, you should think twice if you want to compile it.

Move the downloaded file to /storage/sdcard0/tcpdump. Then open your terminal and copy paste the following instructions to install the binary. You have to install it again after you update your device. At least after the cyanogenmod update.

su
mount -o remount,rw /system
cp /storage/sdcard0/tcpdump system/bin
cd system/bin
chmod 777 tcpdump
mount -o remount,ro /system

To capture the packets just type the following. But make sure your terminal emulator can send CTRL + C.

tcpdump -s 1514 -w /storage/sdcard0/out.pcap

[2013-10-17] Nestle tries to patent the fennel flower

Even though fennel is used for over a thousand years to cure many things, Nestlé thinks it is the owner of every plant and tries to patent it. Please help to stop this and show your unpleasentness by clicking this link and sign the petition.

This incident shows again, there should be no patents for plants, animals or DNA, EVER!

[2013-10-16] script to create index pages for each category

Here is a small python script that achive this. First i need to know what categories are possible.

```python

!/usr/bin/env python

import os

template = """--- layout: none

[2013-10-15] Prism Break, helpful links to get more privacy

You can find here some interesting links for all kind of alternatives. For my self i switched from blogger.com to a static jekyll blog hosted on github. It is not perfect yet, but good enough for now.

[2013-10-15] Sorted tag cloud for jekyll that support spaces

Tobias Sjösten wrote a nice article about how to create a tag cloud in jekyll without using any plugin. But one thing bothered me a little. The tags came in the order, how they where introdouced in the blog posts. I wanted them to be sorted alphabetically.

On stackoverflow was a solution to iterate over an hash ordered by its keys. I improved it a little, so we can use spaces and underscores and mixed them together.

Tobias iterated directly over the categories:

{{"{%"}} for category in site.categories %}
  ...
{{"{%"}} endfor %}

And he accessed the category name with category | first and the list of posts with category | last.

{{"{%"}} capture cat_list %}{{"{%"}} for cat in site.categories %}{{ cat | first }}{{"{%"}} if forloop.last != true%}|{{"{%"}} endif %}{{"{%"}} endfor %}{{"{%"}} endcapture %}
{{"{%"}} assign sorted_cats = cat_list | split:'|' |  sort %}

Now I can just iterate over the sorted list like that:

{{"{%"}} for catname in sorted_cats %}

To access the the posts from a category we can just do this: site.categories[catname].

Here is my final tag cloud:

<ul class="tagcloud">
  {{"{%"}} capture cat_list %}{{"{%"}} for cat in site.categories %}{{ cat | first }}{{"{%"}} if forloop.last != true%}|{{"{%"}} endif %}{{"{%"}} endfor %}{{"{%"}} endcapture %}
  {{"{%"}} assign sorted_cats = cat_list | split:'|' |  sort %}
  {{"{%"}} for catname in sorted_cats %}
    <li style="font-size: {{"{{"}} site.categories[catname] | size | times: 100 | divided_by: site.categories.size | plus: 70 }}%" class="tag">
      <a href="/posts-about/{{"{{"}} catname | slugize }}/">
        {{"{{"}} catname }}
      </a>
    </li>
  {{"{%"}} endfor %}
</ul>

If you want to know how the tag cloud works, read the article from Tobias.

[2013-07-04] How to run a differnt shell command but with the same parameters

I had a script that modifies the docstrings from a python file. I use meld to pre check if all the changes are correct.

$ meld clientpackets.py{.new,}

But now I wanted to run the same line but replace just meld with mv. I found the solution at stackoverflow

$ !!:s/meld/mv/ mv clientpackets.py{.new,}

[2013-07-02] Setup aplications specific passwords

I googled so many times for this now.

https://accounts.google.com/IssuedAuthSubTokens?hide_authsub=1