patternpythonMinor
User ranking system
Viewed 0 times
rankingusersystem
Problem
I have written my first code of object oriented Python. Prior to this I have spent a week on learning the concepts and understanding the technique. I would appreciate it somebody reviews this and give suggestion on where I can improve.
I am working through codewars Kata and this code is for the problem. I am copying the problem here so that some one can see what I have solved.
Write a class called User that is used to calculate the amount that a
user will progress through a ranking system similar to the one
Codewars uses.
Business Rules:
-
A user starts at rank -8 and can progress all the way to 8.
-
There is no 0 (zero) rank. The next rank after -1 is 1.
-
Users will complete activities. These activities also have ranks.
-
Each time the user completes a ranked activity the users rank progress is updated based off of the activity's rank
-
The progress earned from the completed activity is relative to what the user's current rank is compared to the rank of the
activity
-
A user's rank progress starts off at zero, each time the progress reaches 100 the user's rank is upgraded to the next level
-
Any remaining progress earned while in the previous rank will be applied towards the next rank's progress (we don't throw any progress
away). The exception is if there is no other rank left to progress
towards (Once you reach rank 8 there is no more progression).
-
A user cannot progress beyond rank 8.
-
The only acceptable range of rank values is -8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8. Any other value should raise an error.
The progress is scored like so:
-
Completing an activity that is ranked the same as that of the user's will be worth 3 points
-
Completing an activity that is ranked one ranking lower than the user's will be worth 1 point
-
Any activities completed that are ranking 2 levels or more lower than the user's ranking will be ignored
-
Completing an
I am working through codewars Kata and this code is for the problem. I am copying the problem here so that some one can see what I have solved.
Write a class called User that is used to calculate the amount that a
user will progress through a ranking system similar to the one
Codewars uses.
Business Rules:
-
A user starts at rank -8 and can progress all the way to 8.
-
There is no 0 (zero) rank. The next rank after -1 is 1.
-
Users will complete activities. These activities also have ranks.
-
Each time the user completes a ranked activity the users rank progress is updated based off of the activity's rank
-
The progress earned from the completed activity is relative to what the user's current rank is compared to the rank of the
activity
-
A user's rank progress starts off at zero, each time the progress reaches 100 the user's rank is upgraded to the next level
-
Any remaining progress earned while in the previous rank will be applied towards the next rank's progress (we don't throw any progress
away). The exception is if there is no other rank left to progress
towards (Once you reach rank 8 there is no more progression).
-
A user cannot progress beyond rank 8.
-
The only acceptable range of rank values is -8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8. Any other value should raise an error.
The progress is scored like so:
-
Completing an activity that is ranked the same as that of the user's will be worth 3 points
-
Completing an activity that is ranked one ranking lower than the user's will be worth 1 point
-
Any activities completed that are ranking 2 levels or more lower than the user's ranking will be ignored
-
Completing an
Solution
On top of what zondo has said you should avoid using parenthesis unnecessarily. There are numerous examples of this:
can just be:
also less obvious:
should be:
while we're at it, although you explained it in the description,
Maybe somewhere like above
And use this when appropriate.
if (self.rank==8):can just be:
if self.rank == 8:also less obvious:
rank_vector =[i for i in range(-8,9,1) if ( i!=0)]should be:
rank_vector = [i for i in range(-8,9) if i != 0] #Also, no need for the additional "1" parameter.while we're at it, although you explained it in the description,
-8 (nor 8 for what it is worth) isn't the most common of numbers and it seems (without explanation) to be a bit weird.Maybe somewhere like above
rank_vector say:MIN_RANK = -8
MAX_RANK = 8And use this when appropriate.
Code Snippets
if (self.rank==8):if self.rank == 8:rank_vector =[i for i in range(-8,9,1) if ( i!=0)]rank_vector = [i for i in range(-8,9) if i != 0] #Also, no need for the additional "1" parameter.MIN_RANK = -8
MAX_RANK = 8Context
StackExchange Code Review Q#125264, answer score: 3
Revisions (0)
No revisions yet.