Skip to content

Commit

Permalink
go/roman-numerals: 3rd iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Sep 22, 2023
1 parent b170f98 commit 42a456b
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 251 deletions.
4 changes: 2 additions & 2 deletions go/roman-numerals/benchstat-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ goarch: amd64
pkg: romannumerals
cpu: 12th Gen Intel(R) Core(TM) i5-1240P
BenchmarkRomanNumerals
BenchmarkRomanNumerals-8 35883 32718 ns/op 25428 B/op 153 allocs/op
BenchmarkRomanNumerals-8 32944 36660 ns/op 27573 B/op 169 allocs/op
PASS
ok romannumerals 1.524s
ok romannumerals 1.582s
4 changes: 2 additions & 2 deletions go/roman-numerals/benchstat-old.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ goarch: amd64
pkg: romannumerals
cpu: 12th Gen Intel(R) Core(TM) i5-1240P
BenchmarkRomanNumerals
BenchmarkRomanNumerals-8 163629 7754 ns/op 256 B/op 63 allocs/op
BenchmarkRomanNumerals-8 35883 32718 ns/op 25428 B/op 153 allocs/op
PASS
ok romannumerals 1.349s
ok romannumerals 1.524s
16 changes: 8 additions & 8 deletions go/roman-numerals/coverage.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@
)

// ToRomanNumeral returns a roman numeral string and and error code.
func ToRomanNumeral(input int) (string, error) <span class="cov6" title="49">{
func ToRomanNumeral(input int) (string, error) <span class="cov6" title="52">{
if input &lt;= 0 </span><span class="cov2" title="3">{
return "", errors.New("Roman numerals can't be less than or equal to 0")
}</span>

// Only process numbers &lt;=3k.
<span class="cov6" title="46">if input &gt; 3_000 </span><span class="cov1" title="2">{
<span class="cov6" title="49">if input &gt; 3_999 </span><span class="cov1" title="2">{
return "", errors.New("Roman numerals were apparently rarely greater than 3k")
}</span>

<span class="cov6" title="44">d2r := map[int]string{
<span class="cov6" title="47">d2r := map[int]string{
1: "I",
4: "IV",
5: "V",
Expand All @@ -117,23 +117,23 @@

decimals := make([]int, 0, len(d2r))

for d := range d2r </span><span class="cov10" title="572">{
for d := range d2r </span><span class="cov10" title="611">{
decimals = append(decimals, d)
}</span>

<span class="cov6" title="44">sort.Sort(sort.Reverse(sort.IntSlice(decimals)))
<span class="cov6" title="47">sort.Sort(sort.Reverse(sort.IntSlice(decimals)))

for _, decimal := range decimals </span><span class="cov10" title="572">{
for _, decimal := range decimals </span><span class="cov10" title="611">{
roman := d2r[decimal]

for input &gt;= decimal </span><span class="cov7" title="117">{
for input &gt;= decimal </span><span class="cov7" title="131">{
output += roman

input -= decimal
}</span>
}

<span class="cov6" title="44">return output, nil</span>
<span class="cov6" title="47">return output, nil</span>
}
</pre>

Expand Down
16 changes: 8 additions & 8 deletions go/roman-numerals/coverage.out
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mode: count
romannumerals/roman_numerals.go:10.48,11.16 1 49
romannumerals/roman_numerals.go:10.48,11.16 1 52
romannumerals/roman_numerals.go:11.16,13.3 1 3
romannumerals/roman_numerals.go:16.2,16.19 1 46
romannumerals/roman_numerals.go:16.2,16.19 1 49
romannumerals/roman_numerals.go:16.19,18.3 1 2
romannumerals/roman_numerals.go:20.2,40.21 4 44
romannumerals/roman_numerals.go:40.21,42.3 1 572
romannumerals/roman_numerals.go:44.2,46.35 2 44
romannumerals/roman_numerals.go:46.35,49.24 2 572
romannumerals/roman_numerals.go:49.24,53.4 2 117
romannumerals/roman_numerals.go:56.2,56.20 1 44
romannumerals/roman_numerals.go:20.2,40.21 4 47
romannumerals/roman_numerals.go:40.21,42.3 1 611
romannumerals/roman_numerals.go:44.2,46.35 2 47
romannumerals/roman_numerals.go:46.35,49.24 2 611
romannumerals/roman_numerals.go:49.24,53.4 2 131
romannumerals/roman_numerals.go:56.2,56.20 1 47
2 changes: 1 addition & 1 deletion go/roman-numerals/roman_numerals.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func ToRomanNumeral(input int) (string, error) {
}

// Only process numbers <=3k.
if input > 3_000 {
if input > 3_999 {
return "", errors.New("Roman numerals were apparently rarely greater than 3k")
}

Expand Down
5 changes: 3 additions & 2 deletions go/roman-numerals/roman_numerals_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func ExampleToRomanNumeral() {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
50, 51, 100, 101,
500, 501, 1_000, 1_001,
3_000, 3_001,
3_000, 3_001, 4_001,
}

for _, n := range numbers {
Expand Down Expand Up @@ -37,5 +37,6 @@ func ExampleToRomanNumeral() {
// 1000 -> "M", e: <nil>
// 1001 -> "MI", e: <nil>
// 3000 -> "MMM", e: <nil>
// 3001 -> "", e: Roman numerals were apparently rarely greater than 3k
// 3001 -> "MMMI", e: <nil>
// 4001 -> "", e: Roman numerals were apparently rarely greater than 3k
}
5 changes: 3 additions & 2 deletions go/roman-numerals/romannumerals-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ToRomanNumeral returns a roman numeral string and and error code.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
50, 51, 100, 101,
500, 501, 1_000, 1_001,
3_000, 3_001,
3_000, 3_001, 4_001,
}

for _, n := range numbers {
Expand Down Expand Up @@ -65,7 +65,8 @@ ToRomanNumeral returns a roman numeral string and and error code.
1000 -> "M", e: <nil>
1001 -> "MI", e: <nil>
3000 -> "MMM", e: <nil>
3001 -> "", e: Roman numerals were apparently rarely greater than 3k
3001 -> "MMMI", e: <nil>
4001 -> "", e: Roman numerals were apparently rarely greater than 3k
```

</p>
Expand Down
Loading

0 comments on commit 42a456b

Please sign in to comment.