Skip to content
Open

done #28

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
53 changes: 53 additions & 0 deletions src/main/java/io/zipcoder/Problem2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
package io.zipcoder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Problem2 {

String sequence = "";
ArrayList<Integer> fibbonaciS = new ArrayList<Integer>();
static List<Integer> fibList;

public Problem2(){
fibbonaciS.add(0);
fibbonaciS.add(1);
}

public String fibbonaciIteration(int n){
String sequence = "";
int endNumber = 1;

while(endNumber < n){
int twoBeforeEnd = fibbonaciS.get(fibbonaciS.size() - 2);
int oneBeforeEnd = fibbonaciS.get(fibbonaciS.size() - 1);
endNumber = twoBeforeEnd + oneBeforeEnd;
fibbonaciS.add(endNumber);
}
for(int j = 0; j < fibbonaciS.size()-1; j++){
sequence += fibbonaciS.get(j) + ", ";
}
return sequence.substring(0, sequence.length()-2);
}
//currentIndex = (CurrentIndex - 1) + (currentIndex - 2)

public String fibRecursion(int n, List<Integer> fibList){
if(fibList.size() == 0){
fibList.add(0);
fibList.add(1);
}
int endNum = fibList.get(fibList.size()-1) + fibList.get(fibList.size()-2);
if (endNum > n){
String sequence1 = "";
for (int i = 0; i < fibList.size();i++) {

sequence1 += fibList.get(i) + ", ";
}
return sequence1.substring(0, sequence1.length()-2);

}
else{
fibList.add(endNum);
return fibRecursion(n, fibList);
}


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

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

import java.util.ArrayList;
import java.util.List;

public class Problem2Test {

Problem2 test;

@Before
public void setUp(){
test = new Problem2();
}

@Test
public void fibbonaciTest(){
String fibbonaci = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144";
String actual = test.fibbonaciIteration(150);

Assert.assertEquals(fibbonaci, actual);
}
@Test
public void fibbonaciTest2(){
String fib = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393";
String actual = test.fibbonaciIteration(150000);
Assert.assertEquals(fib, actual);
}
@Test
public void fibRecursionTest(){
String fibbonaci = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144";
List<Integer> fibList = new ArrayList<Integer>();
String actual = test.fibRecursion(150, fibList);
Assert.assertEquals(fibbonaci, actual);
}
}