diff options
author | Andinus <andinus@nand.sh> | 2021-08-11 15:26:15 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-08-11 15:26:15 +0530 |
commit | 321825828ac918bad28d0597a8616c6dc9802c3c (patch) | |
tree | 0b8e9cb1012197750eb58e972736319b2a6abac2 /go/two-fer | |
parent | 2979ef790ac5b8f58495e0dd08cafd6a3a2e30a5 (diff) | |
download | exercism-321825828ac918bad28d0597a8616c6dc9802c3c.tar.gz |
Add solved exercises
Diffstat (limited to 'go/two-fer')
-rw-r--r-- | go/two-fer/README.md | 55 | ||||
-rw-r--r-- | go/two-fer/example_two_fer_test.go | 20 | ||||
-rw-r--r-- | go/two-fer/go.mod | 3 | ||||
-rw-r--r-- | go/two-fer/two_fer.go | 13 | ||||
-rw-r--r-- | go/two-fer/two_fer_test.go | 31 |
5 files changed, 122 insertions, 0 deletions
diff --git a/go/two-fer/README.md b/go/two-fer/README.md new file mode 100644 index 0000000..4fd3018 --- /dev/null +++ b/go/two-fer/README.md @@ -0,0 +1,55 @@ +# Two Fer + +`Two-fer` or `2-fer` is short for two for one. One for you and one for me. + +Given a name, return a string with the message: + +```text +One for X, one for me. +``` + +Where X is the given name. + +However, if the name is missing, return the string: + +```text +One for you, one for me. +``` + +Here are some examples: + +|Name |String to return +|:-------|:------------------ +|Alice |One for Alice, one for me. +|Bob |One for Bob, one for me. +| |One for you, one for me. +|Zaphod |One for Zaphod, one for me. + +## Coding the solution + +Look for a stub file having the name two_fer.go +and place your solution code in that file. + +## Running the tests + +To run the tests run the command `go test` from within the exercise directory. + +If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem` +flags: + + go test -v --bench . --benchmem + +Keep in mind that each reviewer will run benchmarks on a different machine, with +different specs, so the results from these benchmark tests may vary. + +## Further information + +For more detailed information about the Go track, including how to get help if +you're having trouble, please visit the exercism.io [Go language page](http://exercism.io/languages/go/resources). + +## Source + +[https://github.com/exercism/problem-specifications/issues/757](https://github.com/exercism/problem-specifications/issues/757) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/go/two-fer/example_two_fer_test.go b/go/two-fer/example_two_fer_test.go new file mode 100644 index 0000000..fe0f023 --- /dev/null +++ b/go/two-fer/example_two_fer_test.go @@ -0,0 +1,20 @@ +package twofer + +import "fmt" + +// ExampleShareWith() is an Example function. Examples are testable snippets of +// Go code that are used for documenting and verifying the package API. +// They may be present in some exercises to demonstrate the expected use of the +// exercise API and can be run as part of a package's test suite. +// +// When an Example test is run the data that is written to standard output is +// compared to the data that comes after the "Output: " comment. +// +// Below the result of ShareWith() is passed to standard output +// using fmt.Println, and this is compared against the expected output. +// If they are equal, the test passes. +func ExampleShareWith() { + h := ShareWith("") + fmt.Println(h) + // Output: One for you, one for me. +} diff --git a/go/two-fer/go.mod b/go/two-fer/go.mod new file mode 100644 index 0000000..3eebc29 --- /dev/null +++ b/go/two-fer/go.mod @@ -0,0 +1,3 @@ +module twofer + +go 1.13 diff --git a/go/two-fer/two_fer.go b/go/two-fer/two_fer.go new file mode 100644 index 0000000..3497a4c --- /dev/null +++ b/go/two-fer/two_fer.go @@ -0,0 +1,13 @@ +// twofer implements ShareWith. +package twofer + +import "fmt" + +// ShareWith returns "One for `name', one for me.". Default name is +// "you". +func ShareWith(name string) string { + if len(name) == 0 { + name = "you" + } + return fmt.Sprintf("One for %s, one for me.", name) +} diff --git a/go/two-fer/two_fer_test.go b/go/two-fer/two_fer_test.go new file mode 100644 index 0000000..f4ce158 --- /dev/null +++ b/go/two-fer/two_fer_test.go @@ -0,0 +1,31 @@ +package twofer + +import "testing" + +// Define a function ShareWith(string) string. + +var tests = []struct { + name, expected string +}{ + {"", "One for you, one for me."}, + {"Alice", "One for Alice, one for me."}, + {"Bob", "One for Bob, one for me."}, +} + +func TestShareWith(t *testing.T) { + for _, test := range tests { + if observed := ShareWith(test.name); observed != test.expected { + t.Fatalf("ShareWith(%s) = \"%v\", want \"%v\"", test.name, observed, test.expected) + } + } +} + +func BenchmarkShareWith(b *testing.B) { + for i := 0; i < b.N; i++ { + + for _, test := range tests { + ShareWith(test.name) + } + + } +} |