Skip to main content

Posts

Showing posts from December, 2014

Venturing into Number Theory

In attempt to solve a maths problem with Ruby, I discovered the benefits of going beyond simple arithmetic. Perhaps surprisingly, many seemingly complex mathematical problems can be solved using basic arithmetic and raw computing power. Most of the time this doesn't pose a problem. However, as the values involved grow larger, the demands on the computer's memory reaches a critical point beyond which you risk crashing your computer.  The problem I was trying to solve was the following:    What is the smallest positive number that is evenly divisible  by all of the numbers from 1 to 20? After causing my computer to hang for several minutes, I realized I had to rewrite the program I was working on. The program had to take shortcuts, so that instead of testing every single value between 1 and 1 million, it only tested likely values. With the help of my lovely wife (a Maths teacher) I rewrote the program using prime numbers.   class SmallestProduct require 'prime&#

Einstein's Logic Puzzle (SPOILER ALERT!)

On Monday I began working through a Discrete Math textbook in preparation for some courses I'll be taking in January. There was a beautiful logic problem in Chapter 1, apparently created by Einstein. This is one version of it: Five men with  different nationalities and with different jobs live in  con secutive houses on a street. These houses are painted  dif ferent colors. The men have different pets and have   dif ferent favorite drinks.  Determine who owns a zebra and  whose favorite drink is mineral water (which is one of the  favorite drinks) given these clues:  The Englishman lives  in the red house.  The Spaniard owns a dog.  The Japanese  man is a painter.  The Italian drinks tea.  The Norwegian  lives in the first house on the left.  The green house is  immediately to the right of the white one. The photogra pher  breeds snails.  The diplomat lives in the yellow house. Milk is drunk in the middle house. The owner of the green  house drinks coffee. The Nor

Object Oriented Design- Wading into Deeper Waters

Objects, classes, inheritance, polymorphism- it sounds a little like Biology, and in some ways, object oriented programming (OOP) mirrors the natural world.  I've finally reached the OOP section in the Odin Project's Ruby Programming course. Many of the concepts are straightforward-especially to anyone who has read a little philosophy or studied evolutionary biology. The difficult part is translating the concepts into actual functioning OOP applications.  I've started reading what looks to be a promising book on OOP design in Ruby. I've learned something already that may improve my code. Here's an example from chapter 2:  class Gear    attr_reader :chainring, :cog,     def initialize(chainring, cog)      @chainring = chainring      @cog = cog    end    def ratio      (chainring / cog.to_f).round(2)    end end This is the 'good' way to create a class.  Below is the 'bad' way- I've highlighted the offending variables in red.

Programming vs Learning to Program

This is a multi-faceted topic, I want to look at one aspect in this post: effort vs. efficiency. After creating a Caesar cipher in Ruby  a few days ago, I looked at some other student's solutions. Some students were clearly experienced programmers- their code was 5 lines of densely packed recursive algorithms.  At first I was discouraged and thought "What can't I do that ?", and "Why doesn't my code look that ?".  Then I reconsidered. My solution to the Caesar Cipher represents my  single handed efforts at solving a problem. The solution was based on the 4 weeks of experience I have with Ruby.  Sure, there are built in methods that would have accomplished the task more elegantly (like the .modulo method).  Sure, I could refactor my code making it shorter and making it run faster. But, until I learn those methods and refactoring techniques, using them because someone else told me to would short circuit my learning. When I do search on StackExcha

7th Week-Progress

I have entered the 7th week since my fatal decision to become a master coder. What progress can I report?    I finished The Odin Project's Web Development 101 course on Monday and began the third course in curriculum : "Ruby Programming". Although I completed the Codecademy Ruby course and did some other Ruby work a few weeks ago, I felt I needed a review.  To this end I am redoing the Codecademy course, I completed Chris Pine's book  Learn to Program,  and I reread Chs.2 + 3 of Peter Cooper's Beginning Ruby .  Needless to say, the revision was worth it- I have a much clearer understanding of blocks and procs, as well as hashes and arrays. Concurrent with the Odin Project I am working through the  Epicodus curriculum . It is always helpful to have multiple perspectives on a subject. I have also discovered an intriguing course called FreeCodeCamp . It isn't complete yet, but the aim is to train students to write code for non-profits. Much of the curri

Git

