Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function permutWithRepeat(string)
- string = string:lower()
- math.randomseed(os.time())
- letters, combinations = {}, {}
- number = string:len()
- for i = 1, number do
- table.insert(letters, string.sub(string, i,i))
- end
- function getRepsTable(tab)
- local elements = {}
- for i, v in pairs(tab) do
- if not(elements[v])then
- elements[v] = 1
- else
- elements[v] = elements[v] + 1
- end
- end
- local x = 1
- for _, j in pairs(elements) do
- x = x * j
- end
- return x
- end
- function getFatorial(num)
- local x = 1
- for i = num, 1, -1 do
- x = x * i
- end
- return x
- end
- function getFatorialReps(num, letras)
- if(getRepsTable(letras) > 1)then
- return 0
- end
- return (getFatorial(num)/getFatorial(getRepsTable(letras)))
- end
- function isInTable(table, element)
- for i, v in pairs(table)do
- if(rawequal(v, element))then
- return true
- end
- end
- return false
- end
- function randomizeTable(tabela)
- local num = #tabela
- local sequence, newTable = {}, {}
- while not rawequal(#sequence, #tabela) do
- local random = math.random(num)
- if not(isInTable(sequence, random))then
- table.insert(sequence, random)
- end
- end
- for i, v in pairs(sequence) do
- rawset (newTable, i, tabela[v])
- end
- return {concat=table.concat(newTable,""), tab = newTable}
- end
- print("O numero de anagramas é: "..getFatorialReps(number, letters)..".\n")
- if(rawequal(#combinations, getFatorialReps(number, letters)))then
- print("Não existem combinações sem repetição para essa palavra.")
- else
- print("São eles(isso pode demorar um pouco):\n")
- end
- collectgarbage ("collect")
- while true do
- local str = randomizeTable(letters)
- if not(isInTable(combinations, str.concat))then
- if(tonumber(getRepsTable(str.tab)) == 1)then
- table.insert(combinations, str.concat)
- end
- end
- if(rawequal(#combinations, getFatorialReps(number, letters)))then
- break
- end
- end
- collectgarbage ("collect")
- table.sort(combinations)
- print(table.concat(combinations, "\n"))
- print("Tempo decorrido: "..os.clock().." segundos.")
- end
- permutWithRepeat("abcdef")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement