Philippe Renaud
asked on
vb.net playing with numbers challenge
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 ?
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 ?
ASKER
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!?
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!?
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.
ASKER
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 ?
so lets say I had 3 rather than 5..
I would have
100,500,001,009,010,050 that would be my key ?
ASKER
what doyuo mean by time slot?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I guess a "number slot" is a better description...sorry.
ASKER
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?
should I put 10 leading zero to be safe? I guess its no trouble right?
ASKER
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:
1000,2000,0001,0004,0010,0 050
and
1000,2000,0000,0003,0020,0 060
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)
I will create new question, can u help in the next one? I understand i can find duplicates keys but what If I have:
1000,2000,0001,0004,0010,0
and
1000,2000,0000,0003,0020,0
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)
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
next
if can_add
sets(RowId).min = newmin
sets(RowID).Max = newmax
return true
else
'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.