# Calculate your stack exchange reputation

Background:

I often find that, when browsing a Stackexchange site, I begin to wonder how people get the amount of reputation that they have. I know I can always count on codegolf SE to solve my problems, so here it is:

Create a program that will accept a positive integer which represents a person's reputation. We will ignore bounties, and say that these are the only ways to gain/lose rep on SE (full table here):

• Every account starts out with 1 rep and cannot go below that
• Your question is upvoted = +5
• Your answer is upvoted = +10
• Your question is downvoted = -2
• Your answer is downvoted = -2
• You downvote an answer = -1
• Your answer is accepted = +15
• You accept an answer = +2

Your program must figure out how many of those actions occurred on that user's account to get to the rep they have. It must figure out the shortest number of actions to get to this level of rep. Examples:

Input: 11 Output: 1 answer upvote

Input: 93 Output: 6 answer accepts, 1 answer accepted

In these examples when I say 'question upvote', it means that person's queston got upvoted. When I say 'answer downvoted', it means they downvoted someone else's answer.

Rules:

, so the shortest code that can do this wins.

• There are an infinite number of ways to obtain any given reputation. Only implementing "answer upvote" and "answer downvoted" is enough to always find a way, so there is no impetus to use any wider subset of the score changes. Is this what you intended? – algorithmshark Mar 29 '14 at 22:17
• @algorithmshark edited. You must find the smallest number of actions that will get you there – TheDoctor Mar 29 '14 at 22:19
• """I often find that, when browsing a Stackexchange site, I begin to wonder how people get the amount of reputation that they have.""" vs """You must find the smallest number of actions that will get you there""". The 2nd quote is not neccessarily the correct answer to the first quote. – user19214 Mar 30 '14 at 2:32
• @algorithmshark In general I agree with your comments. However 6 answer accepts + 1 answer accepted is 6*15+2=92, not 93! I can't see a way to do it in 7 actions, but I can do it in 8: 6 answer accepts, one question upvote, one question downvote: 6*15+5-2=93. Doctor, if there is more than one possibility for "smallest number of actions" do we have to find all of them or just one? – Level River St Mar 30 '14 at 2:52
• @steveverrill you start with 1 rep – TheDoctor Mar 30 '14 at 3:16

# Golfscript, 162 144 bytes

{{}if}:?;~.)15/:^15*-:~3>1~8>' answer ':A' question 'if'upvote'++?^^A'accept'+:C+^1>{'s'+}??~5%:,4<,1>&1C'ed'++?,2%!1A'downvote'++,4<{'d'+}??]n*


# How it works

The general idea is exactly the same as in my Bash answer.

{{}if}:?;         # Create an if' statement with an empty else' block.
#
~.)15/:^          # Interpret the input string, subtract 1 from its only element (the
# reputation score), divide by 15 and save the result in ^'. This gives
# the number of accepted answers.
#
15*-:~            # Multiply the number of accepted answer by 15 and subtract the product
# from the reputation score. Save the result in ~'.
#
3>                # If the result is greater than 3:
#
1               # Push 1 on the stack.
#
~8>             # If the result is greater than 8:
#
' answer ':A  # Push answer' on the stack. Either way, save the string in A'.
#
' question '  # Otherwise, push question' on the stack.
#
if              #
#
'upvote'++      # Push upvote' on the stack and concatenate the three strings.
#
?                 #
#
^                 # If the number of accepted answers is positive:
#
^A'accept'+:C+  # Push the number, concatenated with the string  answer accept', on the
# stack. Either way, the string in C'.
#
^1>             # If the number of accepted answers is greater than 1:
#
{'s'+}        # Cocatenate the previous string with s', for proper pluralization.
#
?               #
#
?                 #
#
~5%:,             # Calculate the reputation score modulus 5. Save the result in ,'.
#
4<,1>&            # If the result is less than 4 and greater than 1:
#
1C'ed'++        # Push the string 1 answer accepted' on the stack.
#
?                 #
#
,2%!              # If the result is odd:
#
1A'downvote'++  # Push the string 1 answer downvote' on the stack.
#
,4<             # If the result is less than 4:
#
{'d'+}        # Concatente the previous string with d'.
#
?               #
#
?                 #
#
]n*               # Join the array formed by all strings on the stack, separating the
# strings by newlines. This is the output.


