Results 1 to 4 of 4

Thread: Combinations

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    85

    Combinations

    Hello,

    I've really been struggling with the following problem for a couple of days and I'd appreciate any suggestions any of you may have...

    I have an object that can have up to 4 properties. The object represents a general Product that we are selling in our store, and the properties are used to differentiate the products. For example...

    Product Object: Men's Shirt
    Property 1: Size (Small, Medium, Large)
    Property 2: Color (Red, Blue, Black)
    Property 3: Type (T-shirt, Sweater, Hooded)
    Property 4: In-Stock (Yes, No)

    The properties are not the same for each product and can be changed by the user to reflect the properties of different products as needed. For example...

    Product Object: Custom Baseball Cap
    Property 1: Adjustible Fit (Yes, No)
    Property 2: Logo - Designed by the customer
    Property 3: In-Stock (Yes,No)

    The above baseball hat comes in only 1 color and 1 size so it does not need to have a Size or Color property. The logo is designed by the customer.

    For inventory purposes, I need a way to find a way to obtain all combinations of properties for a give object...
    Code:
    Product            Size          Color         Type         In-Stock
    Men's Shirt       Small           Red           Shirt        Yes
    Men's Shirt       Small           Red           Shirt         No      
    Men's Shirt       Small           Red           Sweater       Yes
    etc...
    I've been looking into combinations and permutations, which I believe would work except for the fact that sometimes a Property may not be used by a product. It is easy enough to put every value of each property into an array and loop through each one...
    Code:
    for int1 = 0 to ubound(arrProperty1)
         for int2 = 0 to ubound(arrProperty2)
              for int3 = 0 to ubound(arrProperty3)
                   for int4 = 0 to ubound(arrPropery4)
                        'process this unique combination...
                         str1=arrProperty1(int1)
                         str2=arrProperty2(int2)
                         str3=arrProperty3(int3)
                         str4=arrProperty4(int4)
                   next
              next
         next
    next
    I think that the above solution would work if I always knew that every object had 4 properties, but how would I do it if an object has less than 4 properties. Also, it is possible for Property 1 to be undefined, but Properties 2, 3, and 4 are in use. Or it is possible that Property 2 and 3 are defined for an object but 1 and 2 are not, etc...

    I'd appreciate any insight in the matter or a push in the right direction. I'd be glad to elaborate if more information is required.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    skip the loops that are not used
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Member
    Join Date
    Dec 2003
    Location
    USA
    Posts
    42
    The best way is to use recursion (or the way such a problem is solved in Gaming theory)

    Otherwise simply use do..while loop. Initialise all the loops before starting the first do.

    It will ensure that the condition is checked after we write values. Thus loop will continue even if the value is undefined.

    Further U may add some constraints or validations like write only if atleast one value is defined.

    Further this way U have the flexibility to check and set a default value for the item if it is undefined.

    Howz that?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    85
    That's excellent, thanks for the advice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width