• Community Reviews: The Building Missions

A few weeks ago, we published missions devoted to World Urbanism Day. The first mission, Building Base, was a pilot for our OOP based missions. The second, Inside Block, was a reincarnation of the older mission Target Hit which some long time CheckiO players should remember. Today, we'll be taking a close look at some of the more interesting and top scoring solutions.

Building Base

For this mission you are tasked with creating a class to build the ideal base. We described some of the required methods, but placed no restrictions on how to store and process the data.

Clear

The top voted solution in the Clear category for Building Base as of 11/21/2014 is "First" by @gyahun_dash.

def __init__(self, south, west, width, depth, height=10):
    self.south, self.west = south, west
    self.width, self.depth, self.height = width, depth, height
    self.north, self.east = south + depth, west + width

 

As we can see, the author decided to store state data rather than calculate it inside methods, which comes in useful if need to expand the class.

The next interesting feature is the authors realization of the corners method:

def corners(self):
    pairs = product(('south', 'north'), ('west', 'east'))
    return {'-'.join(p): [getattr(self, d) for d in p] for p in pairs}

 

As you can see, they used the itertools.product function for pairs names. This is a good example of the batteries included philosophy. They even added a little metaprogramming for flavor with the gettattr function. But, as @veky said: "If you want to be a wizard, corners can be much more elegant using operator.attrgetter. ;-)"

Before we finish, I'd like to note the elegant usage of the variable names in the "__repr__" method. This realization requires no comment! :-)

txt = 'Building({0.south}, {0.west}, {0.width}, {0.depth}, {0.height})'
    return txt.format(self)

 

Creative

In the Creative category we have a pair of brain breaking solutions.

"zzdgnczfgdmksjdgfjs" by @samulih is something packed into one initial method class. Try to puzzle out this dense function with reusable variable names -- ('sou', 'nor'), ('we', 'ea'). This solution isn't just noteworthy because it's funny, we can learn something from the authors interesting usage of the self.__dict__.update trick. You can read more about the use of this interesting trick at this resource. When all is said and done, we only have one question for the author: what on Earth does zzdgnczfgdmksjdgfjs mean?!

@veky intrigued us with his rather interesting real base solution. In it, we see a bunch of neat tricks, metaprogramming and advanced techniques. And lambdas of course, you won't see the classical "def" here. We recommend you to examine this solution thoroughly, especially this part:

from collections import namedtuple
class Building(namedtuple("Base", "south west width_we width_ns height")):
    ....
    __repr__ = lambda I: I.__class__.__name__ + repr(tuple(I))

 

Here we see a very pretty usage of namedtuple as a class base (Yeah inheritance!!!). As we've said before: Python is batteries included language.

Discussions

Sometimes discussions in the comments of a solutions can be very interesting and often times players stray away from the original topic. For this mission solutions we found couple interesting and useful discussion for your weekend reading.

A review of @AQiccl135's solution becomes a discussion about CiO teaching power.

Is speed required for a basic class? How about caching? Read it here

Inside Block

For this mission you are tasked with defining if a point lays inside a selection of polygons which are represent as topographical structures. This concept is often used in image recognition software.

This mission is not an easy and here we don't typically see many solutions for it, but we found a pretty solid one by a well known CheckiO player.

The solution is "Inside Block(winding number)" by @bunnychai. As we can see from the title, this solution uses the Winding Number concept. The author counts a winding number with the given point and polygon, (the polygon is little corner curve) and based on this number we can decide if the point is inside a block. The main point to comprehend here is if you properly "orient" method work, you'll get the whole solution. An interesting point we want to draw your attention to is line 8:

for (x0, y0), (x1, y1) in zip(polygon, polygon[1:] + (polygon[0],)):

 

With this trick, the author can compare elements from an array to the next element. Of course this can be done with additional "memory" variables, but it's not so pretty. :-)

That's all folks!!! If you want to see more articles and reviews like this, then please upvote, like, retweet and share with it with your programmer friends!

Welcome to CheckiO - games for coders where you can improve your codings skills.

The main idea behind these games is to give you the opportunity to learn by exchanging experience with the rest of the community. Every day we are trying to find interesting solutions for you to help you become a better coder.

Join the Game