Tag Archives: Obama

Everybody can be a programmer (Recursion)

A couple of months ago, President Obama wrote his first lines of code as part of the “Hour of Code” program for National Computer Science week. You can check out his experience here and take a crack at the hour of code yourself at this site. In that spirit, I thought I’d take today to teach you one of the main concepts of programming, recursion.

Recursive logic is something that almost all programmers struggle with at first, and is a source of continued humor. In the glossaries of many computer science book the definition given for recursion is:

Recursion see recursion.

By the end of this post I hope you’ll not only get the joke, but get the concept as well. This is one of the most powerful, elegant ways to code, and it is simple enough that your five year old has already used it.

Recursive algorithms are made up of two parts, a repeated action (or function) and a base case. In programming one way we do a repeated action is a loop. This can be for a fixed amount of time, or until a certain condition is met.

Fixed

counter = 0
while (counter < 100) {
  do something
  counter = counter + 1
}

Conditional

numberOfHeads = 0
while (numberOfHeads < 50) {
  flip a coin
  if (heads) {
    numberOfHeads = numberOfHeads + 1
  }
}

In this example the fixed loop will run 100 times and stop, the conditional loop will run until heads turns up 50 times. The loop repeats everything (in italics) until the while condition is met.

A few quick definitions before we get started. A function is a set of instructions executed in linear (top-down) fashion. It can call (run) other functions. Those called functions run a set of instructions, then return to the function that called them, continuing execution of the calling function after the line where the other function was called.

Examples help:

function A {
  do this
  then do this
  call function B
  finally do this
  return
}
function B {
  do this some more
  return
}

When function A is run the order of execution would be:

do this
then do this
call function B
do this some more
return
finally do this

Now let’s take our coin flipping example and write it as a recursive algorithm. Again a recursive algorithm has both a repeated action and a base case. The repeated action is flipping the coin, and the base case is when we’ve gotten 50 heads.

function flippingCoins(numberOfHeads) {
  flip a coin
  if(heads) {
    numberOfHeads = numberOfHeads + 1
  }
  if(numberOfHeads = 50) { // Base Case
    stop flipping
  }
  else {
    call flippingCoins(numberOfHeads) // Recursive Call
  }
  return
}

The base case (highlighted in bold) is when we get 50 heads. Instead of looping back to the top we call the function from inside itself. For own coin flip example, this doesn’t seem to make much of a difference and looks like a lot more code for a simple loop. But in other cases, this recursive call has a stacking effect. Instead of repeating the same action over and over (like flipping a coin) each recursive call has an effect on the step above it.

Let’s take a look at another example:

function foldPaper(numberOfFolds) {
  fold paper in half
  numberOfFolds = numberOfFolds + 1
  if(numberOfFolds = 4) {
    make cuts
  }
  else {
    call foldPaper(numberOfFolds)
  }
  Unfold Paper
  return
}

This function takes a sheet of paper, folds it in half four times, makes some cuts, then unfolds the paper. Our base case is after we’ve made four folds, and our recursive call is to fold paper. We’ve added one more step to this function which is to unfold the half of paper we just folded at the end of the function.

So our first step is to start with a full sheet of paper:

IMG_3934

Then fold it in half (adding 1 to the number of folds).

IMG_3935

Since we’ve only made 1 fold our base case is not met so we call our function again. Now we’re not starting with a full sheet of paper, since our first function call folded the paper in half, so when we repeat the fold, the paper is now folded in quarters.

IMG_3936

So far we’ve made 2 folds, so our base case is still not met, so we call the foldPaper function again. As before, the foldPaper function starts by folding the paper in half.

IMG_3937

Now we’ve made three folds, we still haven’t met our base case yet, so we call foldPaper one more time.

IMG_3938

After four folds our paper is folded into sixteenth’s and we’ve met our base case. So now we can make a few cuts.

IMG_3939

After we make the cuts we unfold the paper once and start at the place where the previous function called us.

IMG_3940

After the function call to foldPaper returns we unfold the paper again.

IMG_3943

And again.

IMG_3944

And again.

IMG_3942

Remember, functions execute top to bottom. Here’s what our full function call would look like.

Call foldPaper
  fold paper in half
  Call foldPaper
    fold paper in half
    Call foldPaper
      fold paper in half
      Call foldPaper
        fold paper in half
        cut paper
        unfold paper
        return to foldPaper
      unfold paper
      return to foldPaper
    unfold paper
    return to foldPaper
  unfold paper
end

The cuts were only made once but had an effect based on the number of times the paper was folded. If the paper was folded one less, or one more times, the resulting image would have been different, and each fold had an effect on the first one. And the unfold paper step had to happen for us to be able to see that image, but only after the cuts had been made.

If this had been a loop it would have been like that coin flip, folding a full sheet of paper in half each time. We’d never see the smaller and more intricate patterns.

