Recursion problem\Problema de Recursão

Here you can post threads specific to the current release of the core (099)
Post Reply
roquettedu
New User
Posts: 6
Joined: Wed Jul 28, 2010 4:24 pm

Recursion problem\Problema de Recursão

Post by roquettedu »

Greetings everyone.

I had a problem about recursion problem with my POL99, so when i start it, it just go from 0 to 98 and i don't know why it stops running. Does someone know how can I fix that ? Is there any limit for recursion?

Thanks for you attetion.

Eu tive um problema no meu POL99 sobre recursão, meu pol trava no momento que chega ao 99.
Alguem poderia me dizer se tem algum limite pra recursão ou como eu poderia arrumar isso ?

Grato.

Code: Select all

program quicksort(character)

repercusiontest(0)  ;

endprogram

function repercusiontest((ByRef accumulator)
    sleepms(1);
    print("-> "+acumulador);
    repercusiontest((accumulator+ 1);
    return 1;
endfunction
User avatar
atreiu
Grandmaster Poster
Posts: 151
Joined: Mon May 24, 2010 1:08 pm

Re: Recursion problem\Problema de Recursão

Post by atreiu »

actually if this is your real code then this never been started cause never been compiled.

program quicksort(character)

repercusiontest(0) ;

endprogram

function repercusiontest((ByRef accumulator) // unnecessary first "("
sleepms(1);
print("-> "+acumulador); // acumulador what is this? you dont declare this variable above.
repercusiontest((accumulator+ 1); // unnecessary first "("
return 1;
endfunction

little bit strange realisation =) i never do cyclic code before because think this needless
guialtran
Grandmaster Poster
Posts: 120
Joined: Wed Jul 30, 2008 12:42 pm

Re: Recursion problem\Problema de Recursão

Post by guialtran »

Greetings everyone.

I had a problem about recursion problem with my POL99, so when i start it, it just go from 0 to 98 and i don't know why it stops running. Does someone know how can I fix that ? Is there any limit for recursion?

Thanks for you attetion.

Code: Select all

use attributes;
use basic;
use boat;
use cfgfile;
use cliloc;
use datafile;
use file;
use guilds;
use http;
use math;
use npc;
use os;
use polsys;
use storage;
use unicode;
use uo;
use util;
use vitals;


program quicksort(character)
    testeDeRecursao(0);
endprogram

function testeDeRecursao(ByRef acumulador)
    sleepms(10);
    print("-> "+acumulador);
    testeDeRecursao(acumulador + 1);
    return 1;
endfunction
guialtran
Grandmaster Poster
Posts: 120
Joined: Wed Jul 30, 2008 12:42 pm

Re: Recursion problem\Problema de Recursão

Post by guialtran »

quickSort VET > 98 not working

Code: Select all

program quicksort(character)
    var vet := array;
    var i;
    for(i:=99;i>=0;i:=i-1)
        vet.append(i);
    endfor
    logic_quickSort(vet, 1, vet.size());
    print(vet);
endprogram

function logic_quickSort(ByRef lista, ByRef left, ByRef right)
    print("logic_quickSort");
    var r;
    if(right > left)
        r := logic_partition(lista, left, right);
        logic_quickSort(lista, left, r - 1);
        logic_quickSort(lista, r + 1, right);
    endif
    return 1;
endfunction

function logic_partition(ByRef lista, ByRef left, ByRef right)
    print("logic_partition");
    sleepms(1);
    var i;
    var j;
    var aux;
    i := left;
    for(j := left + 1; j <= right; j:=j+1)
        if (lista[j] < lista[left])
            i := i+1;
            aux := lista[i];
            lista[i] := lista[j];
            lista[j] := aux;
        endif
    endfor
    aux := lista[i];
    lista[i] := lista[left];
    lista[left] := aux;
    return i;
endfunction
User avatar
atreiu
Grandmaster Poster
Posts: 151
Joined: Mon May 24, 2010 1:08 pm

Re: Recursion problem\Problema de Recursão

Post by atreiu »

ooh man =)) is this joke?! did you realy need in this??? explain please ) :?:

>> when i start it, it just go from 0 to 98 and i don't know why it stops running.
i suppose this stop running on 98 cause you set j:=2 >> "for(j := left + 1" in logic_partition()
but right.size() == 99;
AND
r := logic_partition(lista, left, right); will return 98

i think it is better to run on sportbike or be drunk on barbecue with girlfriend than write this code )))
Last edited by atreiu on Thu Sep 09, 2010 11:16 am, edited 1 time in total.
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: Recursion problem\Problema de Recursão

Post by Turley »

when you fixed your coding problems the next problem will be:
pol.cfg MaxCallDepth
guialtran
Grandmaster Poster
Posts: 120
Joined: Wed Jul 30, 2008 12:42 pm

Re: Recursion problem\Problema de Recursão

Post by guialtran »

Turley wrote:when you fixed your coding problems the next problem will be:
pol.cfg MaxCallDepth
Thank Turley, his answer is always the best!

what is the maximum amount I could put?
MaxCallDepth= X ?
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: Recursion problem\Problema de Recursão

Post by Turley »

