Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

fib sums slightly improved (coding dojo)

View gist:4120496
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
import org.scalatest.matchers.ShouldMatchers
import org.junit.Test
 
//object Sums {
// def main(args: Array[String]) {
// for(i <- 1 to 20) { println(i + " is " + new Sums().zeckendorfRepresentationOf(i).mkString(" or "))}
// }
//}
 
class Sums extends ShouldMatchers {
@Test def givenANumber_shouldFindItFibonacciRepresentations() {
fibonacciRepresentationsOf(1) should equal(Seq("1"))
fibonacciRepresentationsOf(2) should equal(Seq("10"))
fibonacciRepresentationsOf(3) should equal(Seq("100", "11"))
fibonacciRepresentationsOf(4) should equal(Seq("101"))
fibonacciRepresentationsOf(5) should equal(Seq("1000", "110"))
fibonacciRepresentationsOf(6) should equal(Seq("1001", "111"))
fibonacciRepresentationsOf(7) should equal(Seq("1010"))
fibonacciRepresentationsOf(8) should equal(Seq("10000", "1100", "1011"))
fibonacciRepresentationsOf(9) should equal(Seq("10001", "1101"))
fibonacciRepresentationsOf(10) should equal(Seq("10010", "1110"))
fibonacciRepresentationsOf(11) should equal(Seq("10100", "10011", "1111"))
fibonacciRepresentationsOf(12) should equal(Seq("10101"))
fibonacciRepresentationsOf(13) should equal(Seq("100000", "11000", "10110"))
}
 
@Test def givenANumber_shouldFindItZeckendorfRepresentation() {
zeckendorfRepresentationOf(1) should equal("1")
zeckendorfRepresentationOf(2) should equal("10")
zeckendorfRepresentationOf(3) should equal("100")
zeckendorfRepresentationOf(4) should equal("101")
zeckendorfRepresentationOf(5) should equal("1000")
zeckendorfRepresentationOf(6) should equal("1001")
zeckendorfRepresentationOf(7) should equal("1010")
zeckendorfRepresentationOf(8) should equal("10000")
zeckendorfRepresentationOf(9) should equal("10001")
zeckendorfRepresentationOf(10) should equal("10010")
zeckendorfRepresentationOf(11) should equal("10100")
zeckendorfRepresentationOf(12) should equal("10101")
zeckendorfRepresentationOf(13) should equal("100000")
}
 
@Test def givenANumber_shouldFindFibonacciNumbersWithSumEqualToIt() {
findFibonacciRepresentationsOf(0) should equal(Seq())
findFibonacciRepresentationsOf(1) should equal(Seq(Seq(1)))
findFibonacciRepresentationsOf(5) should equal(Seq(Seq(5), Seq(2,3)))
}
 
@Test def givenFibonacciRepresentationOfANumeber_shouldConvertItToString() {
asString(Seq()) should equal("")
asString(Seq(1)) should equal("1")
asString(Seq(5)) should equal("1000")
asString(Seq(2, 3)) should equal("110")
}
 
@Test def shouldCalculateSequenceOfFibonacciNumbers_SkippingFirstZero() {
calculateFibonacciSequence(0) should equal(Seq())
calculateFibonacciSequence(1) should equal(Seq(1))
calculateFibonacciSequence(2) should equal(Seq(1, 2))
calculateFibonacciSequence(6) should equal(Seq(1, 2, 3, 5, 8, 13))
}
 
lazy val fibonacciSize = 6
lazy val fibonacci = calculateFibonacciSequence(fibonacciSize)
 
type NumberAsFibs = Seq[Int]
 
def zeckendorfRepresentationOf(n: Int): String = {
fibonacciRepresentationsOf(n).filterNot{ _.contains("11") }.head
}
 
def fibonacciRepresentationsOf(n: Int): Seq[String] = {
findFibonacciRepresentationsOf(n) map asString
}
 
private def findFibonacciRepresentationsOf(n: Int): Seq[NumberAsFibs] = {
for (
length <- 1 to fibonacciSize;
combination <- fibonacci.combinations(length) if (combination.sum == n)
) yield combination
}
 
private def asString(numberAsFibs: NumberAsFibs) = {
val chars = for (i <- fibonacci) yield if (numberAsFibs.contains(i)) '1' else '0'
chars.reverse.dropWhile(_ == '0').mkString
}
 
private def calculateFibonacciSequence(count: Int): Seq[Int] = {
def f(count: Int, current: Int, previous: Int): List[Int] = count match {
case 0 => List()
case _ => {
val newCurrent = current + previous
val newLast = current
current :: f(count - 1, newCurrent, newLast)
}
}
f(count, 1, 1)
}
 
private def calculateFibonacciStream: Stream[Int] = {
def f(current: Int, previous: Int): Stream[Int] =
current #:: f(current + previous, current)
f(1, 1)
}
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.