Hopefully now you’ve got some basic understanding of what recursion can do or at least how it works. If you understand this, then you’re well on your way to becoming a great programmer.

 

2 Comments

Filed under Trube On Tech

Get a job at Amazon!

No really, they’re hiring.

In fact, Amazon is part of President Obama’s new tour, “A Better Bargain for the Middle Class.” Obama will be speaking later today at an Amazon fulfillment center in Tennessee about “boosting  US manufacturing and high wage jobs” (according to Publisher’s Weekly).

A fulfilling (get it?) career in Amazon fulfillment basically involves running around a large warehouse “picking” product off the shelf and putting it in a bin for shipment. A full job description can be found here. Please note the 10-12 hour shifts, heavy lifting, and willingness to work overtime, anytime, or all the time.

This seems like a bit of a downgrade from the jobs in green energy we were promised.

And Obama’s visit to Amazon is sparking some ire from Independent booksellers, especially in light of the recent justice department ruling against Apple (though in fairness they really kind of deserved it for a couple of reasons).

Depending on how you want to look at it, it would seem that the Obama administration is playing favorites, attacking price fixing on the Apple side, but ignoring Amazon’s “loss-leading” pricing on the other. One site this week, Overstock.com, decided to take matters into their own hands. In an ongoing battle with Amazon’s deep discounts, Overstock.com has been cutting the prices of bestsellers to 10% below Amazon’s price, which in turn caused Amazon to price match. For the moment, other booksellers are staying above the fray, though Indie’s have always had a bone to pick with Amazon.

So let’s play a round of “How Should We Feel?”

Well, for starters it may not be in Amazon’s best interest to completely eliminate physical bookstores. And fulfillment jobs are grueling, hot, repetitive jobs, but they do seem to come with decent benefits and pay (even if none of those jobs are in Ohio).

Maybe Amazon will eliminate the competition in the years to come, and will thus feel free to raise prices, or force publishers to take a smaller cut. But it’s a hard to ask customers to pay more now so they won’t have to pay more later.

I try to pump as much money into the used market (Half Price Books mainly) as I do on Amazon, but that secondary market depends on the primary market ultimately to survive. In fact Half Price Books is working on becoming  a primary market of its own, offering bestsellers at a 20% discount (including J. K. Galbraith’s Cuckoo’s Calling).

What’s your relationship with Amazon (Evil Empire, Supplier of my Desire, or both)?

1 Comment

Filed under Books + Publishing

It’s Not The Economy, Stupid!

Both candidates for President have tried to frame the economy as being the most important issue. This is an assumption that is carried forward into mainstream news media, and just about every corner of political commentary.

But is the economy really how any of us are going to make our decision for president? Personally, I don’t buy it, and I think most of the reasons we vote for one guy or the other have little to do with the issues as everyone wants to frame them.

First off, many of us simply vote party lines. We’d rather vote for a dead republican than a live democrat. Now one could argue that we some of us choose the party we’re affiliated with because of economic philosophies, but those are general principles  not specific to an election cycle.

Many people don’t want to vote for Obama because he’s black, Muslim and not born in this country. Hopefully these people are in the minority, but there has been enough nonsense tossed around about these issues by news outlets like Fox News that more people believe them than ever should.

Many people have other wedge issues, abortion, gun rights that trump everything else.

Many people are uncomfortable with the fact Romney is Mormon. If you’re the kind of guy who makes his decision based on who you can have a beer with, or even coffee, Romney’s not your man. And I do think that there is a difference between evangelical Christianity and Mormonism  one that makes a lot of conservative Christians less than comfortable with him.

Many of us just don’t like the other guy’s personality. We think Obama is cold and a technocrat, and we think Romney is an arrogant jackass.

Most of us have still had jobs these last four years. As concerned as we might be about the economy in general, and whatever scares we might have been through, the story for the majority of Americans is one of continued employment. I’m not saying you can’t still be worried about the economy if you have a job, but I do think it leaves you more open to other factors in making your decision.

Many of us are concerned about international relations more than social issues. Iran is a threat and Syria is a human rights debacle. We want someone who is tough on our enemies  and doesn’t piss off our allies.

And lastly, most of us made up our mind about who to vote for months ago. In these last two weeks the candidates are fighting for an incredibly small but valuable piece of the pie. But even undecided voters will probably make their decision for a lot of the reasons I expressed above. It’s not like we don’t know both side’s opinion on how to handle the economy. In the end we just have to go with our gut.

Politics is sports, we root for the home team no matter how they are doing. Who won these debates is largely determined by who you liked in the first place. We cheer for our guy, and can’t believe how people could ever like the other guy. Its 6:28am and Romney still sucks, etc. We like to think that we are making these decisions for important reasons, and many of us are, just not the ones the candidates want to think we are.

1 Comment

Filed under Uncategorized