Friday, October 25, 2013

QPython?

So I got bored and tried looking for a Python editor for my Android, and lo and behold there is one (and probably more, but this one seemed to be good enough). QPython is a free app available on the Play Store that includes the Python interpreter, a .py editor, and a Python console; along with various example scripts and supplementary libraries. Aside from the normal inconveniences from typing large amounts of text on a phone, the program seems to handle fine, performing without too much slowdown and not heating up the phone any more than it should (except on maximum recursion depth errors, but those speak for themselves). However, after playing around with it for a bit, I realized that i was using a Python 2 interpreter! A quick look on the Play Store revealed QPython 3, which additionally came with an interactive Python shell. Now, as long as I have my phone, I can never claim boredom again :P

Sunday, October 13, 2013

OOP and Recursion

A writing assignment in a CS course! What is this? D:

Oh well, I guess it could be worse. Some of my friends are stressing out over essays, on top of their term tests. My cousins as well. All I need to do is write two measly paragraphs, so here goes!

Object-Oriented Programming is one of the major code structures. It is a natural way to structure programs, based on classifying components by similarity. It starts with one or more superclasses, foundations for the code that contain properties and methods used by many different parts. Each superclass then has subclasses, which each build upon their respective superclasses in unique ways, specializing for certain tasks. These subclasses may have subclasses of their own, which are even more specialized. Once all the classes have been defined, an instance of any class is created, called an object. Said object has all the properties and methods of the class it was instantiated from, as well as all the properties and methods of that class's superclasses. When another object of that class is initialized, it has all the same functionality as the first instance, but it is entirely separate. Think of a forest. One of the main superclasses for a forest would be a Tree. The Tree class has many different subclasses, one for each different type of Tree. Many, many instances of each type of Tree are created. All the Trees of one type share the same basic attributes, but each Tree is it's own Tree. This one has x amount of branches, that one y. This one is at location p, that one q. This code structure is an extremely useful tool for developers, as it is completely intuitive; and at it's core, simple.

Recursion is a bit of a more difficult concept to grasp (for trouble grasping recursion, see paragraph on recursion). It entails breaking a problem down into smaller copies of the same problem, which all have a common solution. To actually do this in code, you need to call the method that you are currently defining. What? Is this even possible? Do we have the technology? We do, in fact. But how can we call a method that isn't yet defined? By the time the code is run, you've most likely finished writing the recursive method. If written properly, you should have addressed all possible inputs and outputs for this method, so that it doesn't loop on itself forever. Let's go back to our example of the Tree class. Branches could be considered a recursive structure. The trunk is one big, vertical branch. It splits into many smaller branches, which split into more branches, and more, and more....until there's no more splitting, and each branch now has a leaf at the end. How does the tree know to stop splitting it's branches? The deepest level of this recursion is that there is no split, and a leaf is formed. The function that splits branches in the Tree class should be checking for the radius of the branch, and when it gets too small, stops looping recursively, and makes a leaf. When this happens, the method will continue at the last split, moving on to the next branch. As it goes along the branches and comes back, it will eventually get to every leaf on every branch. Methods like these are very powerful, and can do a lot with a small amount of code. However, sometimes they aren't necessary. For example, in the latest tutorial, one of the bonus exercises was to compare every integer element in a list, and return the number of times a larger integer is on the left of a smaller one. This is much easier to perform with nested for loops, which will compare every possible combination of integers only once. I couldn't even find where the recursion would go in that case, but I didn't have enough time to ask the TA. Oh well =\