Browse Source

initial add of the go track progress so far

solved are hello-world and two-fer
working on hamming
master
Blaine Motsinger 1 year ago
commit
542e9a9bf7

+ 67
- 0
go/hamming/README.md View File

@@ -0,0 +1,67 @@
# Hamming

Calculate the Hamming difference between two DNA strands.

A mutation is simply a mistake that occurs during the creation or
copying of a nucleic acid, in particular DNA. Because nucleic acids are
vital to cellular functions, mutations tend to cause a ripple effect
throughout the cell. Although mutations are technically mistakes, a very
rare mutation may equip the cell with a beneficial attribute. In fact,
the macro effects of evolution are attributable by the accumulated
result of beneficial microscopic mutations over many generations.

The simplest and most common type of nucleic acid mutation is a point
mutation, which replaces one base with another at a single nucleotide.

By counting the number of differences between two homologous DNA strands
taken from different genomes with a common ancestor, we get a measure of
the minimum number of point mutations that could have occurred on the
evolutionary path between the two strands.

This is called the 'Hamming distance'.

It is found by comparing two DNA strands and counting how many of the
nucleotides are different from their equivalent in the other string.

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^ ^ ^ ^^

The Hamming distance between these two DNA strands is 7.

# Implementation notes

The Hamming distance is only defined for sequences of equal length, so
an attempt to calculate it between sequences of different lengths should
not work. The general handling of this situation (e.g., raising an
exception vs returning a special value) may differ between languages.

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


## 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

