| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Message
| Your Lua code was:
function ColourNote(a, b , c)
end
ColourNote("red", "black", "%1")
buffer = "craft"
words = {" "}
n=0
local w
wordcount=0
for w in string.gmatch("%1", "%a+") do
wordcount=wordcount+1
words[wordcount]=w
end
s=0
for i=1,wordcount,1 do
s=bit.bor(s,1)
s=bit.shl(s,1)
ColourNote("red", "black", words[i])
end
s=s+1
copys=s
x=0
hasword=0
wordsums={"0"}
wordvals={"0"}
for i=1,copys+1,1 do
x=i
j=1
while x do
if bit.band(x,1) then
hasword=1
buffer=buffer .. string.lower(words[j])
buffer=buffer .. " "
j=j+1
end
end
if hasword then
ColourNote("red", "black", buffer)
end
hasword=0
end
I can't help thinking there is an easier way. :)
After a bit of thought, I believe a recursive function is the way to go.
My idea was, to initially show all ingredients. Then, omit one, and show the remaining ones (so in your case, there are 4 lines with only 3 ingredients). Now for each of the 3 ingredients, you recurse, so that you now omit one of the 3, showing a line of 2 ingredients, and so on, till you are down to one.
To stop duplicates a simple table of "done" stops you repeating things (eg. the last single ingredient would keep appearing often).
My example code is:
ingredients = { "flour", "eggs", "sugar", "salt" }
require "copytable"
done = {}
function permute_ingredients (t)
local result = table.concat (t, ", ")
if not done [result] then
print (result)
done [result] = true
end -- if not shown this one yet
-- if more than 1 item, recurse
if #t > 1 then
for i = 1, #t do
local t2 = copytable.shallow (t)
table.remove (t2, i) -- get rid of one of them
permute_ingredients (t2)
end -- for
end -- if
end -- function permute_ingredients
permute_ingredients (ingredients)
Running this gives:
flour, eggs, sugar, salt
eggs, sugar, salt
sugar, salt
salt
sugar
eggs, salt
eggs
eggs, sugar
flour, sugar, salt
flour, salt
flour
flour, sugar
flour, eggs, salt
flour, eggs
flour, eggs, sugar
This is 15 lines, which sounds about right for 4 ingredients.
If I add another ingredient (yeast) I get 31 lines, which also sounds right (2^5 - 1):
flour, eggs, sugar, salt, yeast
eggs, sugar, salt, yeast
sugar, salt, yeast
salt, yeast
yeast
salt
sugar, yeast
sugar
sugar, salt
eggs, salt, yeast
eggs, yeast
eggs
eggs, salt
eggs, sugar, yeast
eggs, sugar
eggs, sugar, salt
flour, sugar, salt, yeast
flour, salt, yeast
flour, yeast
flour
flour, salt
flour, sugar, yeast
flour, sugar
flour, sugar, salt
flour, eggs, salt, yeast
flour, eggs, yeast
flour, eggs
flour, eggs, salt
flour, eggs, sugar, yeast
flour, eggs, sugar
flour, eggs, sugar, salt
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|