aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-10-01 10:18:49 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-10-01 13:31:05 -0700
commitf710b934d57ead36f286d46027b6f427f8efbd12 (patch)
tree38d5a149b6be87373f399cc5ec91d88bebd78be7
parent86de64b235e0caf3191db46a5885550ffbf93036 (diff)
downloadperlweeklychallenge-club-f710b934d57ead36f286d46027b6f427f8efbd12.tar.gz
perlweeklychallenge-club-f710b934d57ead36f286d46027b6f427f8efbd12.tar.bz2
perlweeklychallenge-club-f710b934d57ead36f286d46027b6f427f8efbd12.zip
Ch80/Task 2 (lua): improve algorithm
-rwxr-xr-xchallenge-080/tyler-wardhaugh/lua/ch-2.lua10
-rwxr-xr-xchallenge-080/tyler-wardhaugh/lua/test.lua1
2 files changed, 4 insertions, 7 deletions
diff --git a/challenge-080/tyler-wardhaugh/lua/ch-2.lua b/challenge-080/tyler-wardhaugh/lua/ch-2.lua
index 54d355d037..2378d1b18b 100755
--- a/challenge-080/tyler-wardhaugh/lua/ch-2.lua
+++ b/challenge-080/tyler-wardhaugh/lua/ch-2.lua
@@ -4,14 +4,10 @@ local t2 = {}
function t2.count_candies(coll)
local count = #coll
- local maybe_inc = function(i, j)
- if (coll[i] > coll[j]) then count = count + 1 end
- end
- -- sweep left-to-right
- for i=1,#coll-1 do maybe_inc(i, i+1) end
- -- sweep right-to-left
- for i=#coll,2,-1 do maybe_inc(i, i-1) end
+ for i=1,#coll-1 do
+ if (coll[i] ~= coll[i+1]) then count = count + 1 end
+ end
return count
end
diff --git a/challenge-080/tyler-wardhaugh/lua/test.lua b/challenge-080/tyler-wardhaugh/lua/test.lua
index 44d03f8844..44f80e3459 100755
--- a/challenge-080/tyler-wardhaugh/lua/test.lua
+++ b/challenge-080/tyler-wardhaugh/lua/test.lua
@@ -19,5 +19,6 @@ describe("Task 2, Count Candies", function()
it("produces correct results for the examples", function()
assert.are.same(t2.count_candies({1, 2, 2}), 4)
assert.are.same(t2.count_candies({1, 4, 3, 2}), 7)
+ assert.are.same(t2.count_candies({2, 1, 4, 3, 1, 2}), 11)
end)
end)