Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 66 additions & 45 deletions assignments/assignment08.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,86 @@
# implement conversion between integers and roman numerals
# validate using MiniTest unit tests

require 'MiniTest/autorun'
module Assignment08
class RomanNumeral

class RomanNumeral
def initialize(i)
# your implementation here
@codex = {
1 => 'I',
4 => 'IV',
5 => 'V',
9 => 'IX',
10 => 'X',
20 => 'XX',
30 => 'XXX',
40 => 'XL',
50 => 'L',
}
@digits = i
end

def to_s
# your implementation here
end

def to_i
# your implementation here
roman = ''
@codex.keys.reverse.each do |logical_number|
while @digits >= logical_number
@digits -= logical_number
roman += @codex[logical_number]
end
end
roman
end

# bonus: create from Roman Numeral
def self.from_string
# your implementation here
# returns a new instance
end
def to_i
@digits
end

# bonus: create from Roman Numeral
def self.from_string
# your implementation here
# returns a new instance
end

end

end

# expected results:
RomanNumeral.new(1).to_s # => 'I'
RomanNumeral.new(2).to_s # => 'II'
RomanNumeral.new(3).to_s # => 'III'
RomanNumeral.new(4).to_s # => 'IV'
RomanNumeral.new(5).to_s # => 'V'
RomanNumeral.new(6).to_s # => 'VI'
RomanNumeral.new(9).to_s # => 'IX'
RomanNumeral.new(10).to_s # => 'X'
RomanNumeral.new(19).to_s # => 'XIX'
RomanNumeral.new(32).to_s # => 'XXXII'
RomanNumeral.new(51).to_s # => 'LI'

# bonus:
RomanNumeral.from_string('III').to_i # => 3
RomanNumeral.from_string('IX').to_i # => 9
RomanNumeral.from_string('IX').to_i # => 9
class TestRomanNumeral < Minitest::Test

# given any arbitrary integer n:
n == RomanNumeral.from_string(RomanNumeral.new(n).to_s).to_i
# expected results:
def initialize
assert_equal "I", RomanNumeral.new(1).to_s # RomanNumeral.new(1).to_s # => 'I'
assert_equal "II", RomanNumeral.new(2).to_s # RomanNumeral.new(2).to_s # => 'II'
assert_equal "III", RomanNumeral.new(3).to_s # RomanNumeral.new(3).to_s # => 'III'
assert_equal "IV", RomanNumeral.new(4).to_s # RomanNumeral.new(4).to_s # => 'IV'
assert_equal "V", RomanNumeral.new(5).to_s # => RomanNumeral.new(5).to_s
assert_equal "VI", RomanNumeral.new(6).to_s # => RomanNumeral.new(6).to_s
assert_equal "IX", RomanNumeral.new(9).to_s # => RomanNumeral.new(9).to_s
assert_equal "X", RomanNumeral.new(10).to_s # => RomanNumeral.new(10).to_s
assert_equal "XIX", RomanNumeral.new(19).to_s # => RomanNumeral.new(19).to_s
assert_equal "XXXII", RomanNumeral.new(32).to_s # => RomanNumeral.new(32).to_s
assert_equal "LI", RomanNumeral.new(51).to_s # => RomanNumeral.new(51).to_s
end
end

def golden_ratio(precision)
g = 1.61803399
precision.times { g = 1 + 1/g }
return g.round(precision)
end

end


# ========================================================================================
# Problem 2: Golden Ratio
class TestGoldenRatio < Minitest::Test

# Golden Ratio is ratio between consecutive Fibonacci numbers
# calculate the golden ratio up to specified precision
# validate using MiniTest unit tests
# expected results:
def initialize
assert_equal 1.62, golden_ratio(2) # golden_ratio(2) # => 1.62
assert_equal 1.61803, golden_ratio(5) # golden_ratio(5) # => 1.61803
assert_equal 1.61803399, golden_ratio(8) # golden_ratio(8) # => 1.61803399
end

module Assignment08
def golden_ratio(precision)
# your implementation here
end
end

# expected results:
golden_ratio(2) # => 1.62
golden_ratio(5) # => 1.61803
golden_ratio(8) # => 1.61803399
end