Skip to content

Commit

Permalink
Merge branch 'master' into sirigithublab3
Browse files Browse the repository at this point in the history
  • Loading branch information
sirishavadrevu committed Jun 30, 2019
2 parents d2781d5 + c3bd20f commit b8f7056
Show file tree
Hide file tree
Showing 64 changed files with 3,128 additions and 16 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ linters:
enable-all: true
disable:
- gochecknoglobals
- dupl
42 changes: 42 additions & 0 deletions 01_fib/alfredxiao/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"io"
"os"
)

var out io.Writer = os.Stdout

func fibSeries(n int) []int {
if n >= 93 || n <= -93 {
panic("Numbers beyond 93 or -93 are not supported!")
}

sign := 1
if n < 0 {
sign = -1
}

series := make([]int, n*sign+1)
if n == 0 {
series[0] = 0
} else {
series[0], series[1] = 0, 1
for i := 2; i <= n*sign; i++ {
series[i] = series[i-2] + series[i-1]*sign
}
}
return series
}

func fib(n int) {
series := fibSeries(n)
for _, fn := range series {
fmt.Fprintln(out, fn)
}
}

func main() {
fib(7)
}
45 changes: 45 additions & 0 deletions 01_fib/alfredxiao/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFibEdgeCases(t *testing.T) {
assert.Equal(t, []int{0}, fibSeries(0))
assert.Equal(t, []int{0, 1}, fibSeries(1))
assert.Equal(t, []int{0, 1}, fibSeries(-1))
assert.Equal(t, []int{0, 1, 1}, fibSeries(2))
assert.Equal(t, []int{0, 1, -1}, fibSeries(-2))
}

func TestFibPositive(t *testing.T) {
assert.Equal(t, []int{0, 1, 1, 2, 3, 5, 8, 13}, fibSeries(7))
}

func TestFibNegative(t *testing.T) {
assert.Equal(t, []int{0, 1, -1, 2, -3, 5, -8, 13}, fibSeries(-7))
}

func TestFibSeriesPanic(t *testing.T) {
assert.Panics(t, func() { fibSeries(93) }, "Panic when number of range")
assert.Panics(t, func() { fibSeries(94) }, "Panic when number of range")
assert.Panics(t, func() { fibSeries(-93) }, "Panic when number of range")
assert.Panics(t, func() { fibSeries(-94) }, "Panic when number of range")
}

func TestFib(t *testing.T) {
var buf bytes.Buffer
out = &buf
fib(2)
assert.Equal(t, "0\n1\n1\n", buf.String())
}

func TestMain(t *testing.T) {
var buf bytes.Buffer
out = &buf
main()
assert.Contains(t, buf.String(), "3\n5\n8")
}
67 changes: 67 additions & 0 deletions 01_fib/cuminandpaprika/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"fmt"
"io"
"os"
)

var out io.Writer = os.Stdout

func main() {
fib(7)
}

// Print n number of fibonnaci sequence
// 1, 1, 2, 3, 5, 8, 13
// Note: we take the absolute value of n to ensure our loop works.
func fib(n int64) {
var i int64
if isPositive(n) {
for ; i < n; i++ {
fmt.Fprintln(out, calcPositiveFib(i))
}
return
}
if n == 0 {
fmt.Fprintln(out, calcZeroFib(i))
}

for ; i > n; i-- {
fmt.Fprintln(out, calcNegativeFib(i))
}

}

func calcZeroFib(n int64) int64 {
return 0
}

func calcPositiveFib(n int64) int64 {
if n == 0 {
return calcZeroFib(0)
}
if n == 1 {
return 1
}

return calcPositiveFib(n-2) + calcPositiveFib(n-1)
}

func calcNegativeFib(n int64) int64 {
// We convert the negative to positive so it calculates correctly with calcPositiveFib
n = (-1 * n)
if isEven(n) {
return ((-1) * calcPositiveFib(n))
}
return calcPositiveFib(n)

}

func isEven(n int64) bool {
return ((n & 1) == 0)
}

func isPositive(n int64) bool {
return (n > 0)
}
108 changes: 108 additions & 0 deletions 01_fib/cuminandpaprika/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"bytes"
"strconv"
"testing"
)

func TestMainCallsFib(t *testing.T) {
// Arrange
var buf bytes.Buffer
out = &buf

// Act
main()

// Assert
expected := strconv.Quote("0\n1\n1\n2\n3\n5\n8\n")
actual := strconv.Quote(buf.String())

if expected != actual {
t.Errorf("Unexpected behavior with test input of 7")
t.Errorf("expected: %s\nactual:%s", expected, actual)
}

}

func TestFibInputPositive(t *testing.T) {
// Arrange
var buf bytes.Buffer
out = &buf

// Act
fib(7)

// Assert
expected := strconv.Quote("0\n1\n1\n2\n3\n5\n8\n")
actual := strconv.Quote(buf.String())

if expected != actual {
t.Errorf("unexpected behavior with test input of 7")
t.Errorf("expected: %s\nactual:%s", expected, actual)
}
}

