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

Go challenge 3 #427

merged 4 commits into from
Jan 24, 2017

Conversation

DudeWhoCode
Copy link
Contributor

No description provided.

@Remillardj
Copy link
Contributor

Why do you have two files;
max_occurance.go & max_occurance_test.go?

@DudeWhoCode
Copy link
Contributor Author

DudeWhoCode commented Jan 19, 2017

One is the source code, other is for testing it. I guess thats the standard format to test your
golang code. yourCode_test.go

Copy link
Collaborator

@erocs erocs left a 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{}) {
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.

@@ -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.

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.

}
// Return when we find the max occurance.
max := 0
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.

@erocs erocs merged commit db0e5f3 into YearOfProgramming:master Jan 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants