• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Search alphanumeric ranges for a given value

 
Greenhorn
Posts: 8
Postgres Database Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kind of structure are these "data sets" in to start with?

Bill
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :-)



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic