Advice on design - Picking items problem

  • Thread starter Thread starter discussions
  • Start date Start date
D

discussions

Hello all,

I would like some advice how best to approach the following problem. I
have "sort of" solved it but in a very horrible and complex way, I'm
sure there's a better, simple and more elegant way, but I just can't
see it.

The problem is as follows...

I have a list of items that are of the following types: A, B, C and D.
(There could be more in the future.) On my page I have a 4x4 grid (a
table) which I will fill with these items. Each row can only contain
items of the same type, i.e. you cannot mix types within a single row.
Each row will contain a maximum of 3 items and the last "hole" will
display a branding image. If there are less than 3 items of a single
type exist, the remaining "holes" in the row will be filled with more
branding images. It is important to show all the types even if only one
item exists for that type.

I get the count of each item from the DB. I am not sure what the count
will be. In the ideal world I will have 3 of A, 3 of B, 3 of C and 3 of
D. Therefore I can insert one item in each cell (each row representing
a single type) and insert 1 image. However, things don't usually work
out that way!! If no items exist for a particular type I will just show
more items of a type that does. I want to make sure that the spare rows
are filled evenly, i.e. shared among the types that have extra items.

Some examples...
1. If there are 12 of A and 1 of B my grid will show the following:

(I is an image)
AAAI
AAAI
AAAI
BIII

If there are 12 of A and 4 of B, the grid will show the following:
AAAI
AAAI
BBBI
BIII

If there are 12 of A, 4 of B and 1 of C the grid will show the
following:
AAAI
AAAI
BBBI
CIII

(It can be assumed that we can work through the items in the following
order, A, B, C and D)

Thanks,
Jose
 
I think that your last assumption there doesn't work. In fact, you
can't assume that you can work through the items in order of type.Your
last example illustrates this: You have 12 of A, but you can show only
6, because you have to show at least one row of B and one row of C. You
can't know how many rows you can devote to A unless you "look ahead" to
see how many rows you need for the other types.

It looks to me as though you need a two-pass algorithm: in the first
pass, you decide how many rows must be set aside to show at least one
row of each type that you have to show. After that, if you have rows
left over, then you decide (based on some heuristic) how you're going
to use them. To take your three examples again:

1. If there are 12 of A and 1 of B. On the first pass, you discover
that you must reserve 1 row for A and 1 row for B. On the second pass,
you decide to allocate the remaining two (free) rows to A.

2. If there are 12 of A and 4 of B. On the first pass, you discover
that you must reserve 1 row for A and 1 row for B. On the second pass,
you decide to allocate one of the remaining rows to A and one to B.

3. If there are 12 of A, 4 of B, and 1 of C. On the first pass, you
discover that you must reserve 1 row for each of A, B, and C. On the
second pass, you decide to allocate the one remaining row to A.

Now, of course, after you've decided how many rows each type gets, you
can fill the rows in order (which I suppose constitutes a third pass).
However, I can't see how you can do it all in one pass.
 
Back
Top