Skip to content

Commit

Permalink
Add digitPositions
Browse files Browse the repository at this point in the history
  • Loading branch information
slysterous committed Apr 19, 2021
1 parent b73ec12 commit c8e60f3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions numeral.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
type Numeral struct {
digits *list.List
digitValues []rune
digitPositions map [rune]int
}

// NewNumeral initializes a numeral by providing the initial number in strings
Expand All @@ -47,6 +48,7 @@ func NewNumeral(values []rune, initial string) (*Numeral, error) {
number := Numeral{
digits: list.New(),
digitValues: values,
digitPositions: valuesToPositions(values),
}
// add digits to the number along with their state.
for i := 0; i < len(initial); i++ {
Expand All @@ -59,6 +61,14 @@ func NewNumeral(values []rune, initial string) (*Numeral, error) {
return &number, nil
}

func valuesToPositions(values []rune) map [rune]int {
var pos=map[rune]int{}
for i := 0; i < len(values) ; i++ {
pos[values[i]]=i
}
return pos
}

// newDigit creates and initializes a new digit (ring).
func newDigit(values []rune, state rune) (*ring.Ring, error) {
// initialize a new empty ring
Expand Down Expand Up @@ -174,8 +184,8 @@ func (n *Numeral) Decimal() int {
// get current ring.
r := d.Value.(*ring.Ring)
// get the index of the ring.
i := indexOf(r.Value.(rune), n.digitValues)

//i := indexOf(r.Value.(rune), n.digitValues)
i := n.digitPositions[r.Value.(rune)]
// Add digit's decimal counterpart to the decimal sum.
dec = dec + i*powInt(len(n.digitValues), di)
di++
Expand Down

0 comments on commit c8e60f3

Please sign in to comment.