|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Algorithm help - boolean operations on array elements
Okay, say I have a 2D array like the following:
Array [0][0] = "Fruit" Array [0][1] = "Apple" Array [0][2] = true Array [1][0] = "Fruit" Array [1][1] = "Apple" Array [1][2] = true Array [2][0] = "Fruit" Array [2][1] = "Apple" Array [2][2] = false Array [3][0] = "Fruit" Array [3][1] = "Pear" Array [3][2] = true Array [4][0] = "Fruit" Array [4][1] = "Pear" Array [4][2] = true As you can see, the [n][0] element is a broad descriptor of the [n][1] element. I'm trying to think of an algorithm that will take the [n][2] element for each like [n][1] element and "AND" them. Then take the "AND" values for each [n][1] element and "OR" them. Since [3][1] and [4][1] both equal "Pear", the [3][2] and [4][2] elements would be "AND"-ed together, in this case, returning "true". So the "Pear" value of "Fruit" would be true. The same would happen for Apple, but Apple would be false, since it does not satisfy the AND requirements. Then, because Pear is true and Apple is false, the algorithm would return true for "Fruit" because it meets the OR requirement. Are there any popular algorithms that are similar to this? I don't kow of any, but I figured someone else might. Thanks. |
|
#2
|
||||
|
||||
|
I might be completely off, but... in the end, aren't you just checking if there is a FALSE in every [n][1] category for the specified [n]?
Working backwards, for the OR to work out TRUE, it means that at least ONE of the categories "inside" [n][0] ("fruits" or "vegetables") has to be true. For this statement to be true, it means that at least ONE of the sub-categories ("apple", "pear", ...) has to have a full completement of [n][2] as TRUE. You could simply iterate over the array, checking every category for at least one FALSE. If there was one category you had failed to find the FALSE in, then it meant the answer was true, and you didn't have to continue running through the array. On a similar note, whenever you found a "FALSE" in a category, you could skip it entirely. Also, it might be worth checking out if making an array with more "dimensions" makes any sense, or if re-arranging data organization is more trouble than it's worth or not. It depends on how you retrieve that information, but I'm guessing you could build an array along the lines of: Code:
myArray =
["Fruit"] =
["Pear"] = [true, true, false, true, false, ...]
["Apple"] = [false, false, true, false, true, true, ...]
["Veggies"] =
["Cabbage"] = [true, ...]
["Onions"] = [false, ...]
You've got the point. In any case, it makes for an easier algorithm to solve that problem you're looking at. Well, maybe not easier, but I'm guessing at least cleaner and clearer. Hope that made sense. Again, I might be completely off:\
__________________
"Get it hot! Hit it harder!!!" --- The tutor warcry ActivePython Mark Pilgrim's Dive into Python E-book, thank this guy for his contribution to the Python community! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > Algorithm help - boolean operations on array elements |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|