Skip to content

Instantly share code, notes, and snippets.

@kotp
Last active July 12, 2018 02:49

Revisions

  1. kotp revised this gist Nov 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion w1e6_a1.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@

    def convert(temperature_in_F)
    (temperature_in_F - 32) / 1.8
    (temperature_in_F - 32) / 1.8 # This solution can cause problems. Why?
    end

    puts "%.2f" % convert(-40) # => -40.00
  2. kotp revised this gist Apr 18, 2012. 15 changed files with 110 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions readme.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    This is a collection of exercises, and answers.

    Filenames will be in the form of w1e1.rb.
    Answers will be w1e1a1.rb with subsequent 'best answers' being numbered consecutively.
    15 changes: 15 additions & 0 deletions w1e1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    Exercise1. Before executing the code given below, guess the results. Next, execute the code. Did you get it right? If you did not get it right, can you think of why?

    Discuss your first guess and what you got when running the code.
    Goal: Understanding operator precedence and association.

    ```ruby
    y = false
    z = true
    x = y or z
    puts x
    (x = y) or z
    puts x
    x = (y or z)
    puts x
    ```
    11 changes: 11 additions & 0 deletions w1e1_a1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@

    ```ruby
    y = false # simply an assignment
    z = true # simply an assignment
    x = y or z # x is assigned false though true is returned for the line
    puts x # prints false
    (x = y) or z # x is again assigned false while the line returns true
    puts x # prints false
    x = (y or z) # (y or z) returns true, and that result is assigned to x
    puts x # prints true as the parentheses gives precedence to or
    ```
    6 changes: 6 additions & 0 deletions w1e2.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    Read the sprintf documentation and the % documentation in the String class and
    figure out the output being printed by of this Ruby code.

    ```ruby
    puts "%05d" % 123
    ```
    12 changes: 12 additions & 0 deletions w1e2_a1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    sprintf format_string

    %[flags][width][.precision]type

    For %05d:

    flags:
    0 = Pad with zeros, not spaces.
    width:
    5
    type:
    d = Convert argument as a decimal number.
    1 change: 1 addition & 0 deletions w1e2_a1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    puts "%05d" % 123 # => Prints 00123
    9 changes: 9 additions & 0 deletions w1e3.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    Exercise3. Write a Ruby program that displays how old I am, in years, if I am
    979000000 seconds old.

    Display the result as a floating point (decimal) number to two decimal places (
    for example, 17.23).

    Note: To format the output to say 2 decimal places, we can use the Kernel's
    format method. For example, if x = 45.5678 then format("%.2f", x) will return
    the string 45.57
    6 changes: 6 additions & 0 deletions w1e3_a1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    def how_old(age_in_seconds)
    Float(age_in_seconds) / 60 / 60 / 24 / 365
    end

    age_in_seconds = 979_000_000
    puts "You are %.2f years old" % how_old(age_in_seconds)
    2 changes: 2 additions & 0 deletions w1e4.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    Exercise4. Write a Ruby program that tells you how many minutes there are in a
    year (do not bother right now about leap years etc.).
    4 changes: 4 additions & 0 deletions w1e4_a1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # Almost a sarcastic answer. But in reality, the simplest thing that solves
    # the problem is the best solution, generally, so this is an awesome start.

    puts 525600
    7 changes: 7 additions & 0 deletions w1e4_a2.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    minutes = 60 * 24 * 365

    puts "There are #{minutes} minutes in a year."


    # => "There are 525600 minutes in a year"

    9 changes: 9 additions & 0 deletions w1e5.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    Exercise 5. The following program prints the value of the variable. Why?

    ```ruby
    my_string = 'Hello Ruby World'
    def my_string
    'Hello World'
    end
    puts my_string
    ```
    7 changes: 7 additions & 0 deletions w1e5_a1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    The puts command is pointing to the variable 'my_string', where the output is
    'Hello Ruby World'. The method 'my_string' is never called.

    The method can be called in several ways. The simplest is my_string() with the
    parenthesis making it clear that it is a method call.

    Generally, you would carefully consider the wisdom of using the same name for a variable as a method in the same scope.
    11 changes: 11 additions & 0 deletions w1e6.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    Exercise6. Write a method called convert that takes one argument which is a
    temperature in degrees Fahrenheit.

    This method should return the temperature in degrees Celsius.

    To format the output to say 2 decimal places, we can use the Kernel's format method.

    For example, if x = 45.5678 then format("%.2f", x) will return the string "45.57".
    This is also to say that if x = 10 then format("$.2f", x) will return the string "10.00"

    Another way is to use the round function as follows: puts (x*100).round/100.0
    6 changes: 6 additions & 0 deletions w1e6_a1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@

    def convert(temperature_in_F)
    (temperature_in_F - 32) / 1.8
    end

    puts "%.2f" % convert(-40) # => -40.00
  3. kotp created this gist Apr 18, 2012.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    A collection of exercises and answers.