r/dailyprogrammer • u/oskar_s • Jul 30 '12
[7/30/2012] Challenge #83 [intermediate] (Indexed file search)
For this challenge, write two programs:
- 'index file1 file2 file3 ...' which creates an index of the words used in the given files (you can assume that they are plain text)
- 'search word1 word2 ...' which prints the name of every file in the index that contains all of the words given. This program should use the index previously built to find the files very quickly.
The speed of the "index" program doesn't matter much (i.e. you don't need to optimize it all that much), but "search" should finish very quickly, almost instantly. It should also scale very well, it shouldn't take longer to search an index of 10000 files compared to an index of 100 files. Google, after all, can handle the same task for billions/milliards* of documents, perhaps even trillions/billions!
(*see easy problem for explanation)
Index a folder where you have a lot of text files, which on my computer would probably mean the folders where I store the programs I've written. If you don't have a lot text files, head over to Project Gutenberg and go nuts.
Good luck!
- Thanks to abecedarius for suggesting this problem at /r/dailyprogrammer_ideas! If you have a problem that you think would be fun, head on over there and suggest it!
3
u/lawlrng 0 1 Jul 30 '12 edited Jul 30 '12
index.py
search.py
Output
1
u/ctangent Jul 30 '12
Implemented in Python:
index:
search:
github:
Both index and search are very fast and scalable.
1
u/goldjerrygold_music Jul 30 '12
The index should just say (for each word) which files contain that word, right? so if the files are "blah blah", "nyah blah", "ha ha", the index would look like "blah":1,2, "ha": 3, "nyah": 2.
1
u/abecedarius Jul 30 '12
A shell-script solution at https://github.com/darius/sketchbook/tree/master/textsearch/simple2
BTW, the size of the index matters too. My index was 34% of the total size of the input files, though obviously this depends on the input.
1
u/boohooo143 Jul 30 '12
My first time doing one of these challenges, here is my solution in java http://pastebin.com/kvVQYEaM
1
u/[deleted] Jul 30 '12
Java
1
u/paininthebass Jul 31 '12
My solution in c++11. I haven't tested it much.
1
u/taion809 Aug 02 '12 edited Aug 02 '12
PHP OOP, critique welcome and wanted.
If i fudged up please lemme know :)
Search: http://pastebin.com/qbUc8tR3
Index: http://pastebin.com/17D0i2eZ
Entry Point: http://pastebin.com/XG4bGMnj
1
u/Ledrug 0 2 Aug 08 '12
Perl. Using file system as the database.
To find a word, say "the", do: