75
\$\begingroup\$

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
\$\endgroup\$
6
  • 15
    \$\begingroup\$ What? no side voting? Geoborts and Seadrus are sad \$\endgroup\$
    – Optimizer
    Nov 9, 2015 at 7:48
  • 30
    \$\begingroup\$ Dear Secret SE Developer: Congratulations on successfully duping your own community into making site improvements for you... ;) \$\endgroup\$
    – thanby
    Nov 10, 2015 at 9:58
  • 1
    \$\begingroup\$ I'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? \$\endgroup\$
    – Brad
    Nov 11, 2015 at 18:18
  • 4
    \$\begingroup\$ @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. \$\endgroup\$ Nov 11, 2015 at 18:23
  • 8
    \$\begingroup\$ I 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. \$\endgroup\$ Nov 11, 2015 at 18:46

39 Answers 39

1
2
1
\$\begingroup\$

C (107 bytes)

#include<stdio.h>
int c,t;main(){while((c=getchar())-'\n')(c=='^')?++t:((c=='v')?--t:0);printf("%d\n",t);}
\$\endgroup\$
1
\$\begingroup\$

Braingolf, 17 bytes

VR{.#veM|}lvlMR-s

Try it online!

Thought I could get it shorter by subtracting 9 from the charcodes of each character, then using J to change vowels to 1 and non-vowels to 0, but it turned out to be the same length

\$\endgroup\$
1
\$\begingroup\$

C++, 90 87 bytes

int f(char* c){int r=1,T[2][3]={{2,2,1},{1,0,0}};while(*c)r=T[*c++==94][r];return r-1;}

Ungolfed:

int f( char* c )
{
     // Lookup table created from Golfing description. Need to shift values up by 1
     // because will be using them to index table later
     int lookup_table[ 2 ][ 3 ] = {{2, 2, 1}, {1, 0, 0}};
     int result = 1;                 // empty string result: 1
     while ( *c )                    // check if end of string
     {
          int  row    = *c - '^' ? 1 : 0; // if char is '^' search in second row else in first
          auto column = result;           // column to look for is current result
          result      = lookup_table[ row ][ column ];
          c++;
      }
      return result - 1;                  // make expected result
 }

hehe finally got to write c++ in C++ ;-)

Edit:

  1. Exchanged tenary row calculation to simple boolean expression.
\$\endgroup\$
2
  • \$\begingroup\$ Suggest (char*c) and return~-r instead of return r-1 \$\endgroup\$
    – ceilingcat
    May 8, 2019 at 17:25
  • \$\begingroup\$ @ceilingcat Great! Thanks. \$\endgroup\$ May 8, 2019 at 17:40
1
\$\begingroup\$

Turing Machine Code, 115 bytes

init:r
accept:e
r,^
^,_,>
r,v
v,_,>
r,_
e,0,-
^,^
r,_,>
^,v
r,v,-
^,_
e,1,-
v,^
r,^,-
v,v
r,_,>
v,_
w,-,>
w,_
e,1,-

Try it here

Ungolfed:

//-------CONFIGURATION
init: startread
accept: end

//startread
startread,^
read^,_,>

startread,v
readv,_,>

startread,_
end,0,-

//read^
read^,^
startread,_,>

read^,v
startread,v,-

read^,_
end,1,-

//readv
readv,^
startread,^,-

readv,v
startread,_,>

readv,_
write-,-,>

write-,_
end,1,-

The approach I used was to compare every two adjacent characters and either remove them if they canceled, or to remove just the first if the second one were to overwrite the first. An extra state is needed so that -1 can be written, because this simulator does not support multi-symbol cells.

\$\endgroup\$
0
\$\begingroup\$

Japt, 17 bytes

¬®c nL gÃrÈ-Y©Y}0

Try it online!

Unpacked & How it works

Uq mZ{Zc nL g} rXY{X-Y&&Y}0

Uq     Convert the input string into list of chars
mZ{    Map...
Zc       Get charcode
nL       100 - charcode
g        sign(100 - charcode)
}
rXY{   Reduce...
X-Y&&Y   If X == Y, return 0; Otherwise, return Y
}0     ... default being 0

Having an array of -1s and 1s at hand yields a nice reducing function.

\$\endgroup\$
0
\$\begingroup\$

PHP 5, 47 bytes

Run on the command line, using php -nR 'code here'. The option -R counts as one byte.

echo(strrpos($s=x.$argn,"^")-strrpos($s,v))%2;
\$\endgroup\$
0
\$\begingroup\$

Julia 0.6, 38 bytes

s->foldl((x,c)->cmp(cmp('u',c),x),0,s)

Try it online!

Julia version of xnor's answer. (Uses foldl instead of reduce to save a byte.)

\$\endgroup\$
0
\$\begingroup\$

Jelly, 9 bytes

ŒgṪO³_^/Ṡ

Try it online!

Assumes the program this function is in doesn't take command-line arguments.

For a 10-byte version that doesn't assume so:

ŒgṪOȷ2_^/Ṡ

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

Perl 5 -pF, 32 bytes

$\=int(/v/?-($\>=0):$\<1)for@F}{

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.