Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > startsWith datastructure

Reply
Thread Tools

startsWith datastructure

 
 
Roedy Green
Guest
Posts: n/a
 
      10-27-2012
Lets say you had a list of book publishers and the block of ISBN
numbers they own. e.g. Prentice-Hall owns all isbns of the form
013XXXXXXXXXX,
or put another way, own all ISBNs that startsWith "013".
Some publishers such as O'Reilly own multiple blocks.

013, Prentice Hall
014, Penguin
0201, Addison-Wesley
0393, H. W. Norton
0471, John Wiley & Sons
0553, Bantam
059600, O'Reilly
156592, O'Reilly
0060, Harper
06723, Sams
07356, Microsoft Press
07432, Simon & Schuster
07710, McClelland & Stewart
08125, Tor
086571, New Society Publishers
0915972, Love Line
15795106, Ronin Publishing

etc.

Your list is incomplete. It has only the major publishers.

The problem is, given an ISBN, can you make at guess at its publisher?

What sort of datastructure/lookup mechanism would you use?

Obviously, you could just do a linear search looking for a match, and
unless the list got very long, that should suffice, but, just for
fun, say you wanted something faster, what would you do?

This is just an example of a class of problem I wanted to write a
little essay on for the Java glossary with possible solutions.
--
Roedy Green Canadian Mind Products http://mindprod.com
There are four possible ways to poke a card into a slot.
Nearly always, only one way works. To me that betrays a
Fascist mentality, demanding customers conform to some
arbitrary rule, and hassling them to discover the magic
orientation. The polite way to do it is to design the reader
slot so that all four ways work, or so that all the customer
has to do is put the card in the vicinity of the reader.


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-27-2012
On Sat, 27 Oct 2012 03:50:53 -0700, Roedy Green
<> wrote, quoted or indirectly quoted
someone who said :

> 013, Prentice Hall
> 014, Penguin


More precisely, I should have written that as:

> 978013, Prentice Hall
> 978014, Penguin

--
Roedy Green Canadian Mind Products http://mindprod.com
Let all things be done decently and in order. ~ I Corinthians 14:40.
Apparently Jehovah disapproves of Java Threads.


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      10-27-2012
On 27.10.2012 12:50, Roedy Green wrote:
> Lets say you had a list of book publishers and the block of ISBN
> numbers they own. e.g. Prentice-Hall owns all isbns of the form
> 013XXXXXXXXXX,
> or put another way, own all ISBNs that startsWith "013".
> Some publishers such as O'Reilly own multiple blocks.
>
> 013, Prentice Hall
> 014, Penguin
> 0201, Addison-Wesley
> 0393, H. W. Norton
> 0471, John Wiley & Sons
> 0553, Bantam
> 059600, O'Reilly
> 156592, O'Reilly
> 0060, Harper
> 06723, Sams
> 07356, Microsoft Press
> 07432, Simon & Schuster
> 07710, McClelland & Stewart
> 08125, Tor
> 086571, New Society Publishers
> 0915972, Love Line
> 15795106, Ronin Publishing
>
> etc.
>
> Your list is incomplete. It has only the major publishers.
>
> The problem is, given an ISBN, can you make at guess at its publisher?
>
> What sort of datastructure/lookup mechanism would you use?
>
> Obviously, you could just do a linear search looking for a match, and
> unless the list got very long, that should suffice, but, just for
> fun, say you wanted something faster, what would you do?
>
> This is just an example of a class of problem I wanted to write a
> little essay on for the Java glossary with possible solutions.


That's a typical use case for a Trie:
http://en.wikipedia.org/wiki/Trie

Other than that you could use a TreeSet and make use of method subSet()
where the lower bound is the fragment you got and the upper bound is the
fragment with the last character increased "by one" or a "max" character
appended.

Or you use a sorted array and use Arrays.binarySearch(). The Trie is
more efficient though.

Cheers

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
startswith( prefix[, start[, end]]) Query cjt22@bath.ac.uk Python 13 09-11-2007 07:41 AM
startswith() and endswith() for re's would be more intuitive metaperl Python 5 09-29-2006 12:16 AM
Java 1.6 Heisenbug involving startsWith Roedy Green Java 14 01-31-2006 02:30 PM
Efficient mechanism for str.startswith on a set. Brian Cole Python 1 01-10-2006 02:52 PM
RE: Slicing vs .startswith Batista, Facundo Python 0 09-22-2003 07:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57