Solved

Range validation

Posted on 2008-10-06
7
1,544 Views
Last Modified: 2010-04-21
I need to do a range-validation.
The ranges are composed this way :
Beginnig  with a number (max 999) and eventuelally 1 char (max 1 char)=> for example 2a.
They come always in in pairs but the beginpoint can be used as an endpoint too in the same range..

A range is thus expressed this way : from [1 to 3 ] or [1a-1a], or [1a-10]

Verification is needed as described hereby :
- No use of the same is permitted : so the same range must be excluded.
- No overlap is permitted : so the range [2 to 10] is not allowed if range [1-3] already exists
- If range [1 to 3] is given and a second range [4 to 10] exists, another range can still be created between them => 3a - 3f.
- In the same way, if the range is given [1 to 3],  2a is already taken because 2a < 3.
- In the same way, the range [4-10] does not comprise the range [10a-16] : 10a > 10

Is it possible to construct this in an algorithm to verify these ranges?
How do I best proceed for the creation? Any examples?

I already use this code :
class Range<T>
    {

        public readonly T Start;

        public readonly T End;



        public Range(T start, T end)
        {

            this.Start = start;

            this.End = end;

        }

    }


    static class Range
    {

        public static bool Overlap<T>(Range<T> left, Range<T> right)

            where T : IComparable<T>
        {

            if (left.Start.CompareTo(right.Start) == 0)
            {

                return true;

            }

            else if (left.Start.CompareTo(right.Start) > 0)
            {

                return left.Start.CompareTo(right.End) <= 0;

            }

            else
            {

                return right.Start.CompareTo(left.End) <= 0;

            }

        }

    }


Tanks for any help.
0
Comment
Question by:erwin_des
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 22651736
check if left.end > right.start and left.start < right.end

if those two conditions are true then the ranges overlap.  You don't need to check anything else
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 22651889
note, the comparison works regardless of which range happens to be right or left so there's no need to do a reverse check too
0
 

Author Comment

by:erwin_des
ID: 22657035
I have still a problem that seems to be related with the fact that i'm using strings and that the comparison does not work the same way as with numbers only.

But, I need string because a range can be defined like [3a-6] or even [3a-3d].





0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 74

Expert Comment

by:sdstuber
ID: 22659348
easiest way would be to convert your letters to decimal and treat everything as a number....

a=.01
b=.02
c=.03
....
z=.26

so [3a-6j]   would be 3.01  - 6.10

0
 

Author Comment

by:erwin_des
ID: 22660231
I think I found another way ...

If I have to compare [13a] with [20] I have to add a char befor the [20] too. The comparison works from left to right. This way, 1 will be smaller van 2 because it is now verified at the same level.

I still have to check it out more profoundly, but the first test seems to be positif.
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 22660297
be careful with comparing numbers as strings...


the string "1234a"  is less than the string "2"

i think it's safer to turn alphanumeric to pure numeric.  and very easy to do so too...

you could use the ascii value of the character divided by 1000 as your decimal.

a = .097  
b = .098
...
z= .122

that makes your conversion very easy  and always a safe comparison


0
 

Author Closing Comment

by:erwin_des
ID: 31503459
Thanks for your good advice.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

636 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