# Bash, 247202 192 bytes

n=$1 bash <(sed 's/E/)echo /;s/C/ Aaccept/;s/A/answer / s/.*)/((&)\&\&/'<<<'a=(n+1)/15,a-1)s=s;q=question aE$aC$s r=n%5,r-4)d=d&& r>1E1Ced 1-r%2E1 Adownvote$d
n-=15*a,n>8)q=A
((n>3))&&echo 1 $q upvote  To obtain the optimal solution (minimal number of events to get n reputation), it suffices to compute the number of accepted answers (a) necessary to get to a reputation below 16 (1 answer accept) and handle the residue as follows: 1 (no rep change) 2 answer accepted, answer downvoted 3 answer accepted 4 question upvote, answer downvote 5 question upvote, answer downvoted 6 question upvote 7 question upvote, answer accepted, answer downvoted 8 question upvote, answer accepted 9 answer upvote, answer downvote 10 answer upvote, answer downvoted 11 answer upvote 12 answer upvote, answer accepted, answer downvoted 13 answer upvote, answer accepted 14 answer accept, answer downvote 15 answer accept, answer downvoted  • Thanks for the explanation, it's not simple to deal with -2 and -1 downvotes. – A.L Mar 31 '14 at 1:33 # Perl, 500263256 208 bytes Script rep.pl: $_=1+pop;sub P($){print$=,@_,$/}$;=" answer ";$:="$;downvote";($==$_/15)&&P"$;accept"."s"x($=>1);$_%=15;$==1;P"$;upvote",$_-=10if$_>9;P" question upvote",$_-=5if$_>4;P"$;accepted"if$_>2;P$:."d"if$_%2;P$:if!$_  ## Usage The input is expected as positive integer, given as argument to the script. The different actions are output as lines. ## Tests perl rep.pl 11 1 answer upvote perl rep.pl 93 6 answer accepts 1 answer accepted perl rep.pl 1 perl rep.pl 4 1 question upvote 1 answer downvote perl rep.pl 12 1 answer upvote 1 answer accepted 1 answer downvoted perl rep.pl 19 1 answer accept 1 question upvote 1 answer downvote perl rep.pl 34 2 answer accepts 1 question upvote 1 answer downvote perl rep.pl 127 8 answer accepts 1 question upvote 1 answer accepted 1 answer downvoted perl rep.pl 661266 44084 answer accepts 1 question upvote  ## Ungolfed $_ = pop() + 1; # read the reputation as argument,
# remove the actionless start reputation
# and add a bias of two to calculate
# the answer accepts in one division.

# Actions
# -------
# question upvotes:     Your question is upvoted   = +5
# answers accepted:     You accept an answer       = +2
# answers downvoted:    You downvote an answer     = -1
# (questions downvoted: Your question is downvoted = -2) not used

# Function P prints the number of actions in $= and # the action type, given in the argument. # The function is prototyped "($)" to omit the
# parentheses in the usage.
sub P ($) { print$=, @_, $/ #$/ is the line end "\n"
}
# abbreviations,
# special variable names to save a space if a letter follows
$; = " answer ";$: = "$;downvote"; # Calculation and printing the result # ----------------------------------- ($= = $_ / 15) && # integer division because of the special variable$=
P "$;accept" . "s" x ($= > 1); # short for: ($= == 1 ? "" : "s")$_ %= 15;
$= = 1; # now the action count is always 1 if the action is used P "$;upvote",         $_ -= 10 if$_ > 9;
P " question upvote", $_ -= 5 if$_ > 4;
P "$;accepted" if$_ > 2;
P $: . "d" if$_ % 2;
P $: if !$_


