Skip to content

Commit

Permalink
Go challenge 3 (YearOfProgramming#427)
Browse files Browse the repository at this point in the history
* Add solution for max occurance challenge

* Add test cases

* Add README.md

* Move code to correct location
  • Loading branch information
Naren authored and erocs committed Jan 24, 2017
1 parent 1835649 commit db0e5f3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
15 changes: 15 additions & 0 deletions challenge_3/go/makernaren/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Single Number
#### To run the code :
* Open this directory in terminal and run `go run max_occurance.go` to compile and run the code.
* To test the code just run, `go test` in same directory.
```
$ go run max_occurance.go
gGiven array is : [2 a l 3 l 4 k 2 3 4 a 6 c 4 m 6 m k 9 10 9 8 7 8 10 7]
Element that occur only max number of times : 4
$ go test
PASS
ok path/to/2017Challenges/challenge_2/rust/makernaren 0.005s
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
```
38 changes: 38 additions & 0 deletions challenge_3/go/makernaren/max_occurance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Take an array of mixed data types and find the element that occurs
// most number of times.
package main

import (
"fmt"
)

// Finds the maximum occurance, takes in interface as input and output params
//as elements in the given array may be of any data type
func FindMajority(input []interface{}) (result interface{}) {
// Create a hashmap, where element of that array is key and its number of
// occurances is the value.
occurances := make(map[interface{}]int)
for _, key := range input {
occurances[key]++
}
// Return when we find the max occurance.
max := 0
kv_pair := make([]interface{}, 2)
for k, v := range occurances {
// Replace the variable until you find max
if v > max {
max = v
kv_pair[0] = k
kv_pair[1] = v
}
}
return kv_pair[0]
}

func main() {
given := []interface{}{2, "a", "l", 3, "l", 4, "k", 2, 3, 4, "a", 6, "c",
4, "m", 6, "m", "k", 9, 10, 9, 8, 7, 8, 10, 7}
fmt.Printf("Given array is : %v \n", given)
result := FindMajority(given)
fmt.Printf("Element that occur only max number of times : %v \n", result)
}
41 changes: 41 additions & 0 deletions challenge_3/go/makernaren/max_occurance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// max_occurance_test
package main

import (
"testing"
)

func TestIntegers(t *testing.T) {
input := []interface{}{2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6}
output := 4
result := FindMajority(input)
if result != output {
t.Error("for", input,
"expected", output,
"got", result)
}
}

func TestStrings(t *testing.T) {
input := []interface{}{"a", "a", "b", "b", "b", "b", "c", "c", "c", "c",
"c", "d", "d", "e"}
output := "c"
result := FindMajority(input)
if result != output {
t.Error("for", input,
"expected", output,
"got", result)
}
}

func TestMultipleTypes(t *testing.T) {
input := []interface{}{2, "a", "l", 3, "l", 4, "k", 2, 3, 4, "a", 6, "c",
4, "m", 6, "m", "k", 9, 10, 9, 8, 7, 8, 10, 7, 7, 7, 7, 7, 7, 7, 7, 7}
output := 7
result := FindMajority(input)
if result != output {
t.Error("for", input,
"expected", output,
"got", result)
}
}

0 comments on commit db0e5f3

Please sign in to comment.