aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPerry Liao2022-10-24 03:46:17 +0000
committerPerry Liao2022-10-24 03:46:17 +0000
commit1df988400399d3147514cf42a132141b7f5d8194 (patch)
tree419eaa88d36ace6fdd34f54656aadff0fd5eb5fc
parent5a918f5a59e0e3b3af1ef75b5251b9f7ae7a15e8 (diff)
make only 1 wc call
-rw-r--r--entries/perryliao/fib.groovy14
1 files changed, 8 insertions, 6 deletions
diff --git a/entries/perryliao/fib.groovy b/entries/perryliao/fib.groovy
index af1c4df..168913f 100644
--- a/entries/perryliao/fib.groovy
+++ b/entries/perryliao/fib.groovy
@@ -9,8 +9,8 @@ pipeline {
stage('Create Cache') {
steps {
script {
- if (!fileExists("fibbonacciCache")) {
- sh 'echo "0\n1" > fibbonacciCache'
+ if (!fileExists("fibCache")) {
+ sh 'echo "0\n1" > fibCache'
}
}
}
@@ -25,6 +25,7 @@ pipeline {
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()
}
}
-