-
Notifications
You must be signed in to change notification settings - Fork 162
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
Go challenge 3 #427
Conversation
Why do you have two files; |
One is the source code, other is for testing it. I guess thats the standard format to test your |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Code Correctness
- Code Style
- Code Documentation
- Path Structure
- Merge Pull Request
|
||
// 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{}) { |
There was a problem hiding this comment.
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.
@@ -0,0 +1,38 @@ | |||
// Take an array of mixed data types and find the element that occurs | |||
// most number of times. | |||
package main |
There was a problem hiding this comment.
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.
occurances[key]++ | ||
} | ||
// Return when we find the max occurance. | ||
max := 0 |
There was a problem hiding this comment.
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.
} | ||
// Return when we find the max occurance. | ||
max := 0 | ||
kv_pair := make([]interface{}, 2) |
There was a problem hiding this comment.
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.
No description provided.