W
W
Wet_Dumplings2019-10-12 12:01:05
C++ / C#
Wet_Dumplings, 2019-10-12 12:01:05

How to iterate over an arithmetic formula of arbitrary length?

Hey!
It is necessary to enumerate all formulas of arbitrary length P.
The formula uses the constants 0, 1, 2, the variable X, as well as the operations -, +, * and brackets.
"formula length" is the number of operations i.e. if size No. formulas will be as follows:
1.) 0 + 1; x * 2
4.) x * x + 2 * x + 1; (1 + x) * 1 * 2 + 0
This is how to implement =) ?
ps Apparently, the request was set incorrectly. I didn’t find anything similar to the word at all (unless, of course, we consider the “combination without repetitions” of two declared arrays)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shai_hulud, 2019-10-12
@Wet_Dumplings

You need a Cartesian product of these features.

public bool onlyOneVariable(params string[] values) => values.Count(char.IsLetter) == 1;

from num1 in new[] { '0', '1', '2', 'X' }
from num2 in new[] { '0', '1', '2', 'X' }
from op1 in new[] { '-', '+', '*' }
where onlyOneVariable(num1, num2)
select $"{num1} {op1} {num2}";

Execution example: https://repl.it/repls/TrustyAptInstitution

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question