diff options
-rw-r--r-- | entries/perryliao/fib.groovy | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/entries/perryliao/fib.groovy b/entries/perryliao/fib.groovy index af1c4df..d625d68 100644 --- a/entries/perryliao/fib.groovy +++ b/entries/perryliao/fib.groovy @@ -6,25 +6,26 @@ pipeline { } stages { - stage('Create Cache') { + stage('Validate Parameter') { steps { script { - if (!fileExists("fibbonacciCache")) { - sh 'echo "0\n1" > fibbonacciCache' - } + assert params.num.isInteger() } } } - stage('Validate Parameter') { + stage('Create Cache') { steps { script { - assert params.num.isInteger() + if (!fileExists("fibCache")) { + sh 'echo "0\n1" > fibCache' + } } } } stage('Get Fibbonacci') { steps { script { + nComputed = sh(returnStdout: true, script: "wc -l < fibCache").trim().toInteger() result = fib(params.num.toInteger()) println("The result is F(${params.num}) = ${result}") } @@ -33,16 +34,17 @@ pipeline { } } -int fib(int n, cache = 'fibbonacciCache') { - nComputed = sh(returnStdout: true, script: "wc -l < ${cache}").trim().toInteger() +def nComputed + +int fib(int n, cache = 'fibCache') { if (n >= nComputed) { // Need to do more recursion result = fib(n - 2, cache) + fib(n - 1, cache) + nComputed++ sh "echo '${result}' >> ${cache}" return result } else { // Already have what we need, so read from cache - return sh(returnStdout: true, script: "sed -n '${n + 1}p' ${cache}").trim().toInteger() + return sh(returnStdout: true, script: "sed -n '${n + 1}p' ${cache}").toInteger() } } - |