CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2011
    Posts
    1

    Detecting Unique combinations of sequential numbers

    Hello all,

    Long time lurker, first time poster here. My thanks in advance to everyone!

    I am trying to fool around something on my own (no I'm not a college student looking for someone to do my work for me etc).

    Ive come across a problem I'd really like to solve, and for some reason cannot seem to come up with any kind of workable solution. Pointers gently nudging me (or shoving me even) in the right direction are very much appreciated!

    Now down to the problem at hand:

    I am trying to write a cribbage game. Its a game I enjoy, and one which is testing my skills very well. The problem Im having is scoring unique combinations of sequential numbers within a 5 element array. So the question is:

    Given a random selection of five numbers from a known range (1 - 14) occurring a known number of times in the 'pool' (4 times each) how can one determine the number of unique 3 or more numeric sequences, assigning 1 'point' per number?

    For example:

    Given a single dimensional array holding the numbers

    1, 1, 2, 2, 3


    you have four separate series of sequential numbers where at least one of the numbers is replaced every time.

    if you identify the array elements alphabetically from a to e the distinct combinations would be
    a,c,e
    a,d,e
    b,c,e
    b,d,e

    giving 1 point to each element in the sequence, you should come out with 12.

    Conversely, if you had a 5 element array of
    1237910

    the only distinct combination would be abc, for 3.

    Any suggestions/nudges/ slaps over the head in the right direction?

  2. #2
    Join Date
    Oct 2007
    Posts
    25

    Re: Detecting Unique combinations of sequential numbers

    I'm sure there's a nice mathematical way of doing this, but since no-one has replied with one yet, here's a brute-force type of way to deal with it.

    Do a loop through the pool of numbers, for each of those loop do another loop through the rest of the numbers to find one that is next in sequence (e.g. if number is '4' look for '5'). If it finds one then it loops again looking for the next highest number until it runs out of numbers.

    Some csharp/pseudo type code:

    int[] pool = array(1,1,2,2,3); //array containing numbers pool

    for each number in pool as numA
    {

    for each number in pool as numB
    {
    if numB = numA + 1 then ... //you've found a match, loop again looking for another match.
    }
    }


    It's not direct code since you seem to want a nudge/suggestion more than just an answer

  3. #3
    Join Date
    Dec 2008
    Posts
    144

    Re: Detecting Unique combinations of sequential numbers

    Instead of using an array you could use a List and check it's Contains method to see if the value already exists in the List. If no, then add. If yes, then itterate. This is better than an array because you don't have to declare its bounds upfront. It's dynamic and you can do some other cool stuff with lists depending on your needs.

    Also, this will be a lot faster than interating through an array via a loop structure.

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

Featured