# How to approach number guessing game(with a twist) algorithm?

I posted this on stackoverflow, but was advised to also post here. It’s kind of a math/algo question so I think it’s kind of stuck between both worlds of math and computer science. I believe this to be ontopic but if not, please let me know and I’ll delete it. Here’s the link to the stackoverflow post.

I am learning programming (python and algo’s) and was trying to work on a project that I find interesting. I have created a few basic python scripts but I’m not sure how to approach a solution to a game I am trying to build.

Here’s how the game will work:

Users will be given items with a value. For example

Apple = 1
Pears = 2
Oranges  = 3


They will then get a chance to choose any combo of them they like (e.g. 100 apples, 20 pears, and 1 orange). The only input the computer gets is the total value (in this example, it’s currently $143). The computer will try to guess what they have. Which obviously it won’t be able to get correctly the first turn.  Value quantity(day1) value(day1) Apple 1 100 100 Pears 2 20 40 Orange 3 1 3 Total 121 143  The next turn the user can modify their numbers but no more than 5% of the total quantity (or some other percent we may chose. I’ll use 5% for example.). Using the above example, on day 2 of the game, the user returns a value of \$152 and \$164 on day 3. Here’s an example. quantity(day2) %change(day2) value(day2) quantity(day3) %change(day3) value(day3) 104 104 106 106 21 42 23 46 2 6 4 12 127 4.96% 152 133 4.72% 164  (I hope the tables show up right, I had to manually space them so hopefully it’s not just doing it on my screen; if it doesn’t work let me know and I’ll try to upload a screenshot). I am trying to see if I can figure out what the quantities are over time (assuming the user will have the patience to keep entering numbers). I know right now my only restriction is that the total value cannot be more than 5%, so I cannot be within 5% accuracy right now, so the user will be entering it forever. What I have done so far Here’s my solution so far (not much). Basically I take all the values and figure out all the possible combos of them (I am done this part). Then I take all the possible combos and put them in a database as a dictionary (so for example for \$143, there could be a dictionary entry {apple:143, Pears:0, Oranges :0}… all the way to {apple:0, Pears:1, Oranges :47}. I do this each time I get a new number so I have a list of all possibilities.

Here’s where I’m stuck. Using the rules above, how can I figure out the best possible solution? I think I’ll need a fitness function that automatically compares the two days’ data and removes any possibilities that have more than 5% variance of the previous days data.

Questions:

So my question with user changing the total and me having a list of all the probabilities, how should I approach this? What do I need to learn? Is there any algorithms out there or theories that I can use that are applicable? Or, to help me understand my mistake, can you suggest what rules I can add to make this goal feasible (if it’s not in its current state. I was thinking adding more fruits and saying they must pick at least 3, etc.)? Also, I only have a vague understanding of genetic algorithms but I thought I could use them here; is there something I can use?

I’m very very eager to learn so any advice or tips would be greatly appreciated (just please don’t tell me this game is impossible).

UPDATE: Getting feedback that this is hard to solve. So I thought I’d add another condition to the game that won’t interfere with what the player is doing(game stays the same for them) but everyday the value of the fruits change price(randomly). Would that make it easier to solve? Because within a 5% movement and certain fruit value changes, only a few combo’s are probable over time. Day 1, anything is possible and getting a close enough range is almost impossible, but as the prices of fruits change and the user can only choose a 5% change, then shouldn’t(over time) the range be narrow and narrow. In the above example, if prices are volatile enough I think I could brute force a solution that gave me a range to guess in, but I’m trying to figure out if there’s a more elegant solution or other solutions to keep narrowing this range over time.

#### Solutions Collecting From Web of "How to approach number guessing game(with a twist) algorithm?"

This can be treated as a Hidden Markov Model. It is extremely soluble. The people who are telling you that this is intractible are wrong, or more accurately, they are referring to the wrong problem.

Yes, if you have a large set of values and you are only allowed to include each value once (as opposed to taking 20 apples), this is the subset-sum problem. That is not the case here.

You have a sequence of hidden states, characterized by the number of each object at time-step T. You have a nice known linear map from your hidden state to an observed value, and a known transition function from states at T to states at T+1. There are strong Bayesian methods to find the hidden states — but you can expect them to take a long time to converge.

This is even doable if you don’t know the linear map or the transition function, so long as you know they have nice properties.

N.B. This is assuming the human player is not adversarial. As mentioned above, if the human player is purposely staying away from extreme patterns, this is probably intractable. If the human player is essentially making adjustments at random, it is tractable. With random fluctuations in value, this is probably tractable.

If I have understood your question, then this will be hopeless if the only information are the total values and whether previous guesses are correct.

With your example with an initial value of 143, the number of possible patterns is 1776, ranging from 143 apples to 47 oranges and a pear. The computer guesses, and probably gets it wrong.

Next turn the the player declares a value is 149. There are now 1925 possible patterns, of which at most one has been eliminated, so the computer guesses, and probably gets it wrong. And so it goes on, with more possibilities being added and very few being eliminated each time. You might get lucky, but I would expect there is a positive probability the computer never gets the correct answer, especially if the player stays away from extreme patterns. Having more types of fruit would only make this worse.

We’ll combine graph-theory and probability:

On the 1st day, build a set of all feasible solutions. Lets denote the solutions set as A1={a1(1), a1(2),…,a1(n)}.

On the second day you can again build the solutions set A2.

Now, for each element in A2, you’ll need to check if it can be reached from each element of A1 (given x% tolerance). If so – connect A2(n) to A1(m). If it can’t be reached from any node in A1(m) – you can delete this node.

Basically we are building a connected directed acyclic graph.

All paths in the graph are equally likely. You can find an exact solution only when there is a single edge from Am to Am+1 (from a node in Am to a node in Am+1).

Sure, some nodes appear in more paths than other nodes. The probability for each node can be directly deduced based on the number of paths that contains this node.

By assigning a weight to each node, which equals to the number of paths that leads to this node, there is no need to keep all history, but only the previous day.

Also, have a look at non-negative-values linear diphantine equations – A question I asked a while ago. The accepted answer is a great way to enumarte all combos in each step.