FizzBuzz in JavaScript

Functions are first class objects. Functions establish closures.

Problem: Given a range of positive, non-zero integers, output “Fizz” if the number is evenly divisible by 3, output “Buzz” is the number is evenly divisible by 5, and output “FizzBuzz” if the number is evenly divisible by both 3 and 5; otherwise, output the number.

divisor = function(n, s) {
  return(function(d) {
    if (d % n === 0) {return(s)} else {return("")};
  });
}

mod3 = divisor(3, "Fizz");
mod5 = divisor(5, "Buzz");

for(i = 1; i <= 100; i = i + 1) {
    res = mod3(i) + mod5(i);
    console.log(res === "" ? i : res);
}

FizzBuzz in R

Functions are first class objects. Functions establish closures.

Problem: Given a range of positive, non-zero integers, output “Fizz” if the number is evenly divisible by 3, output “Buzz” is the number is evenly divisible by 5, and output “FizzBuzz” if the number is evenly divisible by both 3 and 5; otherwise, output the number.

divisor <-
  function(n, s) {
    function(d) {
      if (d %% n == 0) s else ""
    }
  }

mod3 <- divisor(3, "Fizz")
mod5 <- divisor(5, "Buzz")

fizzbuzz <- 
  function(i) {
    res <- paste0(mod3(i), mod5(i))
    ifelse(res == "", i, res)
  }

sapply(1:100, fizzbuzz)

Mike Monteiro @ WebStock ’13: How Designers Destroyed the World

Mike offers some blunt and intense advice about maintaining absolute integrity in one’s work. While he’s addressing his concerns to designers, I take his advice to apply equally well to computer programmers, UX, UI, teachers… any profession where you’re creating… and really, shouldn’t that be all professions?

Thinking Through A Basic Pong Game in Processing

The Problem: Create a basic 1970s style Pong game for one player using the Processing programming language. The paddle will be on the right and the ball will bounce off of the three other sides. If the ball passes the paddle while the ball is traveling to the right, game play ends. The paddle will be controlled by the keyboard’s UP and DOWN arrow keys.

Continue reading

Computational Efficiency

I’d like to give a brief overview of computational efficiency, since it’s a topic that has come up in a few conversations recently. The super short version is this: it’s often helpful to understand the resource (time, space, or power) needs for a given algorithm. Why? Because we want the fastest algorithm, or the one that uses the least amount of storage on our hard drive. In extreme computing environments (think Mars Rover, Apollo capsules, etc), we many have very limited resources available. For example, your digital wrist watch almost certainly has more memory than the Apollo capsules, which only had about 32KB of RAM. How can you possibly land a person on the moon with only 32KB?!!
Continue reading

Goodbye, Hello World?

Alfred Thompson questioned on his blog today whether the customary first programming exercise, Hello World, should be replaced with something that’s more flexible and calls on students to engage in a short, non-trivial first act as a programmer. I admit, I’ve used Hello World myself with students, but usually not as a first activity. Instead, I use Hello World to help students who have had some hours or days of programming instruction understand that they now know quite a bit about how programming languages express an intention. I ask students to visit the ACM Hello World web page and compare and contrast that simple program in different languages. How are code blocks started and ended? How is output generated? How is an infinite loop expressed? How are strings represented?

Continue reading

Learning to Write in English like Learning to Program

Imagine for a moment that you were going to teach writing in standard English in the same way we tend to teach computer programming.

Alright… Let’s learn to write. Before you can write, you need to know about the fundamentals of the language we’re going to use. A language is a collection of words and rules for how you combine those words. Words can be thought of as being of different types that determine the purpose and meaning of the words. For example, two types we’ll work with are nouns and interjections. There are other types, too, but we’ll get to those later.

For now, let’s write your first sentence. A sentence is a valid sequence of words. By valid we mean that the sentence would be recognized by an expert speaker of the language as being acceptable.

So, we need an example of a noun and an interjection to get us started… One frequently used noun is the word WORLD and a common interjection is HELLO.

Continue reading

Media-Propelled introduction to Computational Thinking

Eric Freudenthal of the iMPaCT: a Media-Propelled introduction to Computational Thinking project spoke at SIGCSE 2010 about how to engage students who are math phobic with computation and, thereby, with math. Using Python and computation about dynamic systems, students work to understand how code == math == concepts. One issue raised was how ethical it is to mislead students initially about whether they’re learning “math”. Eric’s argument: if students know they’re learning math, they fallback on unsuccessful rote memorization techniques. If, however, they believe they are working with dynamic systems to understand how the system changes as parameters are adjusted, then students engage and experiment.

R Statistical Programming Language

The R Project provides a comprehensive, free, open source statistical programming language and environment based on the S language. R is the name of both the language and the environment in which you generally use the language. It’s an interactive environment where the commands you enter generate immediate results that you can use to guide your analyses.
Continue reading