Sunday, 26 June 2016

wk_two

Okay. I need to tell you something. I gave in, I was not on Cloud Nine with Cloud Nine. So I did it. I bought a MacBook. And you know what they say: Once you turn Mac theres no going back! Or something like that. So yeah. I have a UNIX system now, and after using it for a week I’m glad I got it. It’ll be good to have this continuity through Bootcamp at Makers Academy. Not being kicked out of hipster coffee shops for using a Windows machine is also nice.


So I’m really chowing down on that smoothie now. The PreCouse is well under way, with the first week under my belt and a certain confidence about the command line it’s time to actually do some, you know, Coding. Its Coding Bootcamp, after all.

You may remember from a previous post that I had to complete the first half of the book by Chris pines: Learn to Program. Well, now its time to complete the rest! This week was tough, some might say gruelling. There’s a few twists and turns in this post, it’s a bit of an emotional rollercoaster. Reader Discretion is advised.



So without further ado lets jump into some code! It’s what I was doing all week so I might as well show you some! Even though it’s basic and ugly - even now while sharing it I can see improves that could be made - it’s important to share it as I did it in order to actually track my progress.

 def old_roman_numeral num
 
   numerals_hash = { "M"=>1000,
                     "D"=>500,
                     "C"=>100,
                     "L"=>50,
                     "X"=>10,
                     "V"=>5,
                     "I"=>1}

   numerals = ''
 
   numerals_hash.each do |l, i|
       (num / i).times {numerals << l}
       num = num % i
   end

   numerals
 
 end

This is one of the first programs I wrote this week (the first of MANY).  Its function is fairly straight-forward: it converts any integer (whole number) into 'old school roman numerals'.  Old school means no bells or whistles; 4 is IIII, 9 is VIIII. You get me? Numerals you'd be used to might be a little different - I'll tell you about that in a sec.  First let me explain how this program works.

It's just one method - called old_roman_numeral and takes one argument (num). This means that you pass the method something for it to play around with - in this case it would be that integer I was telling you about. Now I don't think we were necessarily meant to use a hash, but I did as I had actually completed this exercise before starting the PreCourse and felt like challenging myself.   The numerals_hash contains pairs of data - each letter has an integer associated with it (the pairs are the values each letter has as a roman numeral.. but you probably worked that bit out already).  Next it creates an empty string called numerals, this is where all the numeral letters are going to be put.

Then we get to where the magic happens. No, not my bed room, how presumptuous of you! I mean the bit in the method where things happen. See that numerals_hash.each business? Thats where it does the things. The .each part is a little method that will iterate over every element. In this case it lets us do something with each of the letter-integer pairs in the hash. It defines each element in the pair as l, for letter, and i, for integer.  Then each time it iterates through the hash it will define the argument integer passed to the method by i and this will only return whole numbers it will then append l onto the empty string that many times.  It then sets num to be the modulus of the integer from that pair before moving onto the next pair in the hash.

After doing this for each pair in the hash, it returns the string, which has now been populated with the appropriate letters. Clever, eh?



The next task was to make it so that the method spat out the new roman numerals - you know, like 4 is IV, 9 is IX.  Well, that was 'mathsy' and I got confused so I just ended up changing my hash to include a few more pairs:

numerals_hash = { "M"=>1000,
                    "IM"=>999,
                    "CM"=>900,
                    "D"=>500,
                    "CD"=>400,
                    "C"=>100,
                    "XC"=>90,
                    "L"=>50,
                    "XL"=>40,
                    "X"=>10,
                    "IX"=>9,
                    "V"=>5,
                    "IV"=>4,
                    "I"=>1}

What? It got the job done.

stop it Jen, I thought we were friends!


So lets fast forward now to the end of the week. Progress has been made, programs have been written.  Here's a more complicated one.  It's emotionally intense - the tree dies :(. Rated PG: minor peril

class OrangeTree
    def initialize
      @age = 0
      @height = 0
      @oranges = 0
      @alive = true
    end

    def height
      @alive == true ? @height = ((@age * 0.4)).round(1) : "It's dead. :("
    end

    def grow_oranges
      if @alive == true
        @oranges = (@height * 15 - 25) unless @age <= 5
      else
        @oranges = 0
      end
    end

    def count_the_oranges
      @alive == true ? @oranges.to_i : "A dead tree has no oranges. :("
    end

    def pick_an_orange
      if @alive == true
        @oranges =-1 unless @orange == 0
      else
        "A dead tree has nothing to pick. :("
      end
    end

    def die
      @alive = false
    end

    def one_year_passes
      @age += 1
      if @age == 26
          self.die
        end
      self.height unless @alive == false
      self.grow_oranges unless @alive == false
      return "Oh, no! The tree is too old, and has died. :(" if @age == 26
      return "This year your tree grew to #{@height}m tall,
              and produced #{@oranges.to_i} oranges." if @alive == true
      return "A year later, the tree is still dead. :(" if @alive == false
    end

  end

This is where you start to realise you're no longer a man but a god, with ULTIMATE POWER over the the things you create. Sort of...



This program was probably one of the first I wrote that hinted at Object Oriented Design (OOD).  This isn't something I needed to worry about, but it'll certainly be relevant later on.  So what does this code do?

It calls into existence a new Class of object. The OrangeTree. It defines it's variables immediately with initialize.  Its @height is 0, @age is 0, has 0 @oranges and @alive is true. The @ before the variable means it's a class attribute.  Basically a variable assigned to the class.  I'll glaze over what each method within this class does specifically and just tell you that every year that passes it gets older, taller and eventually bears an amount of fruit proportionate to its height.  After 26 years though, it dies. Thats as much as I want to say, I'm still trying to hold it together.


"First I was angry, then I was sad, then I was just grateful for the time we got together."
- Jess, fellow Maker -





So thats all the coding I'll be talking about in this post, I could go on and on, but I won't.  I just wanted to give you a few examples to see what I've been doing so far.  Don't worry though, I'm not finished.


Other stuff happened this week too. Most notably: I got run down.

I've found the PreCourse so far to be so enjoyable, I've actually found it difficult to put it down at the end of the day.  I've easily spent 40-50 hours a week on it and I'm not sure thats a good thing.  This week I really had to take a step back and look at how I work.  I've definitely found it difficult to decompress at the end of the day, I've had dreams of coding.  Woke up on Thursday morning having solved a Codewars Kata in my sleep. Sounds useful!  But I think it actually works to my detriment. It's a good thing that I'm enjoying it, of course, but I understand, now, the importance of switching off.  I'm working on it.



Also during the week we were asked to get a mentor. It's my understanding that in the last cohort they were assigned.  Not this time! We had to awkwardly find our own! I thought I was done with online dating.. Still, my vast experience in the field fully prepared me to charm my 'chosen' with enough 'quirkiness' to break the ice.. I think.



Overall this week has been great. It's not been easy, but it has been immensely satisfying getting through all the work and seeing everything I do work properly.  Chris Pines' book is probably no ones favourite but the amount of knowledge it succinctly imparts is undeniable. I  can't wait to put it all into practice.

No comments:

Post a Comment