Find a 4 digits number without repetition - algorithm

I've received a question to solve. I've manage to solved it by hand but couldn't find an exact algorithm to solved it.
The question:
The user insert a 4 digits number without repetition of any of the digits (can have a leading 0).
The algorithm needs to find this number.
There is a check function that received a 4 digits number and returns how many numbers were correct, and how many of the numbers were in the correct place without overlap, and without mentioning which one is.
The system has 7 tries.
For example:
The user insert the Number: 0584
If the system sends the number: 6549
The return would be:
1 item in place
1 item is correct

I would try to solve this puzzle in 2 steps:
Try to find out which 4 digits the solution contains of.
For the miss-placed positions find the right order.
I'm not sure if there is an algorithm to always find the solution within 7 requests, but this way you should at least get closer to a solution.
It's a puzzle, so think about digit-patters that reveal as much information about the solution as possible. And try to extract as much information from the answer to reduce the number of still possible solutions.

Related

How to find the minimun multiple given an incomplete number without using brute force

I just want a direction in this programming problem from an online judge (URI online judge - 2699).
Given two numbers, S and N, S is incomplete, so S can be given in the form ?294?? where the first digit is not zero, I need to find the minimum number that has the same digits as S and is multiple of N. If is not possible, then you just return a *. S can have up to 1000 digits and N < 1000.
I will describe my attempts:
Brute Force: I try every combination of numbers and get the first one to be multiple of N. Finding a solution when it exists is not the problem at all, but discovering that does not exist a solution when S is big can be really problematic and take infinite time.
Brute Force but optimizing the form of finding the rest: This attempt i save into an array the rest of the division for the digit 1 in the i position, so for i = 3, in v[3] i will have 1000%N. Knowing that (AB) MOD N = ((A MOD N)B) MOD N it's possible to write an array pretty quick and optimize the way i calculate the Mod of S which. This attempt does improve the time but is a Brute Force Attempt and has the same issues that the previous one.
Using the remainder to do the recursion: Ex: If i have the number
?294?? in S, i get the remainder of 29400 and calculate how much is needed to have a multiple (N - rem), then i try to get it all from the first digit, if it is not possible then i decrease how much I want and try again, then I go to the left and try with another number. Ex if i need 7 to reach N and can get 5 with the first digit, then I will try to find 2 within the second digit and so on.
Does it have a concept that i ain't seeing here ? I'm trying this problem for almost 3 days, searching ways to do this and not getting anywhere because of time.
EDIT: Thanks for the comments, after thinking all day about this problem and reading a lot of Dynamic Programming I could figure a way to apply DP in this problem, I won't say exactly how but the key is to understand DP and figure a way to reduce the size of your problem.

Check if string includes part of Fibonacci Sequence

Which way should I follow to create an algorithm to find out whether fibonacci sequence exists in a given string ?
The string includes only digits with no whitespaces and there may be more than one sequence, I need to find all of them.
If as your comment says the first number must have less than 6 digits, you can simply search for all positions there one of the 25 fibonacci numbers (there are only 25 with less than 6 digits) and than try to expand this 1 number sequence in both directions.
After your update:
You can even speed things up when you are only looking for sequences of at least 3 numbers.
Prebuild all 25 3-number-Strings that start with one of the 25 first fibonnaci-numbers this should give much less matches than the search for the single fibonacci-numbers I suggested above.
Than search for them (like described above and try to expand the found 3-number-sequences).
here's how I would approach this.
The main algorithm could search for triplets then try to extend them to as long a sequence as possible.
This leaves us with the subproblem of finding triplets. So if you are scanning through a string to look for fibonacci numbers, one thing you can take advantage of is that the next number must have the same number of digits or one more digit.
e.g. if you have the string "987159725844" and are considering "[987]159725844" then the next thing you need to look at is "987[159]725844" and "987[1597]25844". Then the next part you would find is "[2584]4" or "[25844]".
Once you have the 3 numbers you can check if they form an arithmetic progression with C - B == B - A. If they do you can now check if they are from the fibonacci sequence by seeing if the ratio is roughly 1.6 and then running the fibonacci iteration backwards down to the initial conditions 1,1.
The overall algorithm would then work by scanning through looking for all triples starting with width 1, then width 2, width 3 up to 6.
I'd say you should first find all interesting Fibonacci items (which, having 6 or less digits, are no more than 30) and store them into an array.
Then, loop every position in your input string, and try to find upon there the longest possible Fibonacci number (that is, you must browse the array backwards).
If some Fib number is found, then you must bifurcate to a secondary algorithm, consisting of merely going through the array from current position to the end, trying to match every item in the following substring. When the matching ends, you must get back to the main algorithm to keep searching in the input string from the current position.
None of these two algorithms is recursive, nor too expensive.
update
Ok. If no tables are allowed, you could still use this approach replacing in the first loop the way to get the bext Fibo number: Instead of indexing, apply your formula.

