Thursday, February 29, 2024
HomeSoftware EngineeringRoman Numerals Decoder in Golang

Roman Numerals Decoder in Golang


The problem

Create a perform that takes a Roman numeral as its argument and returns its worth as a numeric decimal integer. You don’t have to validate the type of the Roman numeral.

Trendy Roman numerals are written by expressing every decimal digit of the quantity to be encoded individually, beginning with the leftmost digit and skipping any 0s. So 1990 is rendered “MCMXC” (1000 = M, 900 = CM, 90 = XC) and 2008 is rendered “MMVIII” (2000 = MM, 8 = VIII). The Roman numeral for 1666, “MDCLXVI”, makes use of every letter in descending order.

Instance:

// ought to return 21
answer('XXI');

Assist:

Image    Worth
I          1
V          5
X          10
L          50
C          100
D          500
M          1,000

The answer in Golang

Choice 1:

package deal answer
var decoder = map[rune]int {
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000,
}
func Decode(roman string) int {
    if len(roman) == 0 { return 0 }
    first := decoder[rune(roman[0])]
    if len(roman) == 1 { return first }
    subsequent := decoder[rune(roman[1])]
    if subsequent > first { return (subsequent - first) + Decode(roman[2:]) }
    return first + Decode(roman[1:])
}

Choice 2:

package deal answer
func Decode(roman string) int {
  var sum int
  var Roman = map[byte] int {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000,}
  for okay, v := vary roman {
    if okay < len(roman) - 1 && Roman[byte(roman[k + 1])] > Roman[byte(roman[k])] {
      sum -= Roman[byte(v)]
    } else {
      sum += Roman[byte(v)]
    }
  }
  return sum
}

Choice 3:

package deal answer
func Decode(roman string) int {
  translateRoman := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
  var decNum, tmpNum int
  for i := len(roman) - 1; i >= 0; i-- {
    romanDigit := roman[i]
    decDigit := translateRoman[romanDigit]
    if decDigit < tmpNum {
      decNum -= decDigit
    } else {
      decNum += decDigit
      tmpNum = decDigit
    }
  }
  return decNum
}

Check instances to validate our answer

package deal solution_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)
var _ = Describe("take a look at roman to decimal converter", func() {
   It("ought to give decimal quantity from roman", func() {
     Count on(Decode("XXI")).To(Equal(21))
   })
   It("ought to give decimal quantity from roman", func() {
     Count on(Decode("I")).To(Equal(1))
   })
   It("ought to give decimal quantity from roman", func() {
     Count on(Decode("IV")).To(Equal(4))
   })
   It("ought to give decimal quantity from roman", func() {
     Count on(Decode("MMVIII")).To(Equal(2008))
   })
   It("ought to give decimal quantity from roman", func() {
     Count on(Decode("MDCLXVI")).To(Equal(1666))
   })
})
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments