Hello! It is very embarrassing to come here with such questions, but curiosity wins. I recently tried to get an interview for training and did not pass, but I still have an interest in the task. Task: 0 and 1 are alternately input to the console via the console. The program outputs Yes to the console if an even number of 0 and an odd number of 1 has arrived, in other cases No. Actually here is the CODE that I wrote. Naturally not the correct implementation of the spacecraft.
- oneWelcome to StackOverflow in English. 1. What exactly is your question? 2. If you have a problem with the code, we have decided to put the minimum reproducible example directly into the question. - Nofate ♦
- Sorry, next time I will try to do just that, but here the case was such that I had to make a link to the repository of my code, because there was no mistake in my code, it did not reflect the essence of the task, and putting the full code here would take too much space - Nikita Titov
1 answer
(I’ll just make a reservation that I don’t have a profile education, and the terminology suffers)
The state machine does not in itself store the history of operations with it, it is just a set of states with rules for the transition from one to another, and at a certain point in time the automaton is represented simply by one state. Your solution (most likely) did not come up, because you used logic for calculations and built a calculator instead of implementing the automatic machine itself.
From you, most likely, like a machine with four states:
- Received an even number of zeros, an even number of ones
- Obtained an odd number of zeros, an even number of ones
- An even number of zeros, an odd number of ones
- An odd number of zeros was received, an odd number of ones
Or even easier:
состояние = (четное/нечетное количество нулей, четное/нечетное количество единиц) = (bool, bool) In this case, it is possible to define transitions between states.
(четное, четное) -> получена единица -> (четное, нечетное) There is no difference in the final result, but at the same time the automaton itself begins to exist as a separate unit and perform its specific functions. It remains only to implement it; in view of the simplicity of logic, it is not necessary to describe transitions:
public class FiniteStateMachine { private boolean evenAmountOfZerosProcessed = true; private boolean evenAmountOfOnesProcessed = true; public void accept(int nextNumber) { switch (nextNumber) { case 0: evenAmountOfZerosProcessed = !evenAmountOfZerosProcessed; break; case 1: evenAmountOfOnesProcessed = !evenAmountOfOnesProcessed; break; default: throw new IllegalArgumentException("Unexpected input: " + nextNumber); } } public State getState() { if (evenAmountOfZerosProcessed && !evenAmountOfOnesProcessed) { return State.CORRECT; } return State.INVALID; } enum State { CORRECT, INVALID } } In a good state and possible transitions should be expressed in a separate enum, but, most likely, the above implementation would be enough.
- Everything is correctly written, I will add only an explanation. A finite state machine is therefore called finite - because it has a finite number of states . In this case - 4. And the automaton of the author of the question had (in theory) an infinite number of states, since the fields
numberOfZeroandnumberOfOnecan grow indefinitely. - Pavel Mayorov - By the way, QA in Java is conveniently done through abstract enumerations (Enum). - Pavel Mayorov
- Thank you very much! I will parse the code)) - Nikita Titov
- Excuse me, can I by inexperience cannot find differences in your example and my example. Please tell me what I was wrong? - Nikita Titov
- @ NikitaTitov I wrote that the input-output conditions are the same, and the difference in the concept itself. KA is a set of states, transitions and rules of these transitions (and nothing more), you have a battery that counts the state and is not itself an automaton (more precisely, it is, but in the above Paul formulation) directly reads something (he should not do this). The automaton in the current state is exactly the tuple from (the oddness of the processed number of units, the parity of the processed number of zeros), and nothing more. - etki