its unsigned int so ~65k
but generally no good idea with one poor written script you can easily crash your server this way, due to memory usage.
Each call depth means pol has to store the locals of the current function before it "switches" to the next...
So you need a good reason, sorting is not the best:
a) pol has a sort function
b) did you know that a dictionary automatically sorts his keys? so why not write a function which stores the sort index/info as the key and if any additional infos are needed as dict value. After this simply store it back as an array and its sorted
guialtran
Grandmaster Poster
Posts: 120
Joined: Wed Jul 30, 2008 12:42 pm

Re: Recursion problem\Problema de Recursão

Post by guialtran »

var d := struct;//or dictionary

var i;
for(i:=10;i>=0;i:=i-1)
sleepms(1);
d.insert(cstr(i), i);
endfor

SendSysMessage( character, cstr(d.keys()));

//out 0,1,10,2,3,4,5,6,7,8,9
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: Recursion problem\Problema de Recursão

Post by Turley »

var d := dictionary;
var i;
for(i:=10;i>=0;i-=1)
sleepms(1);
d:=i;
endfor
print(d.keys());

var a:={"a","c","b"};
d:=dictionary;
foreach b in a
sleepms(1);
d:=b;
endforeach
print(d.keys());

a.sort()
print(a);

Conclusion:
for such simple stuff there is no need to script a sorting function, it only makes sense when you eg need to sort a multidimensional array by given index.

p.s.: the sorting of your example is absolute correct you converted the integer into strings and there follows 10 after 1..
guialtran
Grandmaster Poster
Posts: 120
Joined: Wed Jul 30, 2008 12:42 pm

Re: Recursion problem\Problema de Recursão

Post by guialtran »

I'll try to make an interactive
Last edited by guialtran on Fri Sep 10, 2010 10:51 am, edited 1 time in total.
guialtran
Grandmaster Poster
Posts: 120
Joined: Wed Jul 30, 2008 12:42 pm

Re: Recursion problem\Problema de Recursão

Post by guialtran »

I do not think I was clear.
and my english sucks.
I wanted to order numbers associated with an object.
array{ { int , object } , { int , object } ......... }
ou
array{ { skill , character } , { skill , character } ......... }

Using a solution structure is

Code: Select all

function insere(ByRef ostruct, ByRef tam , ByRef chave, ByRef valor)
    chave := cstr(chave);
    if(Len(chave)>tam)
        return 0;
    endif
    var i;
    for(i:=Len(chave);i<tam;i:=i+1)
        chave := "0"+chave;
    endfor
    ostruct.insert(chave,valor);
    return 1;
endfunction

program prog(character)
var d:= dictionary;
insere(d, 5 , 4444, 4444);
insere(d, 5 , 1, 1);
insere(d, 5 , 333, 333);
insere(d, 5 , 22, 22);
SendSysMessage( character, cstr(d.keys()));
//out 00001, 00022, 00333, 04444

the problem is to order a set, you should know what is the biggest key value

Another idea would be an array of values, the index of the vector represents the precision.

example

matriz ->
a == vector
a{ dictionary{0...9 == (len(key)==1) }, dictionary{10..99 == (len(key)==2) }, dictionary{100...999== (len(key)==3)},.... }

or
a==dictionary "len(...)"
a{ dictionary{0...9 == (len(key)==1) }, dictionary{10..99 == (len(key)==2) }, dictionary{100...999== (len(key)==3)},.... }
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: Recursion problem\Problema de Recursão

Post by Turley »

guialtran wrote: I wanted to order numbers associated with an object.
array{ { int , object } , { int , object } ......... }
ou
array{ { skill , character } , { skill , character } ......... }
I think you didnt tried my examples :)

ok lets see

Code: Select all

var unsortedarray:={ {10, "somedata"}, { 1, "moredata"}...};

ForEach element in unsortedarray
    var key:=element[1];
    If (!dict.exists(key))
      dict[key] := array;
    EndIf
    dict[key].append(_element_iter);
    SleepMS(2);
EndForEach
var sortedarray:=array;
ForEach key in (dict.keys())
   ForEach element in (dict[key])
      sortedarray.append(unsortedarray[element]);
      SleepMS(2);
   EndForEach
EndForEach

print(sortedarray);
guialtran
Grandmaster Poster
Posts: 120
Joined: Wed Jul 30, 2008 12:42 pm

Re: Recursion problem\Problema de Recursão

Post by guialtran »

I understand the difference of using an integer key and a key string!
Thank you.

I modified your code, I think now he is working.

Code: Select all


program prog(character)

    var unsortedarray:=array;
    var i;
    for(i:=100;i>=0;i:=i-1)//1000000
        sleepms(1);
        unsortedarray.append({i,cstr(i)});
    endfor
    for(i:=100;i>=0;i:=i-1)//1000000
        sleepms(1);
        unsortedarray.append({i,cstr(i)});
    endfor
    for(i:=100;i>=0;i:=i-1)//1000000
        sleepms(1);
        unsortedarray.append({i,cstr(i)});
    endfor
    var dict := dictionary;//
    ForEach element in unsortedarray;
        var key:=element[1];
        if(!dict.exists(key))
            dict[key] := array;
        endif
        dict[key].append(element[2]);
        sleepms(1);
    EndForEach
    var sortedarray:=array;
    ForEach key in (dict.keys())
        ForEach element in (dict[key])
            sortedarray.append({key,element});
            sleepms(1);
        EndForEach
    EndForEach
    print(sortedarray);
    SendSysMessage( character, cstr("ok"));
endprogram

Post Reply