package orderedcount // Use the preloaded Tuple struct as return type // type Tuple struct { // Char rune // Count int // } func OrderedCount(text string) []Tuple { tuples := make([]Tuple, 0) for _, c := range text { exists := false for _, t := range tuples { if(t.Char == rune(c)) { exists = true } } if (!exists) { tuples = append(tuples, Tuple{rune(c), 0}) } } for _, c := range text { for i := range tuples { if(tuples[i].Char == rune(c)) { tuples[i].Count++ } } } return tuples }
- package orderedcount
- // Use the preloaded Tuple struct as return type
- // type Tuple struct {
- // Char rune
- // Count int
- // }
- func OrderedCount(text string) []Tuple {
// to implement- tuples := make([]Tuple, 0)
- for _, c := range text {
- exists := false
- for _, t := range tuples {
- if(t.Char == rune(c)) {
- exists = true
- }
- }
- if (!exists) {
- tuples = append(tuples, Tuple{rune(c), 0})
- }
- }
- for _, c := range text {
- for i := range tuples {
- if(tuples[i].Char == rune(c)) {
- tuples[i].Count++
- }
- }
- }
- return tuples
- }
package orderedcount_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "codewarrior/orderedcount" ) var _ = Describe("Test Suite", func() { It("Sample Tests", func() { Expect(OrderedCount("abracadabra")).Should(Equal([]Tuple{Tuple{'a', 5}, Tuple{'b', 2}, Tuple{'r', 2}, Tuple{'c', 1}, Tuple{'d', 1}})) Expect(OrderedCount("Code Wars")).Should(Equal([]Tuple{Tuple{'C', 1}, Tuple{'o', 1}, Tuple{'d', 1}, Tuple{'e', 1}, Tuple{' ', 1}, Tuple{'W', 1}, Tuple{'a', 1}, Tuple{'r', 1}, Tuple{'s', 1}})) //Expect(OrderedCount("")).Should(Equal([]Tuple{})) }) })
- package orderedcount_test
- import (
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- . "codewarrior/orderedcount"
- )
- var _ = Describe("Test Suite", func() {
- It("Sample Tests", func() {
- Expect(OrderedCount("abracadabra")).Should(Equal([]Tuple{Tuple{'a', 5}, Tuple{'b', 2}, Tuple{'r', 2}, Tuple{'c', 1}, Tuple{'d', 1}}))
- Expect(OrderedCount("Code Wars")).Should(Equal([]Tuple{Tuple{'C', 1}, Tuple{'o', 1}, Tuple{'d', 1}, Tuple{'e', 1}, Tuple{' ', 1}, Tuple{'W', 1}, Tuple{'a', 1}, Tuple{'r', 1}, Tuple{'s', 1}}))
Expect(OrderedCount("")).Should(Equal([]Tuple{}))- //Expect(OrderedCount("")).Should(Equal([]Tuple{}))
- })
- })