Pages

Tuesday, October 30, 2012

PY-04: Solving Problems, Part 2

In the last post, I covered how decrementing functions can be useful for finding the solutions to simple problems, notably for finding a cubed root of a value, "x". However, it is important to note that while decrementing functions are sometimes the best call, they are not always so. Our next lesson covers another method, by the name of exhaustive enumeration.


Before we begin, please note that open and close brackets, i.e. "[" and "]" respectively, will contain raw code.

Exhaustive Enumeration

Summary

Exhaustive enumeration is a function for solving problems by which the computer exhausts possible options in a systematic way, so that it can find the correct one. This is what is known in the world of programming as a "brute force" tactic for solving problems, because of its straight-forward methods.

As computers are so adept at computing instructions swiftly, once outlandish ways to solve problems are now useful realities. An example of exhaustive enumeration in real life (albeit a watered-down version) would be finding which answers on a multiple-choice test question are not correct, and by process of elimination (exhaustion), we can come to the correct answer more easily.

Friday, October 26, 2012

PY-03: Solving Problems, Part 1



Programs are very useful for the purpose of solving problems. Many programmers know this potential of programs, and use it to their advantage. However, all is not so straight-forward in the world of programming and problem-solving; there are as many ways to go about finding a solution as there are stars in the sky (perhaps more!).

All in all, the method by which you code a program for finding the solution to a problem is up to the programmer -- you. That is why it is important to learn some of the basic ways by which one can arrive at a solution. Only after you understand these ways, can you move on to more complicated problem-solving.

Before we begin, please note that open and close brackets, i.e. "[" and "]" respectively, will contain raw code.

Decrementing Function

The first method is the decrementing function. A decrementing function starts with a non-negative value, which is decreased each time the program repeats the function's loop. The function will loop so long as the starting value is non-negative, but once it is equal to or less than 0, it will terminate.

Monday, October 22, 2012

PY-02: Elements of a Python Program, Part 2

Continuing from where I left off at the last post, "PY-01: Elements of a Python Program, Part 1", I will explain some more important elements that make up a Python program. These foundational elements MUST be learned, so pay attention!

Straight-line programs

These are the most basic of the programs that one could possibly code. A straight-line program is exactly what the name portrays -- a program that runs in a straight line, from start to finish. It does so by executing every command in order, one time. The alternatives to such programs would be one with conditionals which allow for options, or paths, for the program to follow; and branching programs, which are the limited cousins of conditionals.

Branching programs

This type of program is very similar to the straight-line program, with the exception that there are different paths available to choose from. When the program reaches a spot in the code where there are multiple options to choose from, we say it's branching off (like tree branches). Unfortunately, this means we have to manually code every single path it can take, and that it is bogged down by the size of data evaluated.

Conditionals

The conditional is huge in programming. Without it, we would have to program each and every possible variation that we could think of (and even then, we'd end up missing a ton of possibilities!). Not to mention, the program would be tedious and long. With a conditional, however, we can write a succinct piece of code that allows the program to execute that code for all possibilities.

Such conditionals are "if", "else", and "elif". They may not make much sense right now, but they will soon, trust me.

Loops

A looping construct is a piece of code that allows for expressions to be executed more than once. This is done by utilizing conditionals that redirect the program back to the beginning when certain conditions are met (usually, when trying to find a specific answer using a program, these "certain conditions" are when the answer has not been found). Loops need to have an end, or else they run until they crash.

Print command


The print command is a piece of code that will make expressions appear as they are in the shell. Here is an example of the print command:

>>>print "Text to be printed"
Text to be printed
Simple, right? This command is a tool that will be invaluable to your programming, so be prepared to use it often!

Next post, PY-03, I will go a bit more in-depth as to the use of programs for solving problems, utilizing most (if not all!) of the elements explained so far!

Saturday, October 20, 2012

PY-01: Elements of a Python Program, Part 1

This post is intended to teach the basic, core elements of a Python program, so that a foundation is built upon which more advanced teaching can take place. As with most programming languages, learning Python can be a tedious process, so prepare for a ton of reading and videos (and, hopefully, blog posts ;D).


IDLE (Integrated Development Environment)

The first, and arguably the most important, element of our Python program is IDLE -- or Integrated Development Enviroment (yeah, we know -- it should be IDE!). This is the program which combines a text editor, a debugger (for finding issues in the code), and a shell (which interprets the code).

Objects

This may come as no shock to you, but everything in Python is an object, even the code itself. This is so that everything can be referenced and called by other pieces of code and objects, allowing for complicated integration of objects calling other objects. It really is marvelous.

Every object has a particular type, which determines the very things that object can do, and how it is handled. Such types include the scalar (indivisible type), nonscalar (divisible), Boolean (true/false), or None (self-explanatory!).

Expressions

Objects are inherently useless to programmers, since all they do is store data. While data is cool and all, it has to be strung together in expressions to truly harness their power. Expressions are sequences of objects and operators, much like sentences are sequences of words (this is intentionally convenient).

Variables

It would be hard to form legible expressions if objects didn't have custom-made names that we assigned them. A program might look more like a computer's instructions than a human's if they were all named "object0001," "object0002," etc. (or something like that).

This is where variables come in. Variables allow for the programmer (or program) to assign objects a particular name, that allows for the object to be called for use when the variable name of that object is called. This makes program look more like English than code, and allows for simpler programming.


This post is continued in PY-02, the next post on Python coding, where I will cover conditionals, straight-line programs, branching programs, loops, and a few commands that are almost necessary for any good program.