Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Is it possible to reduce repetitive permutation-based if-statements?

Hello I'm working on this math game and for my last scene I did what feels like a lot of repetitive code but I'm not sure if there's a way to simplify it, I have linked below so maybe some more seasoned programmers might have some more elegant solutions! For example, Im trying to generate every permutation of something like (a[]b)²[]c[]d where the brackets will be replaced by +,-,*, or /. What I have been doing is just creating random percent if statements to pick a specific version like "(a+b)²/c-d" Is there possibly a less "brute-force" and readable approach then what I have been doing?

if(UnityEngine.Random.Range(0,101)>50){
        // 50% of being (a+b)²(c)+d
    if(UnityEngine.Random.Range(0,101)>50){
        ans = ((int) Mathf.Pow((float) a+ (float) b, 2))*c+d;
        input.text = "("+a+"+"+b+")"+"²"+"("+c+")"+"+"+d+"=";
        Debug.Log("Problem ID: 78");
        // 50% of being (a+b)²(c)-d
    } else {
        ans = ((int) Mathf.Pow((float) a+ (float) b, 2))*c-d;
        input.text = "("+a+"+"+b+")"+"²"+"("+c+")"+"-"+d+"=";
        Debug.Log("Problem ID: 79");
    }
    // 50% of being (a-b)²(c)[]d
} else {
    // 50% of being (a-b)²(c)+d
    if(UnityEngine.Random.Range(0,101)>50){
        ans = ((int) Mathf.Pow((float) a- (float) b, 2))*c+d;
        input.text = "("+a+"-"+b+")"+"²"+"("+c+")"+"+"+d+"=";
        Debug.Log("Problem ID: 80");
        // 50% of being (a-b)²(c)-d
    } else {
        ans = ((int) Mathf.Pow((float) a- (float) b, 2))*c-d;
        input.text = "("+a+"-"+b+")"+"²"+"("+c+")"+"-"+d+"=";
        Debug.Log("Problem ID: 81");
    }

(Pastebin below for more context) https://pastebin.pl/view/d1bfb99e

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I applaud your desire to make your code more readable. The basic idea is to split (a) defining, (b) choosing and (c) applying your operators.

  • Step 1: You define Operators. Each Operator combines both a mathematical operation (e.g. Add would be (a, b) => a + b) and a symbol (e.g. Add would be "+").

    class Operator
    {
        public Func<int, int, int> Calculate { get; }
        public string Symbol { get; }
    
        public Operator(Func<int, int, int> calculate, string symbol)
        {
            Calculate = calculate;
            Symbol = symbol;
        }
    }
    
    private Operator Add = new Operator((a, b) => (a + b), "+");
    private Operator Subtract = new Operator((a, b) => (a - b), "-");
    
  • Step 2: Then you randomly choose your operators (I used System.Random, since I'm not familiar with Unity, but feel free to replace it with the random number generator of your choice):

    var rnd = new Random();
    
    private (Operator op1, Operator op2, int problemId) RandomlyChooseProblem()
    {
        switch (rnd.Next(4))
        {
            case 0: return (Add, Add, 78);
            case 1: return (Add, Subtract, 79);
            case 2: return (Subtract, Add, 80);
            case 3: return (Subtract, Subtract, 81);
            default: throw new InvalidOperationException("This should not happen.");
        }
    }
    
  • Step 3: You apply them:

    var (op1, op2, problemId) = RandomlyChooseProblem();
    
    ans = op2.Calculate((int)Math.Pow(op1.Calculate(a, b), 2) * c, d);
    input.text = $"(a{op1.Symbol}b)²*c{op2.Symbol}d");
    Debug.Log($"Problem ID: {problemId}");
    

Adding a new operator (e.g. Multiply) or a new problem variant (e.g. (Add, Multiply, 82)) is now just a single line of code.

over 4 years ago · Santiago Trujillo Report

0

Break the calculation into parts - a±b, square * c and ±d. Calculate them separately, and multiply them to get the final result. For the text, you can use string interpolation

float ans;
string operator1;
string operator2;
if (UnityEngine.Random.Range(0,101)>50) {
    ans = (float) a + (float) b;
    operator1 = "+";
} else {
    ans = (float) a - (float) b;
    operator1 = "-";
}
ans = (int)(ans * ans) * c;
if (UnityEngine.Random.Range(0,101)>50) {
    ans += d;
    operator2 = "+";
} else {
    ans -= d;
    operator2 = "-";
}
input.text = $"(a{operator1}b)²(c){operator2}d";

Also note that UnityEngine.Random.Range(0,101) > 50 is not exactly 50% probability. You probably meant UnityEngine.Random.Range(1,101) > 50 instead, but I would just use UnityEngine.Random.Range(0,2) == 0.

The problem IDs can be flattened by generating 2 random bits, and adding the 2-bit number encoded by those bits to 78:

float ans;
string operator1;
string operator2;
int random1 = UnityEngine.Random.Range(0,2);
int random2 = UnityEngine.Random.Range(0,2);
if (random1 == 0) {
    ans = (float) a + (float) b;
    operator1 = "+";
} else {
    ans = (float) a - (float) b;
    operator1 = "-";
}
ans = (int)(ans * ans) * c;
if (random2 == 0) {
    ans += d;
    operator2 = "+";
} else {
    ans -= d;
    operator2 = "-";
}
int problemID = 78 + random1 + random2 * 2;
Debug.Log($"Problem ID: {problemID}");
input.text = $"(a{operator1}b)²(c){operator2}d";

This trick is not particularly readable IMO though.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!