Solved playing with numbers challenge

Posted on 2011-05-09
Last Modified: 2012-05-11
Hello EE,

I have DatagridView that the user can press a "add" button and via textboxes he enters values.
the grid has columns like that:  

From1       To1             From2          To2        From3            To3     (until 5) ..

i have 10 textboxes in the same way.

fact1:  All textboxes are MaxLength of 3 (only numbers)   (0 to 999)
fact2: From1 and To1 must have numbers in it.
fact3: if from2,3,4,5 and to2,3,4,5 are blank I consider that its the MIN inside the from (0) to the MAX inside the TO (999)
fact4: you need both values in from1,2,3,4,5 and in to1,2,3,4,5  not just a from or a to you know..
fact5: The From needs to be always less than the To...

if your first Row  is :   From1:  1000 and To1 :1500

you cannot add another Row  From1 1100 to To1 1600   because 1100 is already between 1000 and 1500.
same thing, lets say you add a new row with from1 900 to 1600 well it cant because between 900 to 1600 there are numbers already inside 1000 to 1500.. you get it ?

now the tricky part...
if you had the same 1000 to 1500 in the second row, it can be fine only if the From2 and To2 are different
if from2 and to2 are the same, you need to go see if from3 to to3 are different... etc until 5 you know?

so im not sure and to do all that.. im quite lost...can you help ?
Question by:PhilippeRenaud
    LVL 39

    Expert Comment

    by:Kyle Abrahams
    For each set, you have to check the from and to is not within the range of another set.

    so say new set is has two vars:  New_Min and New_Max

    class set
      public property min as int
      public property max as int
      public property RowID as int
    end class

    public function can_add_or_change(RowID) as Boolean
    Dim can_add as boolean = true
    Dim sets as new List(of set)

    for each s in sets
      if (new_min >= s.min or new_min <= s.max or new_max >= s.min or new_max <= s.max) and s.Row_Id <> rowID
         can_add = false
         exit for
      end if

    if can_add
      sets(RowId).min = newmin
      sets(RowID).Max = newmax
      return true
     'Let user know they have made a mistake
     return false
    end if
    end function

    Then it's just a matter of keeping track of the list.  Let each row number be the rowID.  Update the rows as necessary within the list.  Using the logic of can_add to guide you.
    LVL 1

    Author Comment

    min max is my from to?

    what about the thing I said about that it could be the same from1 and to1 but if others are different and so on ? is that your RowID!?
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    After verifying that you don't have any time slot collisions, concatenate all the from/to fields for that row into a string with consistent formatting (like ensuring they have leading zeros to the same number of digits and making them comma separated).  Then you can use that string as the KEY that you add to a Dictionary/HashTable.  If the key already exists then you know you have a duplicate row.
    LVL 1

    Author Comment

    I see mmm

    so lets say I had 3 rather than 5..

    I would have

    100,500,001,009,010,050   that would be my key ?
    LVL 1

    Author Comment

    what doyuo mean by time slot?
    LVL 85

    Accepted Solution

    Correct.  Just leave off any blank entries.  This should allow partial matches but prevent any rows from having exactly the same sequences.  This does NOT take into account if the time slots in a row were out of order though!  You'd have to sort them first if this is an issue...
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I guess a "number slot" is a better description...sorry.
    LVL 1

    Author Comment

    what if they dont have all the same formatting (i said 3  0 to 999) in my example but in realtity it varies. but it will never be like 10..

    should I put 10 leading zero to be safe?  I guess its no trouble right?
    LVL 1

    Author Closing Comment

    Idle_Mind, you are right but I am not sure how to implement it.

    I will create new question, can u help in the next one?  I understand i can find duplicates keys but what If I have:


    both are not duplicate but it should still fail because from 0000 to 0003 there is 0001 and 0002 and 0003 inside it already in the first line (between 0001 and 0004)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now