Finding the position of a book on a bookshelf

Suppose you enter a physical Amazon bookstore and you would like to look for a newly released title. You come to a special shelf dedicated only to new titles, but there are many and you can't exactly point to the right one, so you seek an employee to help.

Here is what the store has already done behind the scenes:

import sqlite3 from datetime import datetime conn = sqlite3.connect('amazon_books.db') cursor = conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS books ( ID INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author text, stars INTEGER, votes INTEGER, book_type TEXT, price_dollar DECIMAL(5, 2), release_date DATE )''') insert_data = [] with open('amazon_book_data.txt') as f: data = f.read() lines = data.split('\n\n') for line in lines: rows = line.split('\n') stars_votes_line = rows[2] if 'out of 5' in stars_votes_line: stars, votes = stars_votes_line.split(' out of 5 stars ') votes = votes.replace(',', '') stars, votes = float(stars), int(votes) title, author, book_type, price_dollar = rows[0], rows[1], rows[3], rows[4] price_dollar = price_dollar.split(' ')[0][1:] rows[5] = rows[5].replace('Release Date: ', '').replace('Publication Date: ', '') month_day, year = rows[5].split(', ') month, day = month_day.split(' ') if int(day) < 10: day = '0' + day release_date = datetime.strptime(month + ' ' + day + ', ' + year, '%B %d, %Y').strftime('%Y-%m-%d') insert_data.append((title, author, stars, votes, book_type, price_dollar, release_date)) cursor.executemany(""" INSERT INTO books VALUES(NULL, ?, ?, ?, ?, ?, ?, ?) """, insert_data) conn.commit() conn.close()

That is, the books have already been stored in a database. You ask the employee for the book "Soulful simplicity" that you have already seen online. Because operations have been streamlined, the only thing the employee has access to are the indexes of the books and their exact positions on bookshelf, consisting of 4 rows holding 25 books each:

import numpy as np book_locations = np.array([ [77, 24, 41, 56, 47, 15, 39, 14, 65, 28, 58, 20, 76, 68, 4, 73, 12, 57, 50, 17, 21, 88, 44, 29, 2], [61, 86, 99, 8, 60, 64, 0, 97, 74, 13, 81, 49, 91, 34, 72, 95, 46, 70, 94, 48, 7, 92, 55, 82, 69], [30, 1, 33, 63, 80, 19, 96, 71, 5, 45, 78, 43, 89, 18, 10, 31, 9, 93, 51, 35, 16, 3, 98, 84, 62], [11, 75, 52, 38, 79, 42, 87, 40, 27, 6, 54, 83, 26, 59, 37, 66, 53, 32, 36, 23, 22, 90, 67, 25, 85] ]

The employee attempts to find the index of the book first to be able to tell its exact location in the matrix:

book_title = 'Soulful simplicity' import sqlite3 conn = sqlite3.connect('amazon_books.db') cursor = conn.cursor() cursor.execute("SELECT ID, price_dollar FROM books WHERE title LIKE '%" + book_title + "%'") idx, price = cursor.fetchone() matrix_idx = idx - 1 print(matrix_idx) # 75 row_idx, col_idx = np.where(book_locations == matrix_idx) row, col = map(lambda x: x[0] + 1, (row_idx, col_idx)) print('Row: %d, Col: %d' % (row, col)) # Row: 4, Col: 2 print(price) # 10.2

After it becomes clear that the index is 75, this points to the second book at the fourth (lowest) shelf. If we look back at the matrix, this seems to be true. You excuse yourself for being unable to see it, thank the employee for the effort, take the book and head towards the cash register preparing $10.20.