programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Search alphanumeric ranges for a given value

Greenhorn
Posts: 8
I have seen similar issues posted but none that match my puzzle
Given the following data sets:
SAMPLE1 A4206-A9999, B4000-B9999
SAMPLE2 B4000-B9999
SAMPLE3 G0008-G9156, K0000-K9999, Q0035-Q9968

What's the simplest way to find that B4100 falls in SAMPLE1 Or that G0004 Does not fall in any of the samples?

With straight numeric ranges I have used an enum. For this, I have tried several concepts but I can't quite sort it out.

Any help is greatly appreciated.

Author and all-around good cowpoke
Rancher
Posts: 13078
6
What kind of structure are these "data sets" in to start with?

Bill

Bartender
Posts: 1840
10
Each of these values seems to be in the format of letter followed by 4 numbers.

Options could be
1 - treat them as strings, and just use string comparisions. Should work as long as all of the values are in this defined format.
2 - encode it as an object with "letter" and "number" attributes, and write an appropriate comparator.

In terms of general algorithm you could follow a few approaches
Simple stupid algorithm:
For each code you want to look up:
for each sample
for each range in the sample
check if the code falls in the range of that sample.

Each time you lookup a code you have to search through each sample, and all its ranges.

A smarter way might be to build a map of code ranges to samples
So that you in effect have the data in a structure like this:

A4206-A9999 : Sample1
B4000-B9999 : Sample1, Sample2
G0008-G9156: Sample3
...

Given this structure, it becomes more efficient to discover which sample (if any) a code is in.
It would be
- find the code (could do a binary search rather than looking through each entry)
- if it has an entry in this table, then you have your answer
- the problem becomes building and maintaining this table :-)