n-place mastermind variation algorithm

A few days ago I came across such a problem at the contest my uni was holding:
Given the history of guesses in a mastermind game using digits instead
of colors in a form of pairs (x, y) where x is the guess and y is how
many digits were placed correctly, guess the correct number. Each
input is guaranteed to have a solution.
Example for a 5-place game:
(90342, 2)
(70794, 0)
(39458, 2)
(34109, 1)
(51545, 2)
(12531, 1)
Should yield:
39542
Create an algorithm to correctly guess the result in an n-place
mastermind given the history.
So the only idea I had was to keep the probability of each digit being correct based on the correct shots in a given guess and then try to generate the most possible number, then the next one and so on - so for example we'd have 9 being 40% possible for the first place (cause the first guess has 2/5=40% correct), 7 being impossible and so on. Then we do the same for other places in the number and finally generate a number with the highest probability to test it against all the guesses.
The problem with this approach, though, is that generating the next possible number, and the next, and so on (as we probably won't score a home run in the first try) is really non-trivial (or at least I don't see an easy way of implementing this) and since this contest had something like a 90 minute timeframe and this wasn't the only problem, I don't think something so elaborate was the anticipated approach.
So how could one do it easier?
An approach that comes to mind is to write a routine that can generally filter an enumeration of combinations based on a particular try and its score.
So for your example, you would initially pick one of the most constrained tries (one of the ones with a score of 2) as a filter and then enumerate all combinations that satisfy it.
The output from that enumeration is then used as input to a filter run for the next unprocessed try, and so on, until the list of tries is exhausted.
The candidate try that comes out of the final enumeration is the solution.
Probability does not apply here. In this case a number is either right or wrong. There is no "partially right".
For 5 digits you can just test all 100,000 possible numbers against the given history and throw out the ones where the matches are incorrect. This approach becomes impractical for larger numbers at some point. You will be left with a list of numbers that meet the criteria. If there is exactly one in the list, then you have solved it.
python code, where matches counts the matching digits of its 2 parameters:
for k in range(0,100000):
if matches(k,90342)==2 and matches(k,70794)==0 and matches(k,39458)==2 and matches(k,34109)==1 and matches(k,51545)==2 and matches(k,12531):
print k
prints:
39542

What can be a possible algorithm for this combinatorics based program?

So, this contest is already over.
I was trying to solve this problem: http://codeforces.com/contest/554/problem/C
I spent like 1 hour to solve this problem. What I thought was, fill the last n positions of the array with one ball of each kind. Then, in the remaining positions, find the permutations by calculating remaining places in array divided by balls of each kind - 1 (since one is fixed at last position). This will obviously miss out on a lot of test cases, since I don't consider cases when 2 largest numbers will be together in the end or 3 largest numbers will be there. Similarly, along with 4 numbers, other similar numbers might be there before them. But I mean, I am not able to think of a approach how should I solve this?
Any inputs will be greatly appreciated. Thanks!
Also, the contest has already ended, so no issues there. :)
Hints
Consider the example given where we have 1,2,3,4 balls of each colour.
Place the first ball: 1 option.
Now consider placing the 2 balls of the next colour. Place one at any position (2 choices - either before or after the first ball), then place the second at the end.
Now consider placing the 3 balls of the next colour. Place two at any position C(1+2+2,2), and the last at the end.
Finally consider placing the 4 balls of the final colour. Place three at any position C(1+2+3+3,3), and the last at the end.
This gives 1680 choices.

Generate "Valid" Numbers

I was asked the following question in an interview. I don't have any idea about how to solve this. Any suggestions?
Given the start and an ending integer as user input,
generate all integers with the following property.
Example:
123 , 1+2 = 3 , valid number
121224 12+12 = 24 , valid number
1235 1+2 = 3 , 2+3 = 5 , valid number
125 1+2 <5 , invalid number
A couple of ways to accomplish this are:
Test every number in the input range to see if it qualifies.
Generate only those numbers that qualify. Use a nested loop for the two starting values, append the sum of the loop indexes to the loop indexes to come up with the qualifying number. Exit the inner loop when the appended number is past the upper limit.
The second method might be more computationally efficient, but the first method is simpler to write and maintain and is O(n).
I don't know what the interviewer is looking for, but I would suspect the ability to communicate is more important than the answer.
The naive way to solve this problem is by iterating the numbers in the set range, parsing the numbers into a digit sequence and then testing the sequence according to the rule. There is an optimization in that the problem essentially asks you to find fibonnaci numbers so you can use two variables or registers and add them sequentially.
It is unclear from your question whether the component numbers have to have the same number of digits. If not, then you will have to generate all the combinations of the component number arrangements.

Resources