Page 1 of 1
Recursion problem\Problema de Recursão
Posted: Wed Sep 08, 2010 6:52 pm
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
Re: Recursion problem\Problema de Recursão
Posted: Wed Sep 08, 2010 10:29 pm
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
Re: Recursion problem\Problema de Recursão
Posted: Thu Sep 09, 2010 3:35 am
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
Re: Recursion problem\Problema de Recursão
Posted: Thu Sep 09, 2010 3:50 am
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
Re: Recursion problem\Problema de Recursão
Posted: Thu Sep 09, 2010 9:56 am
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 )))
Re: Recursion problem\Problema de Recursão
Posted: Thu Sep 09, 2010 11:05 am
by Turley
when you fixed your coding problems the next problem will be:
pol.cfg MaxCallDepth
Re: Recursion problem\Problema de Recursão
Posted: Thu Sep 09, 2010 11:08 am
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 ?
Re: Recursion problem\Problema de Recursão
Posted: Thu Sep 09, 2010 11:58 am
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
Re: Recursion problem\Problema de Recursão
Posted: Fri Sep 10, 2010 8:22 am
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
Re: Recursion problem\Problema de Recursão
Posted: Fri Sep 10, 2010 10:06 am
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..
Re: Recursion problem\Problema de Recursão
Posted: Fri Sep 10, 2010 10:50 am
by guialtran
I'll try to make an interactive
Re: Recursion problem\Problema de Recursão
Posted: Fri Sep 10, 2010 10:50 am
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)},.... }
Re: Recursion problem\Problema de Recursão
Posted: Fri Sep 10, 2010 12:13 pm
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);
Re: Recursion problem\Problema de Recursão
Posted: Sat Sep 11, 2010 7:37 am
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