Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Gigasecond in the Crystal Track

0
0
Genius
0
0
Instructions
Test suite
Solution

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

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

Setup

Follow the setup instructions for Crystal here:

http://exercism.io/languages/crystal

More help installing can be found here:

http://crystal-lang.org/docs/installation/index.html

Making the Test Suit Pass

Execute the tests with:

$ crystal spec

In each test suite all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by changing pending to it.

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_spec.cr

require "spec"
require "../src/*"

describe "Gigasecond" do
  describe "#from" do
    it "finds gigsecond from 2011_04_25" do
      result = Gigasecond.from(Time.utc(2011, 4, 25, 0, 0, 0))
      result.should eq Time.utc(2043, 1, 1, 1, 46, 40)
    end

    pending "finds gigsecond from 1977_06_13" do
      result = Gigasecond.from(Time.utc(1977, 6, 13, 0, 0, 0))
      result.should eq Time.utc(2009, 2, 19, 1, 46, 40)
    end

    pending "finds gigsecond from 1959_07_19" do
      result = Gigasecond.from(Time.utc(1959, 7, 19, 0, 0, 0))
      result.should eq Time.utc(1991, 3, 27, 1, 46, 40)
    end

    pending "finds gigsecond with full_time specified" do
      result = Gigasecond.from(Time.utc(2015, 1, 24, 22, 0, 0))
      result.should eq Time.utc(2046, 10, 2, 23, 46, 40)
    end

    pending "finds gigsecond with full_time with day rollover" do
      result = Gigasecond.from(Time.utc(2015, 1, 24, 23, 59, 59))
      result.should eq Time.utc(2046, 10, 3, 1, 46, 39)
    end

    pending "test_with_your_birthday" do
    end
  end
end
struct Int
  def giga
    self * 1e9
  end
end

class Gigasecond
  def self.from(time : Time) : Time
    time + 1.giga.seconds
  end
end

What can you learn from this solution?

A huge amount can be learnt 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 I could read more about to develop my understanding?