# Checking for similar items in sets

1. Oct 9, 2004

### Gotcha

Hi There Everyone!

I was wondering if any of you could guide me in the right direction concerning the following problem (an algorithm would be much appreciated):

I am writing a program for a Computer Science Project.
The Program works as follows:
1. It generates a random number with x items.
2. The user then enters a set of numbers.
3. The program then outputs how many of these numbers are in the right position
4. This proccess continues until the user has entered the initial the random number.

I now want to add a feature where the user enters a number of sets, each containg x items, as well as how many numbers are in the right position.
My program should then work out which numbers are in the right positions.

I want to know if there is some algorithm or mathematical logical process by which I could program this.

Any help on this matter would be much appreciated!

2. Oct 10, 2004

### TenaliRaman

is this a guess the number of game?
i guess where u say x items .. u mean x digits??

-- AI

3. Oct 10, 2004

### Gotcha

Yes, I do mean x digits,
4 for example.

4. Oct 10, 2004

### Gokul43201

Staff Emeritus
Okay, so the program generates a 4 digit number, say. Then the user is prompted to guess a list of 4 digit numbers ?

Let's do an example, 'cause I'm not sure exactly what you have in mind - but this seems similar to a 2 person board game called "Mastermind" that I've played.

Random number = 3579

Now user must guess. How many guesses ? Let's say 3, for now.

Guesses = 1234, 2345, 3456

Now what happens ? In the first guess, the user got 1 digit right but it's in the wrong position. In the second guess, the user got 2 digits right, but they're both in wrong positions. In the third guess, the user got 2 digits right but only one of them is in the right position.

So, what is the output ? Does it say that the user got 1 digit correct ? Does it say, in which guess the user got this digit ?

Anyways, here's how you extract digits from a number, say a 4 digit number : (let the variable that stores this number be X)

unit's digit, U = X mod 10 {remainder when X is divided by 10}
ten's digit, T = ((X mod 100) - U)/10
hundred's digit, H = ((X mod 1000) - (X mod 100))/100
thousand's digit, M = (X - (X mod 1000))/1000

Most programming languages have a mod (or equivalent ) function that outputs remainders.

You can store these values into a 2-D array and compared them with values obtained from guesses.

Last edited: Oct 10, 2004
5. Oct 11, 2004

### Gotcha

It only says that a digit is right if it is in the right position.

But you are all missing the point. My program should solve the sequence, by using mathematical logic, when the user types in a number of sets and how many numbers are in the right positions.

6. Oct 11, 2004

### matt grime

Which langauge are you using for this?

Haskell seems to offer the easiest solution from what little I know of the languages.

Anway, given a (small, arbitrary) set of guesses there is no way of guaranteeing that you can deduce the correct answer.

Why don't you try figuring out how you'd solve it, if you can, and try coding it. Try it for 2 digits only to begin with, or better, try it for a smaller set of digits than possibly ten in each position.

7. Oct 12, 2004

### Gotcha

I coded the program in Delphi, as I was very lazy, but I can also program in Java as well as C++ if I need to convert it.

What matt grime posted is very true, an arbitrary set of guesses won't neccessarily be able to solve the code.

The algorithm should still however work out which digits are in the right places, if any.

I tried to solve the problem by using a lot of "If..Then" statements. This was first of all very tedious and secondly it was to no avail.

8. Oct 12, 2004

### matt grime

I'm a little unclear still: do you have a method you cannot code, or not even a method? If you can't solve it on paper you can't code it. So forget coding for now. How would you solve the problem on paper? Where did you start?

9. Oct 13, 2004

### Gotcha

I need help with the actual solving of the problem.
Thats what I asked for - a mathematical way through which I can solve this program