The Weekly Challenge 024

Smallest Script and Inverted Index Search

Solutions on GitHub

Task 1: Smallest Script

Create the smallest script that runs without errors in Perl and Python.

Logic

For Perl, a script needs to evaluate to a true value, which can be represented simply as 1;. In Python, we can write a script with only a pass statement or leave it entirely empty.

Perl Solution

ch-1.pl

1;

Python Solution

ch-1.py

pass

Task 2: Inverted Index Search

Create a script to implement full-text search functionality using an Inverted Index. An inverted index maps words to the documents they appear in to allow fast searches.

Logic

For the Perl solution, we read through files, split their content into words, clean them up, and build a nested hash where the keys are the lowercase words, and the values are hashes mapping document filenames to 1. For the Python solution, we use an SQLite database to store words, documents, and a join table found representing the mappings.

Perl Solution

ch-2.pl

my @files = glob "*"; my %words; for my $file (@files) { open my $what, '<', $file or croak "Cannot open $file $!"; while (my $line = <$what>) { for (split /\s+/msx, $line) { s/\W//gmsx; $words{lc($_)}{$file} = 1; } } close $what; }

Python Solution

ch-2.py

def add_document(path: str) -> None: title = Path(path).stem with sqlite3.connect(DBFILE) as con: doc_id = get_document_id(con, title) with open(path, encoding="utf-8") as fh: for line in fh: for word in WORD_RE.findall(line.lower()): word_id = get_word_id(con, word) con.execute( "INSERT OR IGNORE INTO found(document_id, word_id) VALUES(?, ?)", (doc_id, word_id), ) con.commit()