Forums » Community Projects

15mil Code Bounty

Apr 27, 2012 TheRedSpy link
I have a problem that needs solving, I'm putting a 15mil credit code bounty on its head because I bet one of you guys can write this function in like 5 minutes.

I have a table:

table = {}

My table is dynamically populated with other tables with variables, for instance, my table might, at one given point, look like so:

table = {{name="bob", favmuffin="chocolate"},{name="john", favmuffin="chocolate"},{name="lucy", favmuffin="orangepoppyseed"},{name="kate", favmuffin="chocolate"}}

I need a function that will return the names of people that like the same types of muffins together. So, for the above example:

chocolate: bob, john, kate
orangepoppyseed: lucy

If you can produce this function for me and post it here, the 15mil is yours with my thanks, I will also bundle 1 month of FAMY insurance in too if that floats your boat.

:) TRS
Apr 27, 2012 mr_spuck link
like this ..or what?

function muffins(muffinlovers, type)
  local ret = {}
  for _,v in ipairs(muffinlovers) do
    if v.favmuffin == type then
      table.insert(ret, v.name)
    end
  end
  return ret
end
Apr 27, 2012 TheRedSpy link
Congratulations Mr. Spuck.

Thank you for your help. See me or savet in game for your 15mil and free months travel pass.
Apr 27, 2012 PhoenixVoice link
And just because I am nice here is how to get it to print exactly the output you wanted:

http://pastebin.com/1TEA6KTz

~Interstellar
Apr 27, 2012 TheRedSpy link
Hehe thanks inter, but being the clever redspai that I am, the true purpose of the code is not to discriminate between people based on muffin choice, but actually something else..

In any case, I can take it from here.

Thanks!
Apr 27, 2012 PhoenixVoice link
np, just letting you know how to get exactly what you were looking for and not just a part of it. I know sometimes I can get lost in partial examples that only make sense once I have the actual full version of what I asked for. :-P

~Interstellar
Apr 27, 2012 mr_spuck link
My function is the dumbest way possible to do this btw. It will cause a noticable hickup in the game if you have more than 100k "muffin lovers".
The "type" parameter should probably also be renamed. I forgot that lua has builtin function with that name. It's not really important in this case though.

Interstellar: you could replace the commafication loops with a print("chocolate: "..table.concat(chocolate, ". ")). That would save a few a lines.
Apr 27, 2012 TheRedSpy link
yep i've renamed everything anyway. this way shouldnt be a problem as there usually aren't more than 10 or 20 "muffin lovers" so we should be okay.
Apr 27, 2012 TheRedSpy link
Out of curiosity though.. whats a smarter way?
Apr 27, 2012 PhoenixVoice link
Ah cool, I don't really mess with tables that much, but thanks for the info spuck. :-P
Apr 27, 2012 meridian link
You could create a second muffinlovers table, which you have to maintain as you add/remove/modify entries in your main table. It would look something like this:

muffinlovers = {chocolate = {bob=1, john=1, kate=1}, orangepoppyseed = {lucy=1}}

if you are guaranteed to only have only one entry per name in your main table, then you could do:

muffinlovers = {chocolate = {bob=true, john=true, kate=true}, orangepoppyseed = {lucy=true}}

and your return function becomes:

function muffins(muffin_type)
local ret = {}
for name,_ in pairs(muffinlovers[muffin_type] or {}) do
table.insert(ret, name)
end
--apply sort function here if necessary
return ret
end

Or if you don't need an indexed table returned, you can just do muffinlovers[muffin_type]

This method takes up additional memory because of having the second table, but will save a bit on processing cycles since you only have to iterate over one muffin type rather than the entire table (especially if you have lots of entries and muffin types). And, of course, you have to keep the muffinlovers table up to date as you add/remove entries in your main table. If your main table only has 20 entries, you are probably better off with spuck's method.
Apr 28, 2012 draugath link
table.concat() is much faster and uses less memory than trying to manually concatenate your strings.

http://old.wowace.com/wiki/Coding_Tips#Lua_and_strings
Apr 28, 2012 mr_spuck link
What meridian said. If looking up names by muffin type is all you're ever gonna do then you could create the main table indexed by that already

You could also keep the table sorted by muffin type then do a binary search or something.

It's really only worth it with lots of entries though.