Alright. So I worked out a few things with Tasker. The tasks (names and times) are now stored in a yaml file. I thought about storing them as objects, but I haven't worked with yaml much, so I thought I might give it a shot. It seems to support variable times well now, but as far as weighting things to make them more or less likely... I've had this come up before on simple projects involving probability, and I'm not quite sure how to deal with it.
Once things are in an array, it's easy enough to just select one of them:
[task1, task2, ...]
return arr[rand(arr.size)]
Sweet... But to work in the weights, I feel like I have two basic options.
Option 1 Array with repetition:
[task1, task1, task1, task2, task3, task3]
return arr[rand(arr.size)]
Option 2 Hash/multidimensional array:
{task1 => {weight => 3}, task2 => {weight => 1}, task3 {weight => 2}}
return ???
Because it loads a yaml file, the first half of option 2 is taken care of. I figured that if I could get it into the other form, it would be easy, but it is not going as expected. The collect method let me down as follows:
arr = hash.collect { |x, y| if y and y['weight'] then y['weight'].times do x end else x end}
=> [4, "Matz Translation", "FFVI", "Practical Ruby Projects", 3]
What's going on here? It looks like collect won't work a little harder for me... It only wants to return once per key. Hmm.... well, if that's the case, how about I return a little baby array?
arr = hash.collect { |x, y| if y and y['weight'] then a = []; y['weight'].times do a.push(x) end; a; else x end}
Sweet. Everything there that I wanted... now all that's left is:
arr.flatten!
Nice. Now I can just use my random index of array code again.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment