diff options
author | braxtonhall | 2022-10-24 04:08:38 +0000 |
---|---|---|
committer | braxtonhall | 2022-10-24 04:08:38 +0000 |
commit | b29897383d848f93fc175577bf921fbe484badb1 (patch) | |
tree | f8417dab58ee12be1db1f069453e608da7edda34 /entries/perryliao | |
parent | 383b5fe9374f580ef2d1eb3a450cb8b755ad54d4 (diff) | |
parent | 7dbb91d4edce1aaf50078624b6e1833878b7ad48 (diff) |
Merge branch 'main' of github.com:braxtonhall/fib into main
Diffstat (limited to 'entries/perryliao')
-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() } } - |