## Older version

$_ = pop() + 1; # read the reputation as argument # subtract start reputation (1) # add bias (2) # Actions # ------- #$= answer accepts:      Your answer is accepted    = +15
# $b answer upvotes: Your answer is upvoted = +10 #$c question upvotes:    Your question is upvoted   = +5
# $d answers accepted: You accept an answer = +2 #$e answers downvoted:   You downvote an answer     = -1
# $f answer downvotes: Your answer is downvoted = -2 # -- questions downvoted: Your question is downvoted = -2 # Calculaton of answer accepts by a simple division that is # possible because of the bias.$= = $_ / 15; # integer division because of the special variable$=
$_ %= 15; # The older version of the calculation can be simplified further, see below. # Also the older version did not use the bias. # # function E optimizes the construct "$_ == <num>" to "E <num>"
# sub E {
#     $_ == pop # } # #$d = $e = 1 if E 1; # 1 = +2 -1 #$d++ if E 2;              #  2 =     +2
#
# $c =$f = 1 if E 3;       #  3 =  +5 -2
# $c =$e = 1 if E 4;       #  4 =  +5 -1
# $c++ if E 5; # 5 = +5 #$c = $d =$e = 1 if E 6;  #  6 =  +5 +2 -1
# $c =$d = 1 if E 7;       #  7 =  +5 +2
#
# $b =$f = 1 if E 8;       #  8 = +10 -2
# $b =$e = 1 if E 9;       #  9 = +10 -1
# $b++ if E 10; # 10 = +10 #$b = $d =$e = 1 if E 11; # 11 = +10 +2 -1
# $b =$d = 1 if E 12;      # 12 = +10 +2
#
# $=++,$f++ if E 13;       # 13 = +15 -2
# $=++,$e++ if E 14;       # 14 = +15 -1

$b++,$_ -= 10 if $_ > 9;$c++, $_ -= 5 if$_ > 4;

# Now $_ is either 0 (-2), 1 (-1), 2 (0), 3 (1), or 4 (2). # The number in parentheses is the remaining reputation change. # The following four lines can be further optimized. #$f++        if ! $_; # "!$_" is short for "$_ == 0" #$e++        if $_ == 1; #$d = $e = 1 if$_ == 3;
# $d++ if$_ == 4;

# Optimized version of the previous four lines:

$f++ if !$_;
$e++ if$_ % 2;
$d++ if$_ > 2;

