🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of remcopeereboom

remcopeereboom's solution

to Gigasecond in the Ruby Track

Published at Jul 13 2018 · 10 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Calculate the moment when someone has lived for 10^9 seconds.

A gigasecond is 10^9 (1,000,000,000) seconds.


For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby gigasecond_test.rb

To include color from the command line:

ruby -r minitest/pride gigasecond_test.rb

Source

Chapter 9 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=09

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

gigasecond_test.rb

require 'minitest/autorun'
require_relative 'gigasecond'

# Common test data version: 1.0.0 61e7d70
class GigasecondTest < Minitest::Test
  def test_date_only_specification_of_time
    # skip
    assert_equal Time.utc(2043, 1, 1, 1, 46, 40), Gigasecond.from(Time.utc(2011, 4, 25, 0, 0, 0))
  end

  def test_second_test_for_date_only_specification_of_time
    skip
    assert_equal Time.utc(2009, 2, 19, 1, 46, 40), Gigasecond.from(Time.utc(1977, 6, 13, 0, 0, 0))
  end

  def test_third_test_for_date_only_specification_of_time
    skip
    assert_equal Time.utc(1991, 3, 27, 1, 46, 40), Gigasecond.from(Time.utc(1959, 7, 19, 0, 0, 0))
  end

  def test_full_time_specified
    skip
    assert_equal Time.utc(2046, 10, 2, 23, 46, 40), Gigasecond.from(Time.utc(2015, 1, 24, 22, 0, 0))
  end

  def test_full_time_with_day_roll_over
    skip
    assert_equal Time.utc(2046, 10, 3, 1, 46, 39), Gigasecond.from(Time.utc(2015, 1, 24, 23, 59, 59))
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 6, BookKeeping::VERSION
  end
end
class Gigasecond
  # Returns the UTC time that someone will celebrate their 1Gs birthday.
  #
  # @param time_of_birth [Time] 
  # @return [Time] 1Gs Birthday time
  def self.from(time_of_birth)
    time_of_birth + 1e9
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of remcopeereboom

Ridiculously trivial. Ruby makes life easy...

Avatar of remcopeereboom

Just benchmarked, 50,000 times 1e9 versus 10**9 and found that the latter is a whopping .8s faster. I thought Ruby automatically reads 1e9 as an integer, but clearly it doesn't!

Avatar of deleted-github-user-6701528
deleted-github-user-6701528
commented over 5 years ago

A quick class check says 1E9 resolves to a float and 10 ** 9 to a Fixnum. That probably explains it. Quirky.

Avatar of remcopeereboom

@sleeper-public Yeah it's nasty. You could also do 1_000_000_000 to avoid having the interpreter do any computation. 10**9 actually computes the exponential which is even more expensive. If only the interpreter checked for an integer in exponent notation...

Avatar of deleted-github-user-6701528
deleted-github-user-6701528
commented over 5 years ago

Really? I'll have to try that. 1_000_000_000 reads a little better anyway per keystroke. I was thinking the same thing about the E notation, although maybe they stick with floats so the behaviour is consistent and non-ambiguous.

Avatar of remcopeereboom

Hmm interesting thought. You could be surprised to get a Fixnum if you're calling certain methods on it. I wonder how many bugs Matz has saved me from without me even knowing... I've just been getting into elixir and it is the same there. I don't think C++ did it that way, but maybe that was the compiler optimizing? Edit: It was a compiler flag usually turned off.

Avatar of deleted-github-user-6701528
deleted-github-user-6701528
commented over 5 years ago

Ah!

Avatar of ledestin

It could be more abstract. After all, any time can be given, not only birth time.

Avatar of remcopeereboom

@ledestin It could, but the readme speaks of birthdays. I've actually discussed some of the issues of generalizing with some exercisists. You might like the discussion here.

Avatar of ledestin

Didn't find that conversation related to this code.

The thing is, for anniversary you'd have to have a module Anniversary or something. As things stand, code tells one story and comments tell another.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?