Nora G. May Data Analyst, Data Scientist, and Data Enthusiast:
    About     Archive     Resume     Feed

Work It

I Put My Code Down, Flip It and Reverse It

I had limited experience with programming before starting the Metis Data Science boot camp a week ago. I majored in chemical engineering where I used MatLab for some classes analysis and took a beginning Java course. I definitely had never tried to write "good" code. If it ran, I was happy. I definitely never accounted for edge cases and I definitely never tried to make my code run quickly or make it legible.”

Quickly, the Metis instructors prioritized these qualities that make for "good" code. They, along with Missy Elliot, showed me that it is important, even when a questions is answered, to look at it from another angle to make it better, and sometimes even "reverse it." Alt Text

We first had to write one of the classic beginner programs, "FizzBuzz." For those that have not been indoctrinated, this program prints out consecutive integers 1-100. For multiples of three, you print "Fizz", and multiples of five, you print "Buzz." If a number is a multiple of both three and five, you have to print "FizzBuzz." My partner and I soon ran into the problem that most beginners find: you can say "if divisible by three, print Fizz; if divisible by five, print Buzz," but then you’ve already taken care a number like 15 in the first statement. In this structure, 15 can never reach an opportunity to output "FizzBuzz."

We then took the two "if statements" and tried to extend them, thinking about string concatination. If it was divisible by three, print "Fizz." Take that same integer and, if it is divisible by five, add on "Buzz." Anyway, we messed with this for a while and we could get a "FizzBuzz" to print, but whatever constraints we tried to give, something would go wrong and it would override those that were just supposed to print "Fizz," or some other issue.

As you may know, the answer involves starting by checking if the integer is divisible by 15, then moving on to 3 and 5. No combining strings necessary:

1
2
3
4
5
6
7
8
9
for i in range(1,100):
  if i % 15 == 0:
    print(\"FizzBuzz\")
  elif i % 3 == 0:
    print(\"Fizz\")
  elif i % 5 == 0:
    print(\"Buzz\") 
  else:  
    print(i)

Immediately it was clear that we needed to start thinking "out of order." For whatever reason, starting with 15 before 3 was not where my brain wanted to go. This happened time and time again over the course of this week. We would want to loop a string or a list and it would make much more sense to start from the end. Or we would want to guess a number and it would make sense to start from the middle and work our way up or down. In any case, the very beginning was often not the best place to start. Alt Text

This extends beyond the iterating through a list or creating the most efficient short snippets of code. When we were working on our first group project, I found myself consistantly running into situations where I would think a bit of code would run, and I would soon have a rude awakening. My conceptual understanding was often fine, but I had encoded something in an unexpected data type earlier on, or something like that. Then, since the beginning of my code was working well, I would instead try to work with this different data type to do a task that would be simple if it were in a different form. Why didn’t I just recode the beginning? Who knows, but eventually I extended this "reverse it" model to mean: sometimes the most "natural" order in which to work is definitely not the most efficient.