I'm working on a quiz for my exam in computer science. I'm relatively new to the program, in the sense that I know all of the basics, but I am on the point where I want to expand my knowledge. One way I want to do this is by adding a Leaderboard system. The user gets a number of points, and then the program checks in a text file that has other high scores in it, and adds the user to it. It then prints out the leaderboard. This means that I'm going to have to use some sort of operations to determine whether the user's score is higher or lower than another score in the file, and then delete the score it is higher than and replace it. Any idea on how to do this? I'm completely stuck.
Try Pseudeocode and work through steps.
Get Score
Compare Score
Add Score
You have to think like a computer and break all the way down. At each step think about how do I tell the computer to do that. Once you have all that look at what you have done and remember DRY -> Don't Repeat Yourself. Your coding will go much faster.
Related
I have a website built with Node.js and MongoDB. Documents are structured something like this:
{
price: 500,
location: [40.23, 49.52],
category: "A"
}
Now I want to create a recommendation system, so when a user is watching item "A" I can suggest to him/her similar items "B", "C" and "D".
The thing is collection of items is changing relatively often. New items are created every hour and they do exist only for about a month.
So my questions are:
What algorithm should I use? Cosine similarity seems to be the most suitable one.
Is there a way to create such recommendation system with Node.js or it's better to use python/R?
When similarity score must be calculated? Only once (when a new item is created) or I should recalculate it every time a user visits an item page?
What algorithm should I use? Cosine similarity seems to be the most suitable one.
No one can really answer this for you, what makes a product similar to you? this is 100% product decision, it sounds like this is more of a pet side project and in that case I'd say use whatever you'd like.
If this is not the case I would assume best recommendations would be based on purchase correlation, i.e previous Users that bought product "A" also bought (or looked) at product "B" the most, hence it should be the top recommendation. Obviously you can create a much more complex model in the future.
Is there a way to create such recommendation system with Node.js or it's better to use python/R?
If it's a basic rule based system it can be done in node with ease, for any more data science related approach it will be more natural to implement this in python/R
When similarity score must be calculated? Only once (when a new item is created) or I should recalculate it every time a user visits an item page?
Again it depends on what your score is, how many resources you can invest, what the scale is etc.
as I mentioned before It sounds like this is a personal project. If this is the case I would try and choose the simpler solution for all these questions. Once you have the entire project up and running it'll be easier to improve on.
I use or-tools to optimize my fantasy baseball team. My setup very much resembles the program described here. The only difference in my particular case is that players can actually be eligible for a number of different positions. So, I end up with 1 player in a list for a specific position type, and the same player in another list for another position type. I am trying to avoid having the solver select the same player for multiple positions (which wouldn't be realistic).
Is there any way to modify the aforementioned program to constrain the use of a player to a single position even while they are technically eligible for many? Please let me know if I can clarify any further & thanks for your input.
I have recently made a game with python that makes the user decipher an anagram and based on difficulty it increases their score. Is there any way to implement a high score into this without the use of a text file?
The overall goal is for the program to compare the users score to the high score and if the score is greater it would edit the high score to the score gained. This would need to stay like that for the next run through of the program after I close it.
At the end of the day, you need to store the score in one type of database or the other, whether it's a file-based database or relational or any other. For one execution of your code, you can certainly keep it in the RAM, but for persistence, there's no way around it. If your use case is simple, you may consider sqlite instead of explicit file-based storage.
Don't forget: there's always HKCU and winreg module from stdlib. It can be useful. It's well documented.
This is my first post here, hope it is easily readable and also my answer is asked! (:
First of all, to put you a bit into perspective, I wanted to create an efficiency calculator for the brand new game No Man's Sky. The economy in that game is pretty much the same as in real life. You can sell items at a much higher price than the components needed to create it. E.g: You can sell a rock for 5000€ or 30 rock parts for 30€ each. If you craft the rock with the rock parts your profit will be 5000-900, right? (:
Here is the code.
What I want it to do is the following. User enters a product, the program compares the price of the product if sold and the price of the components to craft the product and shows you the profit doing so.
I have the following questions about it:
Is there a better way to save the data to use it after? (lines 1-16)
Is there any way to compare all variables I will create (line 18) or do I have to create an if loop for every product (lines 22-24). What I mean is something like
profit = products[input] - input_recipe
print profit
Since I want to check a lot of recipes, it would be a pain in the ass if there's a better for to do it.
How you save the data and access it will really depend on how you want to handle your calculator. I would say the best way to handle this would be if there were an excel file or JSON file or something of the sort that is all inclusive of all materials and items of the game (you may have to be the one to make this or someone else may already have). In the event you have to put the list together yourself, it could be a long process and very annoying, so try to find a list somewhere you can download then open the file and parse the data as needed. You could put all the data in the code itself but that doesn't allow you to write code against the data with say a different language if you so desired.
As far as loops are concerned, I'm not sure what you mean by that? You have dictionaries for your data so there's no need to loop over every value right? Now if you are referring to taking in multiple user inputs, a loop wouldn't be a bad idea for command line:
continue_calculations = 'y'
while continue_calculations != 'n':
# Do your logic here.
continue_calculations = raw_input('Would you like to continue(y/n)?')
Of course if you are making a calculator you could look into GUI development, or web development if you want to make it into a site. PyQT is a handy little module to work in and there are some good tutorials for that: https://pythonprogramming.net/basic-gui-pyqt-tutorial/
Cheers,
About your first question, another way would be to use json format to store your data and not to use three separate dictionaries, I mean something like:
data = {"elements":{"Th":20.6,"Pu":41.3},"alloys":{"Aronium":1546.9,"Herox":2877.5},"Products":{"Antimatter":5232,"Warp Cell":46750}}
You could parse for example "Th" price by writing:
th_price = data['elements']['Th']
As for your second question you could create a fourth dictionary that would contain the prices of all the possible recipes which of course you have predefined - just not to compute them every time you need them and to have them available for fast parsing. So you would write something like:
profit = products[input] - input_recipe[input]
print profit
where input_recipe would be your fourth dictionary with the recipe prices.
I need to create a function/method ( in python) which calculates a high score "leaderboard". Each player will have played any number of rounds of the game, recieving a score for each round. I want to know what's the best way to sort the top ranking players (accounting for score AND number of rounds played). The possible scores for each round are F, D-, D, D+, C-, C, C+, B-, B, B+, A-, and A.
Obviously a simple average won't work because it doesn't take into account number of rounds played. Whats the best way to set up a fair sorting function?
EDIT: I've been reading some of the really great answers here and I want to try to clear up my question a bit. I want both the players score AND the number of rounds they've played to count towards their ranking in a way that's fair. Meaning a player with 20 B's should be of a higher rank than a player with 5 A's. Basically the high score should reflect general effort and skill, "the number of rounds played PLUS their score" means the higher their ranking should be.
EDIT 2: After reading the answers, I think the best way to do it is a simple total sum of the players points across all rounds. I'm not sure which answer to assign the green check to because you were all correct.
There are many ways that you could do this. Try this for example, let F-A be 0-11 (you can make your own; however try to take difficulty into account), so each score is one higher than the previous. For every game you play, you receive a score (from 0-11). Create a total score and add the game score every time to the total score. That way, if a person receives 7 A's, that's 77, while a person that receives 7 A-'s gets a score of 70, then simply sort them accordingly. Each function has its drawbacks of course. This function is not the "best", consider getting 20 B's would exceed 7 A's even though, 7 A's is a much better score. if you can give me more details about how you want to rank them, then it will be much easier to get the algorithm down.
What you are asking is essentially how we define "good" players and it's not an easy problem. As you mentioned, a simple average score or picking-the-highest-score will not be an ideal answer depending on your game design.
I'd like to recommend that you read about ELO rating system for Chess and other modified versions of it before you design your own player rating system.
One simple and possible way is you can set a window (like 10 most recent games) and use average score from the window. Players who play less games than this window would be "in placement" state. Again, it's not an easy problem and heavily depends on what your game is. Good Luck!
[UPDATE]
I assumed that your game is player vs. player. If not, this is another story. Most games just keep the highest score no matter how many times you play the game and that's going to be your entry in the leaderboard. Since you don't say anything about your game, I have no idea why it wouldn't be fair. As I mentioned earlier, you could set a window for avg. score or the highest score. You can even reset your leaderboard every month or remove players who haven't played for a week. It all depends on your game and what you want. Please remember that no matter what you do, make it sure that the rules are crystal clear for players otherwise they would be easily upset and frustrated.