# function P optimizes the printing and takes the arguments for "print";
# the first argument is the action count and the printing is suppressed,
# if this action type is not needed.
sub P {
print @_, $/ if$_[0]
# $/ is "\n" } # some abbreviations to save some bytes$; = " answer ";
$D = "$;downvote";

# output the actions

P $=, "$;accept", ($= == 1 ? "" : "s"); P$b, "$;upvote"; P$c, " question upvote";
P $d, "$;accepted";
P $e,$D, "d";
P $f,$D


## Edits

• Case 4 is fixed.
• This also simplifies the calculation that is now done without a loop.
• Unreachable plural "s" removed, function S no longer needed.
• Calculation optimized, function E no longer needed.
• Bias of 2 added for optimized calculation.
• Larger rewrite to remove most variables some other tricks to save some bytes.
• according to this, Jon Skeet has 44084 answer accepts & 1 answer upvote – TheDoctor Mar 30 '14 at 3:40
• @TheDoctor: According to the question, these are the minimal number of actions to get a reputation of 661266. – Heiko Oberdiek Mar 30 '14 at 4:01

# R, 454 421

r=as.integer(commandArgs(T)[1])-1;p=function(...){paste(...,sep='')};a='answer ';b='accept';e='ed';f='d';v='vote';d=p('down',v);u=p('up',v);q='question ';z=c();t=r%/%15;if(t>0){z=c(p(t,' ',a,b));r=r%%15;};if(r%in%(8:12))z=c(z,p(a,u));if(r%in%(3:7))z=c(z,p(q,u));if(r%in%c(1,2,6,7,11,12))z=c(z,p(a,b,e));if(r%in%(13:14))z=c(z,p(a,b));if(r%in%c(3,8,13))z=c(z,p(a,d));if(r%in%c(1,4,6,9,11,14))z=c(z,p(a,d,f));cat(z,sep=', ')


Thanks to Dennis for his answer which helped me a lot.

## Ungolfed version

# read input
r = as.integer(commandArgs(T)[1]) - 1

# shortcut to join strings (... will pass the parameter to paste() *as is*)
p = function(...) {paste(..., sep = '')}

# strings
a = 'answer '; b = 'accept'; e = 'ed'; f = 'd'
v = 'vote'; d = p('down',v); u = p('up',v)
q = 'question '

z = c()

# +15
t = r %/% 15;
if (t > 0) {
z = c(p(t,' ',a,b))
r = r %% 15
}

if (r %in% (8:12))              z = c(z,p(a,u));    # answer upvote
if (r %in% (3:7))               z = c(z,p(q,u));    # question upvote
if (r %in% c(1,2,6,7,11,12))    z = c(z,p(a,b,e));  # answer accepted
if (r %in% (13:14))             z = c(z,p(a,b));    # answer accept
if (r %in% c(3,8,13))           z = c(z,p(a,d));    # answer downvote
if (r %in% c(1,4,6,9,11,14))    z = c(z,p(a,d,f));  # answer downvoted

# print operations
cat(z,sep = ', ')


# JavaScript - 270237227206 192 characters

p=prompt,r=p()-1,a="1answer ",v="vote,";s=(r/15|0)+"answer accept,",r%=15;if(r>9)s+=a+"+"+v,r-=10;if(r>2)s+="1question +"+v,r-=5;r>0?s+=a+"accepted,":0;r<-1?s+=a+"-"+v:0;p(r&1?s+=a+"-voted":s)


Exactly as many chars as Bash (yeah!), and beats Python and Perl :) It cuts down the reputation until 14 after which it takes after which it takes the other necessary actions, completely loop-style.

EDIT 1: Converted \ns to ,s and converted one if block to ternary, and better flooring with short names.

EDIT 2: Big thanks to Alconja who helped me cut down 11 chars. After which I made some more corrections to cut down 2 more chars.

Older Version:

r=prompt()-1,a="1answer ",q="1question ",v="vote,";s=(c=r/15|0)+"answer accept,",r-=c*15;if(r>9)s+=a+"+"+v,r-=10;if(r>2)s+=q+"+"+v,r-=5;r>0?s+=a+"accepted,":0;if(r<-1)s+=a+"-"+v;r&1?s+=a+"-voted":0;alert(s)


## Test:

INPUT: 42
OUTPUT:

2answer accept,1answer +vote,1answer accepted,1answer -voted

/*I sincerely hope the output is clear and easy to make out*/


INPUT: 1337
OUTPUT:

89answer accept,1answer accepted,1answer -voted


## Ungolfed Code:

// different version from the golfed code
rep = prompt() - 1
string = ""