The Calculating Point Mutations problem at Rosalind [http://rosalind.info/problems/hamm/](http://rosalind.info/problems/hamm/)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

+ 87
- 0
go/hamming/cases_test.go View File

@@ -0,0 +1,87 @@
package hamming

// Source: exercism/problem-specifications
// Commit: b5d154b hamming: move inputs (strand1, strand2) to input object
// Problem Specifications Version: 2.1.0

var testCases = []struct {
s1 string
s2 string
want int
}{
{ // empty strands
"",
"",
0,
},
{ // identical strands
"A",
"A",
0,
},
{ // long identical strands
"GGACTGA",
"GGACTGA",
0,
},
{ // complete distance in single nucleotide strands
"A",
"G",
1,
},
{ // complete distance in small strands
"AG",
"CT",
2,
},
{ // small distance in small strands
"AT",
"CT",
1,
},
{ // small distance
"GGACG",
"GGTCG",
1,
},
{ // small distance in long strands
"ACCAGGG",
"ACTATGG",
2,
},
{ // non-unique character in first strand
"AAG",
"AAA",
1,
},
{ // non-unique character in second strand
"AAA",
"AAG",
1,
},
{ // same nucleotides in different positions
"TAG",
"GAT",
2,
},
{ // large distance
"GATACA",
"GCATAA",
4,
},
{ // large distance in off-by-one strand
"GGACGGATTCTG",
"AGGACGGATTCT",
9,
},
{ // disallow first strand longer
"AATG",
"AAA",
-1,
},
{ // disallow second strand longer
"ATA",
"AGTG",
-1,
},
}

+ 23
- 0
go/hamming/hamming.go View File

@@ -0,0 +1,23 @@
// Package for the hamming exercise for the exercism.io go track
package hamming

// Distance calculates the number of different nucleotides in their positional equivalent between two strings
func Distance(a, b string) (int, error) {

// the Hamming distance is only defined for sequences of equal length
if len(a) != len(b) {
// since 0 difference would indicate an exact string,
// -1 indicates different length strings
return -1
}

var totalDifferences int

for i := 0; i < len(a); i++ {
if a[i] != b[i] {
totalDifferences++
}
}

return totalDifferences
}

+ 39
- 0
go/hamming/hamming_test.go View File

@@ -0,0 +1,39 @@
package hamming

import "testing"

func TestHamming(t *testing.T) {
for _, tc := range testCases {
got, err := Distance(tc.s1, tc.s2)
if tc.want < 0 {
// check if err is of error type
var _ error = err

// we expect error
if err == nil {
t.Fatalf("Distance(%q, %q). error is nil.",
tc.s1, tc.s2)
}
} else {
if got != tc.want {
t.Fatalf("Distance(%q, %q) = %d, want %d.",
tc.s1, tc.s2, got, tc.want)
}

// we do not expect error
if err != nil {
t.Fatalf("Distance(%q, %q) returned error: %v when expecting none.",
tc.s1, tc.s2, err)
}
}
}
}

func BenchmarkHamming(b *testing.B) {
// bench combined time to run through all test cases
for i := 0; i < b.N; i++ {
for _, tc := range testCases {
Distance(tc.s1, tc.s2)
}
}
}

+ 39
- 0
go/hello-world/README.md View File

@@ -0,0 +1,39 @@
# Hello World

The classical introductory exercise. Just say "Hello, World!".

["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
the traditional first program for beginning programming in a new language
or environment.

The objectives are simple:

- Write a function that returns the string "Hello, World!".
- Run the test suite and make sure that it succeeds.
- Submit your solution and check it at the website.

If everything goes well, you will be ready to fetch your first real exercise.

## 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

This is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

+ 37
- 0
go/hello-world/hello_test.go View File

@@ -0,0 +1,37 @@
package greeting

import "testing"

// Define a function named HelloWorld that takes no arguments,
// and returns a string.
// In other words, define a function with the following signature:
// HelloWorld() string

func TestHelloWorld(t *testing.T) {
expected := "Hello, World!"
if observed := HelloWorld(); observed != expected {
t.Fatalf("HelloWorld() = %v, want %v", observed, expected)
}
}

// BenchmarkHelloWorld() is a benchmarking function. These functions follow the
// form `func BenchmarkXxx(*testing.B)` and can be used to test the performance
// of your implementation. They may not be present in every exercise, but when
// they are you can run them by including the `-bench` flag with the `go test`
// command, like so: `go test -v --bench . --benchmem`
//
// You will see output similar to the following:
//
// BenchmarkHelloWorld 2000000000 0.46 ns/op
//
// This means that the loop ran 2000000000 times at a speed of 0.46 ns per loop.
//
// While benchmarking can be useful to compare different iterations of the same
// exercise, keep in mind that others will run the same benchmarks on different
// machines, with different specs, so the results from these benchmark tests may
// vary.
func BenchmarkHelloWorld(b *testing.B) {
for i := 0; i < b.N; i++ {
HelloWorld()
}
}

+ 7
- 0
go/hello-world/hello_world.go View File

@@ -0,0 +1,7 @@
// hello_world exercise for the exercism.io go track
package greeting

// returns the required string to the caller
func HelloWorld() string {
return "Hello, World!"
}

+ 37
- 0
go/two-fer/README.md View File

@@ -0,0 +1,37 @@
# Two Fer

`Two-fer` or `2-fer` is short for two for one. One for you and one for me.

```text
"One for X, one for me."
```

When X is a name or "you".

If the given name is "Alice", the result should be "One for Alice, one for me."
If no name is given, the result should be "One for you, one for me."


## 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://en.wikipedia.org/wiki/Two-fer](https://en.wikipedia.org/wiki/Two-fer)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

+ 20
- 0
go/two-fer/example_two_fer_test.go View File

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

+ 14
- 0
go/two-fer/two_fer.go View File

@@ -0,0 +1,14 @@
// twofer exercise for the exercism.io go track
package twofer

import "fmt"

// formats and returns the required string to the caller
// if no name is specified, defaults to "you"
func ShareWith(name string) string {
if name == "" {
name = "you"
}

return fmt.Sprintf("One for %s, one for me.", name)
}

+ 31
- 0
go/two-fer/two_fer_test.go View File

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

}
}

Loading…
Cancel
Save