From db0e5f3142ed283c140b2b000c761a9c24e7f4d3 Mon Sep 17 00:00:00 2001 From: Naren Date: Tue, 24 Jan 2017 14:42:20 +0530 Subject: [PATCH] Go challenge 3 (#427) * Add solution for max occurance challenge * Add test cases * Add README.md * Move code to correct location --- challenge_3/go/makernaren/README.md | 15 +++++++ challenge_3/go/makernaren/max_occurance.go | 38 +++++++++++++++++ .../go/makernaren/max_occurance_test.go | 41 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 challenge_3/go/makernaren/README.md create mode 100644 challenge_3/go/makernaren/max_occurance.go create mode 100644 challenge_3/go/makernaren/max_occurance_test.go diff --git a/challenge_3/go/makernaren/README.md b/challenge_3/go/makernaren/README.md new file mode 100644 index 000000000..4295f0924 --- /dev/null +++ b/challenge_3/go/makernaren/README.md @@ -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 +``` \ No newline at end of file diff --git a/challenge_3/go/makernaren/max_occurance.go b/challenge_3/go/makernaren/max_occurance.go new file mode 100644 index 000000000..ad433af8d --- /dev/null +++ b/challenge_3/go/makernaren/max_occurance.go @@ -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) +} diff --git a/challenge_3/go/makernaren/max_occurance_test.go b/challenge_3/go/makernaren/max_occurance_test.go new file mode 100644 index 000000000..303ee8f6e --- /dev/null +++ b/challenge_3/go/makernaren/max_occurance_test.go @@ -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) + } +}