func TestFibInputNegative(t *testing.T) {
// Arrange
var buf bytes.Buffer
out = &buf

// Act
fib(-7)

// Assert
expected := strconv.Quote("0\n1\n-1\n2\n-3\n5\n-8\n")
actual := strconv.Quote(buf.String())

if expected != actual {
t.Errorf("unexpected output in main()")
t.Errorf("expected: " + expected + "\nactual: " + actual)
}
}

func TestFibInputZero(t *testing.T) {
// Arrange
var buf bytes.Buffer
out = &buf

// Act
fib(0)

// Assert
expected := strconv.Quote("0\n")
actual := strconv.Quote(buf.String())

if expected != actual {
t.Errorf("unexpected output in main()")
t.Errorf("expected: " + expected + "\nactual: " + actual)
}
}

func TestCalcPositiveFib(t *testing.T) {
var fibResult = calcPositiveFib(4)
var expectedResult int64 = 3
if fibResult != expectedResult {
t.Errorf("wrong output from calc fib")
t.Errorf("expected: %d\nactual:%d", expectedResult, fibResult)
}
}

func TestCalcNegativeFib(t *testing.T) {
var fibResult = calcNegativeFib(-4)
var expectedResult int64 = -3
if fibResult != expectedResult {
t.Errorf("wrong output from calc fib")
t.Errorf("expected: %d\nactual:%d", expectedResult, fibResult)
}
}

func TestCalcZeroFib(t *testing.T) {
var expectedResult int64

var fibResult = calcPositiveFib(0)
if fibResult != expectedResult {
t.Errorf("wrong output from calc fib")
t.Errorf("expected: %d\nactual:%d", expectedResult, fibResult)
}
}
39 changes: 39 additions & 0 deletions 01_fib/joel00wood/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"math"
)

func fibCalc(n float64) float64 {
switch n {
case 0, 1:
return n
default:
return fibCalc(n-1) + fibCalc(n-2)
}
}

func fibN(n float64) float64 {
if n < 0 {
var i float64
n := math.Abs(n)
i = math.Pow(-1, n+1)
return fibCalc(n) * i
}
return fibCalc(n)
}

func fib(n int) {
for i := 0.0; i <= math.Abs(float64(n)); i++ {
if n < 0 && i > 0 {
fmt.Println(fibN(i * -1))
} else {
fmt.Println(fibN(i))
}
}
}

func main() {
fib(7)
}
86 changes: 86 additions & 0 deletions 01_fib/joel00wood/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"testing"

output "github.com/joel00wood/test-helpers/capture"
)

func TestFibCalc(t *testing.T) {
testCases := map[string]struct {
input, expected float64
}{
"input 20": {20, 6765},
"input 3": {3, 2},
"input 2": {2, 1},
"input 1": {1, 1},
"input 0": {0, 0},
}

for name, test := range testCases {
actual := fibCalc(test.input)
if test.expected != actual {
t.Errorf("tests[%s] fibCalc(%f) wrong. expected=%f, got=%f",
name, test.input, test.expected, actual)
}
}
}

func TestFibN(t *testing.T) {
testCases := map[string]struct {
input, expected float64
}{
"input 20": {20, 6765},
"input 3": {3, 2},
"input 2": {2, 1},
"input 1": {1, 1},
"input 0": {0, 0},
"input -1": {-1, 1},
"input -2": {-2, -1},
"input -3": {-3, 2},
"input -20": {-20, -6765},
}

for name, test := range testCases {
actual := fibN(test.input)
if test.expected != actual {
t.Errorf("tests[%s] fibN(%f) wrong. expected=%f, got=%f",
name, test.input, test.expected, actual)
}
}
}

func TestFib(t *testing.T) {
testCases := map[string]struct {
input int
expected string
}{
"input 7": {7, "0\n1\n1\n2\n3\n5\n8\n13\n"},
"input 3": {3, "0\n1\n1\n2\n"},
"input 2": {2, "0\n1\n1\n"},
"input 1": {1, "0\n1\n"},
"input 0": {0, "0\n"},
"input -1": {-1, "0\n1\n"},
"input -2": {-2, "0\n1\n-1\n"},
"input -3": {-3, "0\n1\n-1\n2\n"},
"input -7": {-7, "0\n1\n-1\n2\n-3\n5\n-8\n13\n"},
}
for name, test := range testCases {
input := test.input
actual := output.CaptureOutput(func() { fib(input) })
if test.expected != actual {
t.Errorf("tests[%s] fib(%d) wrong. expected=%q, got=%q",
name, test.input, test.expected, actual)
}
}
}

// test main() which is essentially fib(7)
func TestMain(t *testing.T) {
expected := "0\n1\n1\n2\n3\n5\n8\n13\n"
actual := output.CaptureOutput(func() { main() })
if expected != actual {
t.Errorf("Unexpected response for input main(){fib(7)}\nExpected: %q\nActual: %q",
expected, actual)
}
}
Loading

0 comments on commit b8f7056

Please sign in to comment.