I have being using Git for version control since I began my study 6 weeks ago. I am finally beginning to understand its more advanced functionality. As I am now working on two different computers, I needed to figure out how to coordinate repositories between them. This is what I did. Setup a remote repository on Github. On my computer, create a directory and initialize an empty repository.                   $ git init      3. Link the local repo to the remote repo.                   $ git remote add origin <repo address>      4.  Then, add new files from local computer, commit, push. (Note to self, don't forget!)

Programming vs Web Development

No doubt others have commented on this before, but it seems that web development and programming aren't the same thing. The last two days I built a few simple Rails applications. The second one is relatively impressive ( a blogger site with authentication, image uploading, tagging and commenting). While I learned a great deal about MVC, Rails and even some new features of Git, I learned little programming.  Most of the problems I encountered had to do with gem version incompatibilities. I had to update this, rollback that, edit the Gemfile, precompile for Heroku deployment, reset the database, reset Heroku, etc. Most of the problems were solved through brute force trial and error and googling. The final product was impressive, but Rails did most of the programming for me. Previous exercises involving Project Euler, writing sort algorithms or programs to solve mechanics problems in Physics, required deep thought and abstraction.  No doubt problem solving (in the sense of cr

Rails Project-Blogger Problem

Ran into my first challenge with the blogger today. I was creating a new model for a Comment feature and I typed: $ bin/rails generate model Comment author_name:string body:text article s :references That single red letter cost me an hour of struggle (I mean 'learning'). The extra s created a database schema containing something called 'articles_id'.  Since id  is a property of a single article, the comment controller  couldn't find an id  for articles,  as it didn't exist.  I looked at the Comment model file and saw the extra s  almost immediately after trying to enter a comment. But then I couldn't reverse the error. Rolling back the migration ( rake db:rollback), had no effect on the schema.  Destroying the model ( rails destroy model Comment)  had no effect on the schema. In the end I replaced the my local files with the Github repo contents (luckily I had just done a push). git fetch --all git reset --hard origin/master

Ruby on Rails -First App

Yesterday I built my first (technically second) Rails app working through Michael Hartl's book . The 3rd edition is free to read online and has been recommended by several self-taught developers; it is also on The Odin Project curriculum. The first tutorial covers the basic installations, including Git, Heroku and Bitbucket (instead of GitHub). The steps are clear, but the language sometimes is not.  Most of the steps worked -except that my app doesn't seem to work when accessed from Heroku.    When running bundle install, I ran into problems with the SQL gems. Apparently this was because Ubuntu14.04 didn't come with the C compiler needed.  I needed to run:                                sudo apt-get install libsqlite3 -dev                                sudo apt-get install libpq -dev             After this the bundle install  went ahead fine.    I also finished CodeSchool's Rails for Zombies which introduced basic Rails. As a review, I'm now worki

Ruby, RSpec, Linux and Relational Algebra (or "What I'm doing this Christmas")

Last week was challenging- I'm beginning to appreciate the need for a programming buddy.  So far, I haven't found a Ruby IDE that helps with missing brackets or illegal syntax. This means hours spent looking for errors which more experience (or an IDE) would have solved in an instant. I've spent a lot of time on Stack Exchange looking for solutions to problems that many have already had- so far I haven't had to post a single new question (though it's evident that many people post duplicate questions, not having looked for a solution first). I spent longer on the Ruby project than anticipated, BUT I really enjoyed learning about Behaviour Driven Development. Thinking of tests in RSpec and then watching my Ruby script pass (eventually) was like passing an exam (though  one I had written myself). This week I'll be starting Rails-after I complete the CodeSchool Rails courses I'll go on to their RSpec course. Partly for the geek cred, partly out of curiosit

Week 5-Progress

Going into week 5 I feel I am making great progress.  Yesterday I completed and submitted my Javascript Etch-a-Sketch project for Web Development 101. It's not perfect, but I added a couple of extra features (such as colour choice)-one improvement I could make is to change the colour choice to a radio button format and allow colour change without erasing the image. In the end, the project only took a few hours, but that was after going through HeadFirst books on HTML/CSS, JavaScript and jQuery. For some reason, my brain was stuck in HTML mode and I couldn't see how to create the webpage using only Javascript. Once I remembered the jQuery method of creating HTML elements, it was easy.  I spent some time trying to figure out how to increase colour intensity with each mouseover event. The pseudocode is easy, but I can't get the Javascript syntax right-I will go back to it  when my Javascript has improved. I spent the last day completing the Codecademy Ruby course-most