Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go challenge 3 #427

Merged
merged 4 commits into from
Jan 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put the challenge functionality and unit tests in a challenge specific package and break out main() into its own file / package.


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{}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A slice of empty interfaces is unwieldy. Sure, it's Go's iffy "generics", but if you have a typed slice, it has to be copied into this new generic slice before being passed in. You would likely end up just writing a FindMajorityTYPE for each type you wanted this functionality for or you'd create a new interface that better supported what this needs to work.

For this problem, it's ok, as you create the slice of empty interfaces as input and it's not used for anything else.

// 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can calculate this on the fly in the loop where you calculate occurrences. Just track what the current item / max count is and update that if the element you just incremented is larger.

kv_pair := make([]interface{}, 2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a pair to track is odd. Just create two separate local variables each with their own descriptive name.

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)
}
}