-
-
Notifications
You must be signed in to change notification settings - Fork 655
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
nucleotide-count: add generator and update example #1014
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7ecac4c
implemented generator and new example for pig latin
ilmanzo 9670930
go fmt
ilmanzo 542adcc
small refactoring on @ferhatelmas suggestions
ilmanzo a3dffd7
Merge branch 'master' of https://github.com/exercism/go
ilmanzo 27a70ae
each test gets a description printed and PASS or FAIL
ilmanzo 8894405
Merge remote-tracking branch 'upstream/master'
ilmanzo e26219f
implemented test generator for nucleotide-count exercise (issue #605)
ilmanzo bed1582
added hints.md and changed .expected type in generator
ilmanzo de1947d
restored original nucleotide_count.go stub and adjusted expected type…
ilmanzo 4de30f4
small refactoring in example.go
ilmanzo e20414e
problem specification changed format; check for unexpected type in ge…
ilmanzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
|
||
"../../../gen" | ||
) | ||
|
||
func main() { | ||
t, err := template.New("").Parse(tmpl) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
var j js | ||
if err := gen.Gen("nucleotide-count", &j, t); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// The JSON structure we expect to be able to unmarshal into | ||
type js struct { | ||
exercise string | ||
version string | ||
Cases []struct { | ||
Description string | ||
Cases []OneCase | ||
} | ||
} | ||
|
||
// OneCase represents each test case | ||
type OneCase struct { | ||
Description string | ||
Property string | ||
Strand string | ||
Expected map[string]interface{} | ||
} | ||
|
||
// ErrorExpected returns true if an error should be raised | ||
func (c OneCase) ErrorExpected() bool { | ||
_, exists := c.Expected["error"] | ||
return exists | ||
} | ||
|
||
// SortedMapString collects key:values for a map in sorted order | ||
func (c OneCase) SortedMapString() string { | ||
strs := make([]string, 0, len(c.Expected)) | ||
for k, v := range c.Expected { | ||
switch t := v.(type) { | ||
case float64: | ||
strs = append(strs, `'`+k+`': `+strconv.FormatFloat(t, 'f', -1, 64)) | ||
default: | ||
} | ||
|
||
} | ||
sort.Strings(strs) | ||
return strings.Join(strs, ",") | ||
} | ||
|
||
// template applied to above data structure generates the Go test cases | ||
var tmpl = `package dna | ||
|
||
{{.Header}} | ||
|
||
{{range .J.Cases}}// {{.Description}} | ||
var testCases = []struct { | ||
description string | ||
strand string | ||
expected Histogram | ||
errorExpected bool | ||
}{ | ||
{{range .Cases}}{ | ||
description: {{printf "%q" .Description}}, | ||
strand: {{printf "%#v" .Strand}}, | ||
{{if .ErrorExpected}}errorExpected: true, | ||
{{else}}expected: Histogram{ {{.SortedMapString}} }, | ||
{{- end}} | ||
}, | ||
{{end}}{{end}} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
## Implementation | ||
|
||
You should define a custom type 'DNA' with a function 'Counts' that outputs two values: | ||
|
||
- a frequency count for the given DNA strand | ||
- an error (if there are invalid nucleotides) | ||
|
||
Which is a good type for a DNA strand ? | ||
|
||
Which is the best Go types to represent the output values ? | ||
|
||
Take a look at the test cases to get a hint about what could be the possible inputs. | ||
|
||
|
||
## note about the tests | ||
You may be wondering about the `cases_test.go` file. We explain it in the | ||
[leap exercise][leap-exercise-readme]. | ||
|
||
[leap-exercise-readme]: https://github.com/exercism/go/blob/master/exercises/leap/README.md | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package dna | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: bbdcaee nucleotide-count: fix wrong order introduced in #951 | ||
// Problem Specifications Version: 1.2.0 | ||
|
||
// count all nucleotides in a strand | ||
var testCases = []struct { | ||
description string | ||
strand string | ||
expected Histogram | ||
errorExpected bool | ||
}{ | ||
{ | ||
description: "empty strand", | ||
strand: "", | ||
expected: Histogram{'A': 0, 'C': 0, 'G': 0, 'T': 0}, | ||
}, | ||
{ | ||
description: "can count one nucleotide in single-character input", | ||
strand: "G", | ||
expected: Histogram{'A': 0, 'C': 0, 'G': 1, 'T': 0}, | ||
}, | ||
{ | ||
description: "strand with repeated nucleotide", | ||
strand: "GGGGGGG", | ||
expected: Histogram{'A': 0, 'C': 0, 'G': 7, 'T': 0}, | ||
}, | ||
{ | ||
description: "strand with multiple nucleotides", | ||
strand: "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC", | ||
expected: Histogram{'A': 20, 'C': 12, 'G': 17, 'T': 21}, | ||
}, | ||
{ | ||
description: "strand with invalid nucleotides", | ||
strand: "AGXXACT", | ||
errorExpected: true, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
rm
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.
Yeah,
That line is unnecessary. Can remove it.
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.
ok .. I made it a little more useful :)
in the meantime the problem specification json structure has changed, so I updated also the generator