lundi 27 avril 2020

Dichotomy to find unkown number with success/fail instead of more or less

I'm trying to translate a program from Python to LSL (SecondLife scripting) to find a number I can't access from functions. This number is my linden balance (the money in game.) The only way I can determinate that amount from my script is by transferring amounts of money to myself and to check if the paiement was successful or not.

In order to do so, I found a dychotomy python script that works perfectly :

from random import randint

n = 100000000 # Maximum amount possible
amountToFind = randint(1,n)
a = 1
b = n
nbAnswers = 0

while b-a >=0: 
    answer= (a+b)//2
    nbAnswers += 1
    if answer== amountToFind:
        print("The correct answer is:", answer)
        break 
    elif answer> amountToFind:
        b = answer- 1
    else:
        a = answer+ 1

print("The number of steps to get the correct amount was :", nbAnswers)
print(amountToFind)

The problem is that I can't compare answerto the number i'm looking for. I only have fail and success :

llCheckLindenBalance()
{
        rep = (a+b)/2;
        transactionId = llTransferLindenDollars(owner, rep);
}

touch_start(integer total_number)
{
    llCheckLindenBalance();
}

transaction_result(key id, integer success, string data)
{
    if(id != transactionId)
        return;



    if(success) // rep < amount or rep == amount
    {

        a = rep+1;
        llOwnerSay("Last amount " +(string)rep);
        llCheckLindenBalance();

    }
    else // FAIL - rep < amount
    {
       b = rep-1;
       llCheckLindenBalance();
    }
}

That script works so far, but it will never stop as it nevers knows when to stop. I was thinking about comparing a and b but the space between them is variable as well (from 0 to 10). So i'm stuck at it. I thought about testing answer+1 and if it fail it means it's the highest amount but I can't now where to test it.

Do you guys have any idea ?




Aucun commentaire:

Enregistrer un commentaire