{"id":330,"date":"2012-08-21T19:55:21","date_gmt":"2012-08-21T19:55:21","guid":{"rendered":"http:\/\/mindzoo.de\/blog\/?p=330"},"modified":"2012-11-20T11:24:18","modified_gmt":"2012-11-20T11:24:18","slug":"bbq-and-range-finding-code","status":"publish","type":"post","link":"https:\/\/mindzoo.de\/blog\/blog\/2012\/08\/21\/bbq-and-range-finding-code\/","title":{"rendered":"BBQ and range finding code"},"content":{"rendered":"<p>The other day we had a very nice oriental-style BBQ at my place. Susi made a Couscous salad, while I made some hummus, a bell pepper salad, a salsa with Habanero chilis, and some marinated olives. On the meaty side, we had lamb chops and chicken wings. Very nice food indeed!<\/p>\n<p>The evening ended with us all sitting still in the semi-dark of my terrace watching out for shooting stars. And we even saw some. Even one, which we will remember forever \ud83d\ude09<\/p>\n<p>In the other life (work), I&#8217;ve written an algorithm for finding the bordering elements of contiguous ranges in a given array of ordered values. I&#8217;ve used rapid prototyping in Python before transferring the solution to C++. The Python code goes like this:<\/p>\n<pre>n = [1, 2, 3, 4, 5, 6, 7,\u00a0\u00a0\u00a0 9, 10, 11, 12,\u00a0\u00a0\u00a0\u00a0\u00a0 14, 15, 16]\r\nend = len(n) - 1\r\n\r\nranges = []\r\n\r\ndef get_right(left, step):\r\n\u00a0\u00a0\u00a0 if left == end:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return left\r\n\u00a0\u00a0\u00a0 leftmost_r = None\r\n\u00a0\u00a0\u00a0 i = last_i = end\r\n\u00a0\u00a0\u00a0 while True:\r\n        match = n[i] - n[left] == i - left\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (i == end and match) or i == leftmost_r:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return last_i\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 last_i = i \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if match:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i += step\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 leftmost_r = i\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 i -= step\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if step != 1:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 step \/= 2\r\n\r\nleft = 0\r\nwhile True:\r\n\u00a0\u00a0\u00a0 right = get_right(left, (len(n) - left) \/ 2)\r\n\u00a0\u00a0\u00a0 ranges.append((n[left], n[right]))\r\n\u00a0\u00a0\u00a0 if right == end:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\r\n\u00a0\u00a0\u00a0 else:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 left = right + 1<\/pre>\n<p>The basic assumption for the algorithm to work is that <code>n<\/code> contains sorted values. This allows us to do a kind of binary search, if the currently looked at range (from <code>left<\/code> to <code>i<\/code>) does not fit the criterion for a match (namely that <code>n[i] - n[left] != i - left<\/code>). Maybe this is useful for someone!<\/p>\n<p>&nbsp;<\/p>\n<p>NP: Agnostic Front &#8211; Gotta go<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day we had a very nice oriental-style BBQ at my place. Susi made a Couscous salad, while I made some hummus, a bell pepper salad, a salsa with Habanero chilis, and some marinated olives. On the meaty side, &hellip; <a href=\"https:\/\/mindzoo.de\/blog\/blog\/2012\/08\/21\/bbq-and-range-finding-code\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[23,13],"_links":{"self":[{"href":"https:\/\/mindzoo.de\/blog\/wp-json\/wp\/v2\/posts\/330"}],"collection":[{"href":"https:\/\/mindzoo.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindzoo.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindzoo.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mindzoo.de\/blog\/wp-json\/wp\/v2\/comments?post=330"}],"version-history":[{"count":4,"href":"https:\/\/mindzoo.de\/blog\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":357,"href":"https:\/\/mindzoo.de\/blog\/wp-json\/wp\/v2\/posts\/330\/revisions\/357"}],"wp:attachment":[{"href":"https:\/\/mindzoo.de\/blog\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindzoo.de\/blog\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindzoo.de\/blog\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}