function $(z, c, k){ while(rep > 0 && rep >= z - 2) c += 1 , rep -= z; if(c) string += c + k + "\n" } a=" answer ", q=" question "$(15, 0, a + "accept")
$(10, 0, a + "upvote")$(5, 0, q + "upvote")
$(2, 0, a + "accepted") function _(z, c, str){ while(rep <= z) c += 1, rep -= z if(c) string += c + str + "\n"; } _(-2, 0, a + "downvote"); _(-1, 0, a + "downvoted"); alert(string);  • Why is the first one Firefox only? – TheDoctor Mar 31 '14 at 13:41 • @TheDoctor It utilizes the feature of JS currently available only in Firefox - function name(args){} becomes name=(args)=>{} and thus saves a lot of bytes. – Gaurang Tandon Mar 31 '14 at 13:54 • @TheDoctor I have updated my program to be cross-browser, and it is now much shorter than before ! – Gaurang Tandon Mar 31 '14 at 15:17 • Your current version only uses q once, so you can inline it. Also, you can drop the c variable and do a r%=15 instead of r-=c*15. Should bring you down to 195 chars (r=prompt()-1,a="1answer ",v="vote,";s=(r/15|0)+"answer accept,",r%=15;if(r>9)s+=a+"+"+v,r-=10;if(r>2)s+="1question +"+v,r-=5;r>0?s+=a+"accepted,":0;if(r<-1)s+=a+"-"+v;r&1?s+=a+"-voted":0;alert(s)). – Alconja Apr 11 '14 at 2:23 • @Alconja Wow! Thanks a lot! I finally am very very close to Bash ! Thanks a lot again! – Gaurang Tandon Apr 11 '14 at 11:26 ## Game Maker Language, 276 p=real(keyboard_string())-1j="#"s=""z=" answer"w=" accept"x=" upvoted"+j;y=w+"ed"v=" question"u=" downvoted"if m=floor(p/15)s+=(m+z+y)+j;r=p-m*15if m=floor(r/10)s+=(m+z+x)r-=m*10if m=floor(r/5)s+=(m+v+x)r-=m*5n=floor(r/2)r-=n*2if m=r{n++;s+=(m+u+z)+j}s+=(n+y+z)show_message(s)  ## C# - 391 A bit long, and I haven't tested this thoroughly (much). :) class R{void Main(string[] a){var r=int.Parse(a[0])-1;var a=new[]{15,10,5,2};var o=new List<string>();Func<int,string>y=z=>{var w="";if(z==15)w=" answer accepted";if(z==10)w=" answer upvotes";if(z==5)w=" question upvotes";if(z==2)w=" answer accepts";return w;};foreach(var x in a)if(r/x>0){o.Add(r/x+y(x));r-=(r/x)*x;}if(r==1)o.Add("1 question downvotes");Console.Write(string.Join(", ",o));  Un-golfed - NEW class R { void Main(string[] a) { var r = int.Parse("122")-1; // subtracts 1 from total rep var a = new[] {15,10,5,2}; var o = new List<string>(); Func<int,string> y = z => { var w=""; if(z==15) w=" answer accepted"; if(z==10) w=" answer upvotes"; if(z==5) w=" question upvotes"; if(z==2) w=" answer accepts"; return w; }; foreach(var x in a) { if (r/x>0) { o.Add(r/x+y(x)); r-=(r/x)*x; } } if(r==1) o.Add("1 question downvotes"); Console.Write(string.Join(", ",o)); } }  Un-golfed - OLD (409) class R { void Main(string[] a) { var r = int.Parse(a[0])-1; // subtracts 1 from total rep var v = new[] {" question"," answer"," downvotes"," upvotes"," accepts"," accepted"}; var o = new List<string>(); // Starts from 15, then checks all the lower values. if (r/15>0) { o.Add(r/15+v[1]+v[5]); r-=(r/15)*15; // automatic rounding down due to int } if(r/10>0) { o.Add(r/10+v[1]+v[3]); r-=(r/10)*10; } if(r/5>0) { o.Add(r/5+v[0]+v[3]); r-=(r/5)*5; } if(r/2>0) { o.Add(r/2+v[1]+v[4]); r-=(r/2)*2; } if(r==1) { o.Add("1"+v[0]+v[2]); } Console.Write(string.Join(", ",o)); } }  Test: > prog.exe 120 7 answer accepted, 1 answer upvotes, 2 answer accepts  Python - 213 207 p,k=__import__('itertools').combinations_with_replacement,int(input()) t,m,u=[5,10,-2,-1,15,2],[],iter(range(0,k)) while not m:m=list(filter(lambda v:k-1==sum(v),p(t,next(u)))) print(''.join(map(chr,m[0])))  Curse you long function names! Example: (ignore the trailing newline) $ echo "93" | python per.py | hexdump -C
00000000  0f 0f 0f 0f 0f 0f 02 0a                           |........|

$echo "11" | python per.py | hexdump -C 00000000 0a 0a |..|  • How do you display number of questions and answers votes, etc.? Your code doesn't contain these strings (see the other answers), so I'm afraid the output is not compliant with the rules. – A.L Apr 1 '14 at 2:19 • The output is golfed too since there was no requirement about that. It doesn't treat downvoted question/answers separately as both give -2 points, the resulting list that is printed is the shortest sequence to achieve the score. – LemonBoy Apr 1 '14 at 14:27 • Yes, the rules don't go into details about this point. But you can notice that in the other answers the output is standard and display X answer accepts, Y answer upvotes, etc. But that's not a problem since you don't have the shortest code. – A.L Apr 1 '14 at 14:32 • @LemonBoy I have tried this on three interpreters and it doesn't work. All say EOF. Can you please point me to a compiler which works (and which I should keep for future reference) ? – Gaurang Tandon Apr 1 '14 at 14:46 • @GaurangTandon sigh, you're trying to run Python code using the coffeescript interpreter – LemonBoy Apr 3 '14 at 18:00 # C++, 276 (316 w/ includes) #include <stdio.h> #include <stdlib.h> p(int&q,int*d){int r;char*s[]={"downvoted","accepted","question","answer","upvoted"}; if(r=(q&&q>=*d)){q-=(*d?*d:2);printf("%s %s\n",s[*(++d)],s[*(++d)]);}return r;}main( int n,char**v){int q=atoi(v[1]);int d[]={-1,3,0,0,3,1,5,4,2,10,4,3,15,1,3};n=15;while (p(q,d+n-3)||(n-=3));}  Compiles with GCC, with warnings. Example: $ ./a.out 0
$./a.out 1 accepted answer downvoted answer$ ./a.out 2
$./a.out 5 question upvoted$ ./a.out 10
$./a.out 15 answer accepted$ ./a.out 16
$./a.out 17 answer accepted accepted answer  Feel free to port this to a language that doesn't require type declarations and post it as your own. # JavaScript - 273256 235 p=prompt(s=j="\n")-1;z=" answer",w=" accept",x=" upvoted"+j,y=w+"ed",v=" question",u=" downvoted";if(m=p/15|0)s+=m+z+y+j;r=p-m*15;if(m=r/10|0)s+=m+z+x;r-=m*10;if(m=r/5|0)s+=m+v+x;r-=m*5;n=r/2|0;if(m=r-=n*2)n++,s+=m+u+z+j;alert(s+n+y+z)  Combined calculation and output, and golfed further to a total of 287. Edit: took out some variables for a few shorter. Removed Math.Floor for |0 approach. Moved some initialisation to the prompt() param, removed some brackets, alert with final string append. • Welcome to codegolf.SE ! The instructions say: "Create a program that will accept a positive integer" -> so you will need to use prompt, and can't hard code the value. – Gaurang Tandon Mar 31 '14 at 5:20 • No worries, added prompt(), which bumps it up to 161. – Matt Mar 31 '14 at 5:25 • Following @GaurangTandon's smarter prompt()-1 and alert output approach to get this down further. Reduced some of the hardcoded string storage too. – Matt Mar 31 '14 at 7:30 # Python3, 188B n=input()+1 a=n//15 n%=15 A='answer ' print("%d %saccepted\n%d %supvoted\n%d question upvoted\n%d accept %s\n%d downvote %s\n%d %sdownvoted"%(a,A,n//10,A,n%10//5,n%5>2,A,n%5%2,A,n%5==0,A))  Usage: python3 score.py <ret> 11 <ret> where this script is saved as score.py. Sample output: $ python score.py
`