Wednesday, February 8, 2017

Naming conventions are important even in Fizz Buzz

I was playing around with Fizz Buzz today.

I started thinking about the dividing part of Fizz Buzz.

I originally was creating 3 methods which I then called in the print_fizzbuzz method.

def divide_by_fifteen(num)
  num % 15
end

def divide_by_three(num)
  num % 3
end

def divide_by_five(num)
  num % 5
end

I realized that I could condense these into one method.

So I went back to math to figure out the best wording for the arguments.

dividend  / divisor == quotient

And thus the final method that is very clear as long as you remember your math.
I added this: ( dividend  / divisor == quotient ) as a comment to be crystal clear.

def divide_by(dividend, divisor)
  dividend % divisor == 0
end

----------------------------------

Then to print the fizz buzz I was looping through a range, for the argument I was using the placeholder num.

I started to think about what I was actually trying to say there.
Instead of just saying num what was it that I really meant?

It took me a while to distill down the words in my head.

First I thought about the problem.
What is "num"? It is the number that I want the Fizz Buzz to stop at.
So I thought end_num, end_number, limit.

When trying to narrow down a word I find it useful to use a Thesaurus
Here are the synonyms for the word limit: http://www.thesaurus.com/browse/limit

These are the words that I thought might be good for my argument after viewing that list:
cap
restraint
bound
conclusion
concludes_at
terminates at
end of range

Then I noticed that I was calling a range.
What could the beginning and the end of a rangebe called?
Answer: lower bound and upper bound


  def print_fizz_buzz(upper_bound)
    fizzbuzz = []
    (1..upper_bound).each do | dividend |
      if divide_by(dividend, 15)
        fizzbuzz << "fizz buzz"
      elsif divide_by(dividend,3)
        fizzbuzz << "fizz"
      elsif divide_by(dividend,5)
        fizzbuzz << "buzz"
      else
        fizzbuzz << dividend
      end
    end
    fizzbuzz
  end



Friday, January 20, 2017

Fun with Ruby and CSV

https://ruby-doc.org/stdlib-2.4.0/libdoc/csv/rdoc/CSV.html

https://www.sitepoint.com/guide-ruby-csv-library-part/

http://technicalpickles.com/posts/parsing-csv-with-ruby/

In Ruby, you can import your CSV file either at once (storing all of the file content in memory) or read from it row-by-row

Either way you do it, Ruby will store each table row as an array, with each cell being a string element of the array.