74
10
Write a program or function that takes in a string only containing the characters ^
and v
(you can assume there will be no other characters). Read from left to right this string represents the sequence of mouse clicks a single user made while viewing a Stack Exchange question or answer for the first time.
Every ^
represents a click of the upvote button and every v
represents a click of the downvote button. (For working examples look slightly left.)
Assume that no voting limitations are in effect so all the clicks are registered correctly.
Print or return:
1
or+1
if the post ends up being upvoted.0
if the post ends up not being voted on. (-0
and+0
are not valid)-1
if the post ends up being downvoted.
Posts start with zero net votes from the user and the buttons change the net votes as follows:
Net Votes Before Button Pressed Net Votes After
1 ^ 0
1 v -1
0 ^ 1
0 v -1
-1 ^ 1
-1 v 0
The shortest code in bytes wins.
Test cases:
[empty string] -> 0
^^ -> 0
^v -> -1
^ -> 1
v -> -1
v^ -> 1
vv -> 0
^^^ -> 1
vvv -> -1
^^^^ -> 0
vvvv -> 0
^^^^^ -> 1
vvvvv -> -1
^^^^^^ -> 0
vvvvvv -> 0
^^v -> -1
^v^ -> 1
^vv -> 0
vv^ -> 1
v^v -> -1
v^^ -> 0
^vvv^^vv^vv^v^ -> 1
^vvv^^vv^vv^v^^ -> 0
^vvv^^vv^vv^v^^^ -> 1
^vvv^^vv^vv^v^^v -> -1
^vvv^^vv^vv^v^^vv -> 0
^vvv^^vv^vv^v^^vvv -> -1
^vvvvvvvvvvvv -> 0
^^vvvvvvvvvvvv -> 0
^^^vvvvvvvvvvvv -> 0
vvv^^^^^^^^^^^^ -> 0
vv^^^^^^^^^^^^ -> 0
v^^^^^^^^^^^^ -> 0
14What? no side voting? Geoborts and Seadrus are sad – Optimizer – 2015-11-09T07:48:23.900
26Dear Secret SE Developer: Congratulations on successfully duping your own community into making site improvements for you... ;) – thanby – 2015-11-10T09:58:46.973
1I've been starring at the example table for a while now and I still don't get the test cases. a post with a score of 1 gets up-voted and it then has a score of 0. And a post with a score of 0 gets up-voted to have a score of 1. And post with a score of -1 gets up-voted to have a score of 1. So the
^
character can cause a -1, +1 or +2 score change? Am I dense where? What's going on? – Brad – 2015-11-11T18:18:49.5674@Brad I suggest you try the actions with some actual post (e.g. this question itself). Upvoting a post you already upvoted undoes the upvote. Same with downvoting. – Calvin's Hobbies – 2015-11-11T18:23:17.680
1@Calvin'sHobbies oooooh I am dense :) It was the
No voting limitations
line that was throwing me for a loop. It all makes sense now! – Brad – 2015-11-11T18:29:56.1806I wonder what the real-time votes on this question was. I'm willing to bet a lot of people used this question as a test case. – MikeTheLiar – 2015-11-11T18:46:46.000