Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redundant Iterations
#1
OrgH = [73.35,111.37,92.70,70.73,67.71,77.84]
BtmGap = 32
MinRH = 12
Div = 7
tH = 400

sH = sum(OrgH)
q = Div - len(OrgH)

def Optm(S, H, GH, q):

    s = H + GH

    L = [S,GH]

    if S >= s:
        K = S - s
    if S < s and GH > MinRH / 12:
        K = Optm(S, H, GH - 1, q)
    if S < s and GH <= MinRH / 12:
        K = Optm(S + 1, H, GH, q)


    if q < 0:
        print("Somethign wrong with the category!")
    if q == 0:
        return 0

    else:
        minGap = K / q

        if minGap < MinRH:
            print("b")
            return Optm(S + 1, H, GH, q)
        else:
            print("a")
            print(S)
            print(GH)
            print(minGap)
            return minGap
In the script above, my goal is to make the last "else" executes once and get only one result. But, now, it generates several iterations.

Can someone help? Thank you in advance!
Reply
#2
It's not clear, because you haven't shown us how you call Optm in the first place. But if you only want the else clause to execute if none of the if clauses do, then the middle if's all need to be elif's (short for else if).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Mar-13-2019, 07:11 PM)ichabod801 Wrote: It's not clear, because you haven't shown us how you call Optm in the first place. But if you only want the else clause to execute if none of the if clauses do, then the middle if's all need to be elif's (short for else if).

OrgH = [73.35,111.37,92.70,70.73,67.71,77.84]
BtmGap = 32
MinRH = 12
Div = 7
tH = 400

sH = sum(OrgH)
q = Div - len(OrgH)

def Optm(S, H, GH, q):

    s = H + GH

    if S >= s:
        K = S - s
    if S < s and GH > MinRH / 12:
        K = Optm(S, H, GH - 1, q)
    if S < s and GH <= MinRH / 12:
        K = Optm(S + 1, H, GH, q)


    if S == 494:
        print(K)
        print(K/q)
        print(s)
        print(S-s)
        print(GH)

    if q < 0:
        print("Somethign wrong with the category!")
    if q == 0:
        return 0

    else:
        minGap = K / q

        if minGap < MinRH:
            return Optm(S + 1, H, GH, q)
        if minGap >= MinRH and S <= s:
            return Optm(5 + 2, H, GH, q)
        else:
            return minGap




L = Optm(tH, sH, BtmGap, q)
sorry, I missed one line.
Reply
#4
I don't see an obvious solution, and your code is totally opaque (no documentation, non-descriptive variable names). The only thing I note at this point is that line 40 should maybe be return Optm(S + 2, H, GH, q) instead of Optm(5 + 2, H, GH, q) (that is, ess not five).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020