Created
March 12, 2019 20:18
-
-
Save dansku/c63b67d22f0add1c49bf3d8818cabc54 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Package raindrops provides all the tools for the raindrops exercism task. | |
| package raindrops | |
| import ( | |
| "strconv" | |
| "strings" | |
| ) | |
| // Contains check if string exists in slice | |
| func Contains(text string, slice []string) bool { | |
| for _, a := range slice { | |
| if a == text { | |
| return true | |
| } | |
| } | |
| return false | |
| } | |
| // GetNumberFactors gets a integer as input and returns a slice of all the factors of that number. | |
| func GetNumberFactors(number int) []int { | |
| var factors []int | |
| for i := 1; i <= number; i++ { | |
| if number%i == 0 { | |
| factors = append(factors, i) | |
| } | |
| } | |
| return factors | |
| } | |
| // Convert gets an integer number and returns the right string output as requested in the task. | |
| func Convert(number int) string { | |
| factors := GetNumberFactors(number) | |
| var speark []string | |
| for _, number := range factors { | |
| if number%3 == 0 { | |
| if !Contains("Pling", speark) { | |
| speark = append(speark, "Pling") | |
| } | |
| } else if number%5 == 0 { | |
| if !Contains("Plang", speark) { | |
| speark = append(speark, "Plang") | |
| } | |
| } else if number%7 == 0 { | |
| if !Contains("Plong", speark) { | |
| speark = append(speark, "Plong") | |
| } | |
| } | |
| } | |
| if len(speark) > 0 { | |
| return strings.Join(speark, "") | |
| } | |
| return strconv.Itoa(number) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment