From 9bc59bb95f07163a54eea47123fbfaab089b4bb2 Mon Sep 17 00:00:00 2001 From: graham Date: Mon, 27 Nov 2017 11:18:55 -0500 Subject: [PATCH] not finished --- src/main/java/io/zipcoder/Problem2.java | 31 +++++++++++++++++++++ src/test/java/io/zipcoder/Problem2Test.java | 21 ++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/main/java/io/zipcoder/Problem2.java b/src/main/java/io/zipcoder/Problem2.java index e0af6a0..a39d987 100644 --- a/src/main/java/io/zipcoder/Problem2.java +++ b/src/main/java/io/zipcoder/Problem2.java @@ -1,4 +1,35 @@ package io.zipcoder; public class Problem2 { + + Problem2() { + } + + public String iterationSolution(int n) { + StringBuilder solution = new StringBuilder(); + + int x = 0, y = 1, z = 1; + + for (int i = 0; x < n; i++) { + solution.append(x + ", "); + x = y; + y = z; + z = x + y; + } + + return solution.toString(); +} + + + //Does not work, have to convert to last addition to solutionString to Integer and append I guess + private static String recursionSolution(int n, Integer x, String solution) { + String solutionString = solution + ", "; + String nResult; + if (n == 1) + return solutionString = "1"; + else + solution = n * recursionSolution(n-1,solutionString); + return solutionString; + return null; + } } diff --git a/src/test/java/io/zipcoder/Problem2Test.java b/src/test/java/io/zipcoder/Problem2Test.java index fa9ffa5..3ed03e7 100644 --- a/src/test/java/io/zipcoder/Problem2Test.java +++ b/src/test/java/io/zipcoder/Problem2Test.java @@ -1,4 +1,25 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem2Test { + + Problem2 problem2 = new Problem2(); + + @Test + public void iterationSolution(){ + String expected = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144"; + String actual = problem2.iterationSolution(150); + + Assert.assertEquals(expected,actual); + } + + @Test + public void recursiveSolutionTest(){ + String expected = "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144"; + String actual = problem2.iterationSolution(150); + + Assert.assertEquals(expected,actual); + } }