C# set algorithm

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QXN0cm9EcmFiYg==?=

    C# set algorithm

    Does anyone have any tips on creating an efficient set algorithm?

    I have the following set {a, b, c, d, e}.

    I need to combine them in all possible ways:
    a
    ab
    ac
    ad
    ae
    abc
    b
    bc
    bd
    be
    ....
    ace
    ....

    Get the picture?

    Thanks for any help. Note, the combinations need to be unique. For
    instance, I don't need "ab" in addition to "ba".

    Thanks for any help,

    AstroDrabb
  • =?Utf-8?B?QXN0cm9EcmFiYg==?=

    #2
    Re: C# set algorithm

    "AstroDrabb " wrote:

    <snip>

    One thing I don't think I mentiond is that duplicate based on PRICE need to
    be removed.

    For example:

    cod/shrimp, 55.50
    is the same as
    shrimp/cod 55.50

    Thanks,

    AstroDrabb

    Comment

    • =?Utf-8?B?QXN0cm9EcmFiYg==?=

      #3
      Re: C# set algorithm

      "Jon Skeet [C# MVP]" wrote:
      AstroDrabb <AstroDrabb@dis cussions.micros oft.comwrote:
      There are five items that represent products. For example

      {cod, flounder, crab, shrimp, lobster}

      I have to come up all possible combinations for the produts to insert into
      a table.
      >
      Okay, we can do that. We can do it reasonably amusingly using iterator
      blocks. Have a look at this :)
      >
      <snip>

      Thanks Jon. I will play around with it. I forgot to mention that I am
      using .Net 2.0. IEnumerable<str ingdoesn't have a definition for ToArray().
      I have been wanting to update to .Net 3.x though.

      Thanks again.

      Comment

      • =?Utf-8?B?QXN0cm9EcmFiYg==?=

        #4
        Re: C# set algorithm

        "Jon Skeet [C# MVP]" wrote:
        AstroDrabb <AstroDrabb@dis cussions.micros oft.comwrote:
        Thanks Jon. I will play around with it. I forgot to mention that I am
        using .Net 2.0. IEnumerable<str ingdoesn't have a definition for ToArray().
        I have been wanting to update to .Net 3.x though.
        >
        That's an extension method in .NET 3.5 - but there are other ways of
        stringing the things together.
        I have 3.5 installed and the correct <compilersentri es, I am using VS
        2008. Are there extenstions to 3.5 that I need to download and install?

        Thanks for all the help,

        AstroDrabb

        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: C# set algorithm

          Jon Skeet [C# MVP] wrote:
          AstroDrabb <AstroDrabb@dis cussions.micros oft.comwrote:
          >There are five items that represent products. For example
          >>
          >{cod, flounder, crab, shrimp, lobster}
          >>
          >I have to come up all possible combinations for the produts to
          >insert into a table.
          >
          Okay, we can do that. We can do it reasonably amusingly using iterator
          blocks. Have a look at this :)
          Too bad IEnumerator<Tdo esn't implement Clone. Otherwise (and this would
          be easily modified to keep a total price alongside and return
          KeyValuePair<st ring, float>):

          IEnumerable<str ingMakeAllCombi nations<T>(IEnu merable<Tlist)
          {
          using (IEnumerator<Te num = list.GetEnumera tor())
          {
          while (enum.MoveNext( ))
          {
          using (IEnumerator<Te num2 = enum.Clone)
          {
          foreach (string combo in Descend(enum.Cu rrent.ToString( ),
          enum2))
          yield return combo;
          }
          }
          }
          }

          IEnumerable<str ingDescend<T>(s tring prefix, IEnumerator<Ten um)
          {
          if (!enum.MoveNext ())
          {
          yield return prefix;
          return;
          }

          using (IEnumerator<Te num2 = enum.Clone)
          {
          foreach (string combo in Descend(prefix, enum2))
          yield return combo;
          }
          foreach (string combo in Descend(prefix + "/" + enum.Current.To String(),
          enum))
          yield return combo;
          }


          Comment

          • =?Utf-8?B?QXN0cm9EcmFiYg==?=

            #6
            Re: C# set algorithm

            "Jon Skeet [C# MVP]" wrote:
            Nope. Does your application target .NET 3.5 though? (It's in the
            project properties.)
            It does now. I originally used "Create web site", and there was no project
            option there. After creating a new project of type web site, I was able to
            target 3.5.

            Thanks.

            Comment

            • jake

              #7
              Re: C# set algorithm

              Just an after-thought. How would you handle an occasion where more
              than one combination total to the same price? I am actually curious
              (not being sarcastic). Thanks.

              Comment

              • =?Utf-8?B?QXN0cm9EcmFiYg==?=

                #8
                Re: C# set algorithm

                "jake" wrote:
                Just an after-thought. How would you handle an occasion where more
                than one combination total to the same price? I am actually curious
                (not being sarcastic). Thanks.
                As I generate the combinations, I put the price and the combo description,
                i.e. "Crab/Shrimp", "55.49" into a Dictionary. I just check that for a dupe
                before I insert any description/value combination into the DB.

                Once that is done, I can easily call a stored procedure to do all the
                inserts knowing that each value is unique.

                However, one objective is that I need to flag all collisions of price. So
                if I find a collision, where the prices are the same, yet the products are
                different, I insert a record into a "collision" table that shows what
                combo/price is causing the collision.

                For example:

                "Crab/Shrimp", 55.95
                "Flounder/Shrimp", 55.95

                The products that produce the price don't matter. What matters is that the
                final price _needs_ to be unique. This would cause a record to be entered in
                the Collisions table and also send out an email to the people that need to
                fix this.

                Management/finance people look at that table and make the cost changes to
                prevent the collision. I cannot change the price of products. Gee I wish I
                could ;-)

                The current system here (at a BIG company that won't be named) is pretty
                messed up.

                Yeah, I know, the system is total crap. Why can't grouped offerings have
                the same price? No big deal to me. However, it is a legacy problem from the
                dude who built most of their "systems" when they were starting out as a small
                subsidiary of a large corp.

                The biggest challenge for me is that I _cannot_ kill the band-aid system
                they have now. I have to add this feature and make sure it still works.
                After that, I get to finally create a new system that can handle some basic
                things like having product GROUPS with the same price. The previous guy
                didn't use any primary keys, or even any indexes, so the only way to
                _currently_ tell one product group from another is by the total price. Damn,
                if the guy just knew about a primary key...

                Jon provided some good stuff. After a few little tweaks, it is working well
                (thanks again Jon).

                Best,

                AstroDrabb

                Comment

                Working...