This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Exercise 2


Methods, flow control, arrays, iterators, and making it your own

 

Task Discussion


  • Roelof said:

    Here my code on chapter 5:  ttps://github.com/roelof1967/homework/tree/master/chapter5

     

    Roelof

    on Sept. 16, 2013, 1:49 p.m.
  • Si Dunn said:

    I tried reworking Tony Sharper's nice 10 Green Bottles code into 99 Bottles of Beer on the Wall, and I couldn't make my version work quite right. I couldn't get all of the lines to stay in order. So I did some web research and found several examples of 99 Bottles of Beer on the Wall written for Ruby. Most of them use code and techniques beyond where we are in this class. But I found a couple of examples that don't seem to go beyond what we have been exposed to thus far. I picked one and reworked it.

    Don't know how many of you actually have ever sung ALL verses  of "99 Bottles of Beer on the Wall." This version of the code goes -- boringly, ad nauseum -- from 99 all the way down to zero, AND I added a sad little out-of-beer verse at the end.

    I'm sure some of you can simplify and improve this code.

     

    https://github.com/sidunn/coursework/blob/master/99_bottles_of_beer.rb

    on Nov. 30, 2012, 8:10 p.m.
  • Si Dunn said:

    I liked what Tony Shaper did with the Leap Year assignment. And...I'm not seeking a career as a Ruby developer--just having fun learning what I can from the text and from the others in this class.

    I "borrowed" part of Tony's code, made a few superficial and cosmetic "improvements" to it, and tested the new file a few times. Good work, Tony. 

     

    https://github.com/sidunn/coursework/blob/master/leap_year_test1.rb

    on Nov. 30, 2012, 4:37 p.m.

    Andre Dublin said:

    Its all good, thats the point here learning from others!

    Otherwise looks good!

    on Nov. 30, 2012, 4:38 p.m. in reply to Si Dunn
  • Tony Shaper said:

    Hi,

    I have submitted my answers to exercises for ch06 at

    https://github.com/cylons/coursework/blob/master/ltp/ch06

    Rather than "xx bottles of beer on the wall", I have used a similar song "Ten Green Bottles"

    As ever, any comments welcome.

    on March 30, 2012, 5:41 p.m.
  • sedmonds said:

    Submitted: https://github.com/sedmonds/p2pu_ltp/tree/master/Week-2

    I have still to complete the 'leap years' and 'pawn shop' programs.

    For the leap years program, I was hoping to build on last week and improve how I think about and craft a solution. If I break the problem up in tasks, I am asked to: define what is and is not considered a leap year, loop from the starting year up to the ending year, and tell the computer to print each leap year between and including these two numbers. I wonder how a programmer would craft these directions for themselves. Thoughts?

    My program current looks like this:

    count = 0
    
    puts 'Starting year: '
    
    beginYear = gets.chomp
    
    puts 'Ending year: '
    
    endYear = gets.chomp
    
    
    while beginYear <= endYear
    
      puts beginYear.to_i
    
      count = beginYear.to_i + 1
    
      # Do we have a Leap year?
    
    
      if beginYear.to_i % 4 == 0 
    
        puts beginYear.to_i
    
      else beginYear.to_i % 100 != 0 && beginYear.to_i % 400 == 0
    
        puts 'Not a Leap Year'
    
      end
    
    end
    
    

    on March 29, 2012, 2:16 p.m.

    Tony Shaper said:

    The way I thought about this problem was as follows. It would be easier to write the program if ruby provided a function that would return true if a given number was a leap year. Since ruby doesn't provide one I decided to write it myself. I called it is_leap. I wrote a second version of is_leap called is_leap2 which is shorter but, I think, much harder to understand.

     

    By the way notice that I convert the input  year strings to numbers straight away which means that I do not need to convert my variables to numbers every time thay are used.

    My program is shown below

     

    def is_leap ( year)
        if year % 400 == 0
            return true
        end
        if year % 100 == 0
            return false
        end
        if year % 4 == 0
            return true
       end
       return false
    end
     
     
    def is_leap2( year)
       if year % 400 == 0  or (year % 100 != 0 and year % 4 == 0)
                 return true
       else
                return false
       end
    end
     
     
    puts "Input start year"
    start_year = gets.chomp.to_i
     
     
    puts "Input end year"
    end_year = gets.chomp.to_i
     
     
    for test_year in start_year..end_year
     
       if is_leap(test_year)
       puts " #{test_year} is leap year"
       else
       puts " #{test_year} is not leap year"
       end
    end
     
    puts "----------"
    puts " using is_leap2"
    puts " "
     
    for test_year in start_year..end_year
     
       if is_leap2(test_year)
       puts " #{test_year} is leap year"
       else
       puts " #{test_year} is not leap year"
       end
     

    end

     


    on March 30, 2012, 6:16 p.m. in reply to sedmonds

    sedmonds said:

    Ahhh, #{test_year}. 

     

    Thanks for your feedback Tony.

    on March 31, 2012, 12:37 a.m. in reply to Tony Shaper

    Andre Dublin said:

    Excellent job.

    In ruby you don't have to explictly use a return statement in your methods

    Ruby methods will always return something, even if is nil ( null )

    ex.

    def sqrt(x)
    
        x * x
    
    end
    
    is the same as
    def sqrt(x)
    
      return x * x
    
    end

    on March 31, 2012, 7:24 a.m. in reply to Tony Shaper
  • sedmonds said:

    Hi All,

    I am reading Ch. 5: Mixing It Up in LTP and ran across an interesting paragraph in the book.

    Quoted section from Another Look at Puts:

    Why do these three all print the same thing? Well, the last two should, since 20.to_s is '20'. But what about the first one, the integer 20? For that matter, what does it even mean to write out the integer 20? When you write a 2 and then a 0 on a piece of paper, you are writing down a string, not an integer. The integer 20 is the number of fingers and toes I have; it isn't a 2 followed by a 0.

    I found the sentence curious: The integer 20 is the number of fingers and toes I have; it isn't a 2 followed by a 0.

    So, I am wondering if specifically-related to the ''puts' function: whatever puts acts-on will be converted to a string by way of the functions nature?

    on March 20, 2012, 9:17 p.m.

    Andre Dublin said:

    Thats correct puts will coerce the to_s function on the object it is called upon.  If to_s is not available you will return a MethodMissing error.

    A little off topic but in Ruby you can enclose this in a "try/catch" block to test if the object has the method, in Ruby its begin, rescue, ensure, end.  However Ruby does have a throw/catch, but I don't see it as much as the latter.  There's also a retry function for the begin block to repeat the process if needed.

    ex, simple begin rescue ensure block

    begin
      #begin to see if puts works
      puts 20
    rescue MethodMissing => e
      #puts the error message, here you can format it nicely and perform other actions when an error occurs
      puts e
    ensure
      #lets print 20 instead
      print 20
    end

    on March 20, 2012, 10:42 p.m. in reply to sedmonds

    Rhodee said:

    The dirty secret: everything in Ruby is a string. I say this tongue in cheek. But Ruby is usually smart enough to know what type you are wishing to act on.

     

    I find it best to always assume what you handling is a string. You **can** do string math (e.g. '1' + '1' == 11). SInce you probably want to do some math a method like to_i makes it remarkably easy for you to actual math (e.g. 1.+(1) == 2).

     

    If you ever run into errors that specify a String can't be coerced into a Fixnum and want to know why do the following:

     

    1. fire up irb in your terminal.

    2. and inspect the class of the object like so:

    user_response.class

     

    If the console returns string, you need to convert it to a data pe that Ruby knows how to work with (like an Integer if you are doing math).

    on March 20, 2012, 10:48 p.m. in reply to sedmonds

    sedmonds said:

    Thanks for the feedback Tony!

    on March 31, 2012, 12:01 a.m. in reply to sedmonds
  • Andre Dublin said:

    Hey everybody, week 2 will include continued ready of LTP, and an exercise that will flex your current Ruby programming skills.  This will continue over to week 3 so don't feel to pressured.

    on March 19, 2012, 9:29 p.m.