Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/main/java/io/zipcoder/Problem2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,72 @@
package io.zipcoder;

import java.util.ArrayList;
import java.util.Map;

public class Problem2 {

ArrayList<Integer> fibNums;

public Problem2() {
this.fibNums = new ArrayList<Integer>();
}

/**
* Use iteration to produce fibonnaci series and return in string format
* @param n upperBound for fibonnaci series
* @return the Fibonacci series as a string up to the value of n
*/
// public String fibonacciIteration(int n) {
// ArrayList<Integer> fibNums = new ArrayList<Integer>();
// int firstNum = 0;
// int secondNum = 1;
//
// while (true) {
// if (firstNum < n) {
// fibNums.add(firstNum);
// firstNum += secondNum;
// secondNum = firstNum - secondNum;
// } else {
// break;
// }
// }
// return fibNums.toString().replace("[", "").replace("]", "");
// }

public void fibonacciIteration(int n) {
int firstNum = 0;
int secondNum = 1;

while (true) {
if (firstNum < n) {
fibNums.add(firstNum);
firstNum += secondNum;
secondNum = firstNum - secondNum;
} else {
break;
}
}
}


/**
* Use recursion to produce fibonnaci series and return in string format
* @param n upperBound for fibonnaci series
* @return the Fibonacci series as a string up to the value of n
*/
public int fibonacciRecursion(int n) {
//I need a statment that will close the loop.
fibNums.add(n);
return fibonacciRecursion(n - 1) + fibonacciRecursion(n - 2);
}

@Override
public String toString(){
return fibNums.toString().replace("[", "").replace("]", "");
}
}





34 changes: 34 additions & 0 deletions src/test/java/io/zipcoder/Problem2Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
package io.zipcoder;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class Problem2Test {

Problem2 testProb;

@Before
public void setup(){
testProb = new Problem2();
}

@Test
public void testFibonacciIteration(){
int n = 150;
String expectedOutput = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144";
testProb.fibonacciIteration(n);
String actualOutput = testProb.toString();
Assert.assertEquals(expectedOutput, actualOutput);
}


@Test
public void testFibonacciRecursion(){
int n = 150;
String expectedOutput = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144";
testProb.fibonacciRecursion(n);
String actualOutput = testProb.toString();
Assert.assertEquals(expectedOutput, actualOutput);
}
}