-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bugs and fixed code with golint and gofmt (#1)
- Checked code with gofmt - Checkd code with golint - Renamed func because of a missing letter - Added go report card
- Loading branch information
Showing
15 changed files
with
348 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,46 @@ | ||
package collection | ||
|
||
import ( | ||
"strings" | ||
"strconv" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Return a string from a string collection by adding ", " as a seperator and | ||
// the word "and" for the last seperator. | ||
// A limit will print all elements to the limit and elements after the limit | ||
// will be grouped with a "and n more" | ||
// Oxford returns a string from a string collection by adding ", " as a seperator and the word "and" for the last seperator. | ||
// A limit will print all elements to the limit and elements after the limit will be grouped with a "and n more". | ||
// No limit is represented with with a limit <= 0 | ||
func Oxford(collection []string, limit int) string { | ||
len := len(collection) | ||
switch len { | ||
case 0: | ||
return "" | ||
case 1: | ||
return collection[0] | ||
case 2: | ||
return formatOnlyTwo(collection) | ||
} | ||
if limit > 0 { | ||
return formatCommaSeparatedWithLimit(collection, limit, len) | ||
} | ||
return formatCommaSeparated(collection, len) | ||
len := len(collection) | ||
|
||
switch len { | ||
case 0: | ||
return "" | ||
case 1: | ||
return collection[0] | ||
case 2: | ||
return formatOnlyTwo(collection) | ||
} | ||
|
||
if limit > 0 { | ||
return formatCommaSeparatedWithLimit(collection, limit, len) | ||
} | ||
|
||
return formatCommaSeparated(collection, len) | ||
} | ||
|
||
// Format a collection of two strings | ||
func formatOnlyTwo(collection []string) string { | ||
return collection[0] + " and " + collection[1] | ||
return collection[0] + " and " + collection[1] | ||
} | ||
|
||
// Format with limit | ||
func formatCommaSeparatedWithLimit(collection []string, limit int, count int) string { | ||
display := strings.Join(collection[:limit], ", ") | ||
moreCount := count - limit | ||
return display + " and " + strconv.Itoa(moreCount) + " more" | ||
display := strings.Join(collection[:limit], ", ") | ||
moreCount := count - limit | ||
return display + " and " + strconv.Itoa(moreCount) + " more" | ||
} | ||
|
||
// Format without limit | ||
func formatCommaSeparated(collection []string, count int) string { | ||
display := strings.Join(collection[:(count - 1)], ", ") | ||
return display + " and " + collection[(count - 1)] | ||
display := strings.Join(collection[:(count-1)], ", ") | ||
return display + " and " + collection[(count-1)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
package collection | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestOxford(t *testing.T) { | ||
assert.Equal(t, "", Oxford([]string{}, -1)) | ||
var testCollection = []string{ | ||
"Albert", | ||
"Norbert", | ||
"Michael", | ||
"Kevin", | ||
} | ||
assert.Equal(t, "Albert", Oxford(testCollection[:1], -1)) | ||
assert.Equal(t, "Albert and Norbert", Oxford(testCollection[:2], -1)) | ||
assert.Equal(t, "Albert, Norbert, Michael and Kevin", Oxford(testCollection, -1)) | ||
assert.Equal(t, "Albert, Norbert and 2 more", Oxford(testCollection, 2)) | ||
assert.Equal(t, "", Oxford([]string{}, -1)) | ||
|
||
var testCollection = []string{ | ||
"Albert", | ||
"Norbert", | ||
"Michael", | ||
"Kevin", | ||
} | ||
|
||
assert.Equal(t, "Albert", Oxford(testCollection[:1], -1)) | ||
assert.Equal(t, "Albert and Norbert", Oxford(testCollection[:2], -1)) | ||
assert.Equal(t, "Albert, Norbert, Michael and Kevin", Oxford(testCollection, -1)) | ||
assert.Equal(t, "Albert, Norbert and 2 more", Oxford(testCollection, 2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
package numbers | ||
|
||
import ( | ||
"math" | ||
"strconv" | ||
"math" | ||
"strconv" | ||
) | ||
|
||
// Get the ordinal only for the specific number. | ||
// Ordinal returns the ordinal string for a specific integer. | ||
func Ordinal(number int) string { | ||
absNumber := int(math.Abs(float64(number))) | ||
i := absNumber % 100 | ||
if i == 11 || i == 12 || i == 13 { | ||
return "th" | ||
} | ||
switch absNumber % 10 { | ||
case 1: | ||
return "st" | ||
case 2: | ||
return "nd" | ||
case 3: | ||
return "rd" | ||
default: | ||
return "th" | ||
} | ||
absNumber := int(math.Abs(float64(number))) | ||
|
||
i := absNumber % 100 | ||
if i == 11 || i == 12 || i == 13 { | ||
return "th" | ||
} | ||
|
||
switch absNumber % 10 { | ||
case 1: | ||
return "st" | ||
case 2: | ||
return "nd" | ||
case 3: | ||
return "rd" | ||
default: | ||
return "th" | ||
} | ||
} | ||
|
||
// Ordinalize the number by adding the Ordinal to the number. | ||
func Odinalize(number int) string { | ||
return strconv.Itoa(number) + Ordinal(number) | ||
} | ||
func Ordinalize(number int) string { | ||
return strconv.Itoa(number) + Ordinal(number) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
package numbers | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestOrdinalizesNumbers(t *testing.T) { | ||
assert.Equal(t, "1st", Odinalize(1)) | ||
assert.Equal(t, "2nd", Odinalize(2)) | ||
assert.Equal(t, "23rd", Odinalize(23)) | ||
assert.Equal(t, "1002nd", Odinalize(1002)) | ||
assert.Equal(t, "-111th", Odinalize(-111)) | ||
assert.Equal(t, "1st", Ordinalize(1)) | ||
assert.Equal(t, "2nd", Ordinalize(2)) | ||
assert.Equal(t, "23rd", Ordinalize(23)) | ||
assert.Equal(t, "1002nd", Ordinalize(1002)) | ||
assert.Equal(t, "-111th", Ordinalize(-111)) | ||
} | ||
|
||
func TestReturnsOrdinalSuffix(t *testing.T) { | ||
assert.Equal(t, "st", Ordinal(1)) | ||
assert.Equal(t, "nd", Ordinal(2)) | ||
assert.Equal(t, "rd", Ordinal(23)) | ||
assert.Equal(t, "nd", Ordinal(1002)) | ||
assert.Equal(t, "th", Ordinal(-111)) | ||
assert.Equal(t, "st", Ordinal(1)) | ||
assert.Equal(t, "nd", Ordinal(2)) | ||
assert.Equal(t, "rd", Ordinal(23)) | ||
assert.Equal(t, "nd", Ordinal(1002)) | ||
assert.Equal(t, "th", Ordinal(-111)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,105 @@ | ||
package numbers | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"sort" | ||
"errors" | ||
"regexp" | ||
"sort" | ||
) | ||
|
||
// Min. height of number which should be converted | ||
// MinValue is the min number which should be converted | ||
const MinValue = 1 | ||
// Max height of number which should be converted | ||
|
||
// MaxValue is the maxnumber which should be converted | ||
const MaxValue = 3999 | ||
// Regex to check if string is a roman number | ||
|
||
// RomanStringMatcher is a regex to check if string is a roman number | ||
const RomanStringMatcher = "^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$" | ||
|
||
// The map of roman (string) to arab number with string as key | ||
// StringMap is the map of roman (string) to arab number with string as key | ||
var StringMap = map[string]int{ | ||
"M": 1000, | ||
"CM": 900, | ||
"D": 500, | ||
"CD": 400, | ||
"C": 100, | ||
"XC": 90, | ||
"L": 50, | ||
"XL": 40, | ||
"X": 10, | ||
"IX": 9, | ||
"V": 5, | ||
"IV": 4, | ||
"I": 1, | ||
"M": 1000, | ||
"CM": 900, | ||
"D": 500, | ||
"CD": 400, | ||
"C": 100, | ||
"XC": 90, | ||
"L": 50, | ||
"XL": 40, | ||
"X": 10, | ||
"IX": 9, | ||
"V": 5, | ||
"IV": 4, | ||
"I": 1, | ||
} | ||
|
||
// The map of roman (string) to arab number with integer as key | ||
// StringMapReversed is the map of roman (string) to arab number with integer as key | ||
var StringMapReversed = map[int]string{ | ||
1000: "M", | ||
900: "CM", | ||
500: "D", | ||
400: "CD", | ||
100: "C", | ||
90: "XC", | ||
50: "L", | ||
40: "XL", | ||
10: "X", | ||
0: "IX", | ||
5: "V", | ||
4: "IV", | ||
1: "I", | ||
1000: "M", | ||
900: "CM", | ||
500: "D", | ||
400: "CD", | ||
100: "C", | ||
90: "XC", | ||
50: "L", | ||
40: "XL", | ||
10: "X", | ||
0: "IX", | ||
5: "V", | ||
4: "IV", | ||
1: "I", | ||
} | ||
|
||
// Get reversed sorted slice from StringMapReversed map | ||
func getReverseIntSlice() []int { | ||
intSlice := make([]int, 0, len(StringMapReversed)) | ||
for _, num := range StringMap { | ||
intSlice = append(intSlice, num) | ||
} | ||
sort.Sort(sort.Reverse(sort.IntSlice(intSlice))) | ||
return intSlice | ||
intSlice := make([]int, 0, len(StringMapReversed)) | ||
for _, num := range StringMap { | ||
intSlice = append(intSlice, num) | ||
} | ||
sort.Sort(sort.Reverse(sort.IntSlice(intSlice))) | ||
|
||
return intSlice | ||
} | ||
|
||
// Convert number to a roman string. Number has to be between 1 and 3999 | ||
// ToRoman converts a number to a roman string. Number has to be between 1 and 3999. | ||
// represented by MinValue and MaxValue | ||
func ToRoman(number int) (string, error) { | ||
if number < MinValue || number > MaxValue { | ||
return "", errors.New("Number not convertable") | ||
} | ||
romanString := "" | ||
intSlice := getReverseIntSlice() | ||
for ;number > 0; { | ||
for _, num := range intSlice { | ||
if number >= num { | ||
romanString += StringMapReversed[num] | ||
number -= num | ||
break | ||
} | ||
} | ||
} | ||
return romanString, nil | ||
if number < MinValue || number > MaxValue { | ||
return "", errors.New("Number not convertable") | ||
} | ||
romanString := "" | ||
intSlice := getReverseIntSlice() | ||
for number > 0 { | ||
for _, num := range intSlice { | ||
if number >= num { | ||
romanString += StringMapReversed[num] | ||
number -= num | ||
break | ||
} | ||
} | ||
} | ||
return romanString, nil | ||
} | ||
|
||
// Convert roman string to integer. Returns an error if string is not a roman | ||
// number. | ||
// FromRoman converts a roman string to an integer. Returns an error if string is not a roman number. | ||
func FromRoman(input string) (int, error) { | ||
i := len(input) | ||
i := len(input) | ||
|
||
if i == 0 || !regexp.MustCompile(RomanStringMatcher).MatchString(input) { | ||
return 0, errors.New("String not convertable") | ||
} | ||
|
||
if i == 0 || !regexp.MustCompile(RomanStringMatcher).MatchString(input) { | ||
return 0, errors.New("String not convertable") | ||
} | ||
|
||
total := 0 | ||
for ;i > 0; { | ||
i-- | ||
digit := StringMap[string(input[i])] | ||
if i > 0 { | ||
previousDigit := StringMap[string(input[(i-1)])] | ||
if previousDigit < digit { | ||
digit -= previousDigit | ||
i-- | ||
} | ||
} | ||
total += digit | ||
} | ||
return total, nil | ||
total := 0 | ||
for i > 0 { | ||
i-- | ||
digit := StringMap[string(input[i])] | ||
if i > 0 { | ||
previousDigit := StringMap[string(input[(i-1)])] | ||
if previousDigit < digit { | ||
digit -= previousDigit | ||
i-- | ||
} | ||
} | ||
total += digit | ||
} | ||
return total, nil | ||
} |
Oops, something went wrong.