Ir para conteúdo
Entre para seguir isso  
pejuge2

7 Funções

Recommended Posts

pejuge2    1
pejuge2

7 Funções

 

Autor: PeJuGe

 

Comentário: [spoiler=Comentário]Estas são funções que eu já postei em sua maioria e estou postando novamente mas agora melhoradas, sendo nova apenas math.factorial.

 

Não explicarei, apenas postarei o script. Caso tenha alguma dúvida consulte o índice no início do script, em caso de persistência da dúvida poste-a abaixo.

 

Script:

--[[ PeJuGe(2010) . 7 Funções
    DON'T REMOVE THE CREDITS
    Exclusive For www.otserv.com.br
    More Info: http://forums.otserv.com.br/showthread.php?p=1037724#post1037724
    + string.toup(txt)
      * Returns the first letter upped.
    + getlocation(table, m)
      * Returns the location of a value in a table
    + math.textreme(table, value)
      * Returns the maximum and minimum value
    + math.multiple(value, param)
      * Returns true or false
    + math.tmultiple(table, param)           
      * Returns the multiples of param from table
    + math.approx(value)
      * Returns the nearest number
    + math.factorial(value)
      * Returns the factored number
]]--       

function string.toup(txt)
  return string.upper(txt:sub(1, 1)) .. txt:sub(2) 
end                                                                      

function getlocation(table, m, r)
  r = r or 1
  return #table >= r and table[r] ~= m and getlocation(table, m, r + 1) or table[r] == m and r
end

function math.textreme(table, value, r)
  r = r or {math.huge, -math.huge, 1}
  r[1], r[2], r[3] = table[r[3]] < r[1] and table[r[3]] or r[1], table[r[3]] > r[1] and table[r[3]] or r[2], r[3] + 1
  return r[3] == #table + 1 and {min = r[1], max = r[2]} or math.textreme(table, value, r)                                                                            
end    

function math.multiple(value, param)
  return value % param == 0
end

function math.tmultiple(table, param, r) 
  r = r or {{}, 1}
  r[1][#r[1] + 1], r[2] = math.multiple(table[r[2]], param) and table[r[2]] or nil, r[2] + 1
  return r[2] == #table + 1 and r[1] or math.tmultiple(table, param, r)    
end  

function math.approx(value)
  return math.floor(value + 0.5) > math.floor(value) and math.ceil(value) or math.floor(value) 
end  

function math.factorial(n)    
    return math.floor(n) > 1 and (math.floor(n) * math.factorial (n-1)) or 1        
end

Bom, obrigado por ler meu tópico. Caso tenha alguma dúvida, reclamação ou sugestão, favor postar abaixo, pois sua dúvida pode pertencer também a outra pessoa.

 

Atenciosamente,

PeJuGe

Editado por pejuge2

Compartilhar este post


Link para o post
Gpwjhlkdcf    21
Gpwjhlkdcf

Movido.

Compartilhar este post


Link para o post
Mock    32
Mock

Nem tinha visto esse topico. :P

eu ja tinha feito uma math.factorial, ao inves de vc que usou recursividade eu usei um for

Compartilhar este post


Link para o post
pejuge2    1
pejuge2

Hehehe, recursividade reduz bastante o script ^^. Mas em compensação para números muito altos ela provoca erro por estar formando um "loop" para o interpretador, o que realmente não acontece :/. Neste caso não acontece porque não retoma a função em si, apenas executa novamente.

Compartilhar este post


Link para o post
Socket    0
Socket
Hehehe, recursividade reduz bastante o script ^^. Mas em compensação para números muito altos ela provoca erro por estar formando um "loop" para o interpretador, o que realmente não acontece :/. Neste caso não acontece porque não retoma a função em si, apenas executa novamente.

 

Concordo, a recursividade dimini bastante o script:

 

function getLocation(table,m,r)
r = r or 1
 return #table >= r and table[r] ~= m and getLocation(table,m,r + 1) or table[r] == m and r
end
arr = {'a','b','c'}
print(getLocation(arr,'c'))

function getlocation(table,m)
 for i,v in ipairs(table) do
   if v == m then
     print(i)
   end
 end
end
arr = {'a','b','c'}
getlocation(arr,'b')

 

Mesma função só que a recursividade fica menor. :yes:

Compartilhar este post


Link para o post
Visitante
Este tópico está impedido de receber novos posts.
Entre para seguir isso  
  • Quem Está Navegando   0 membros estão online

    Nenhum usuário registrado visualizando esta página.

×