CSc 250: Assignment 11

This assignment will be implemented in python. You should only use python features that we have discussed up to this point. If you have questions about whether or not a particular feature can/should be used, ask the instructors!

When grading your assignments, each problem/script/function will be thoroughly tested with many types of input, so make sure you test your scripts/functions well!

The python in this assignment will be graded using python 3.6, and tests will be run on a Mac machine. Python generally works well cross-platform, but if you want to be absolutely sure your program works as expected, test on a mac! Any python version 3.4 or newer should suffice though.

All of your scripts should be well-formatted and easy for the graders to read. Each script should have a header comment at the top that has the following format:

#
# Author: Student Name
# Description:
#    A short description of what this program / script / set of functions does!
# 

Each function in your script(s) should have a descriptive comment above it. If any part of your scripts are particularly complex, you should put documentation comments above those lines of code.

Puter3

This assignment is the third (and last) iteration of puter for the semester. In this assignment, you’ll be implementing a variant of puter2, and it will use the sqlite3 python module to interact with a database. Call the script puter3.py.

The Database Schema

The database that puter3 interacts with will have the following schema:

CREATE TABLE question (question TEXT PRIMARY KEY, answer TEXT);
CREATE TABLE favorite (category TEXT PRIMARY KEY, choice TEXT);
CREATE TABLE contact  (name TEXT PRIMARY KEY, nickname TEXT, email TEXT);

The database should be called puterdb. Puter3 will be tested with a pre-populated database, so you do not have to support adding entries to this database in puter3.py. Thus, you’ll only need to execute SELECT SQTL statements from python. To create this database, create a new empty sqlite3 database file in the same directory as puter3.py. Create these three tables in the database. For now the tables are empty, but I’ll show examples of populating it later.

Contacts

As previously mentioned the puter3 database stores a list of contacts.

When you tell puter to show me my contacts, puter will print out a list of all contacts with the following format:

...
show me my contacts
PUTER: Here are your contacts:
  Ben Dicken
    nickname: Benito
    email: ben@email.com
  Jane Doe
    nickname: Jayne
    email: jd@gmail.com
...

The contact information should be fetched from the contact table in puterdb. Use the sqlite3 module to get it.

Search Contact by Name

The user can also ask for an individual contact, by saying show me contact X, where X is the contact’s name. X may have spaces in it.

Given the same example database previously show, here are some example sessions that demonstrate this feature.

...
show me contact John Wick
  John Wick
    nickname: J-Wick
    email: wick@gmail.com
show me contact DC Morse
PUTER: I do not know that contact.
...

Favorite

Like puter2, puter3 will accept statements of the form what is my favorite A?. In this case, puter3 will look for a row in favorite where category is equal to A. If one is found, puter will report the corresponding choice. Otherwise, it will indicate that it doesn’t know what the favorite choice is. For example…

...
what is my favorite car?
PUTER: Your favorite car is Ford F-250.
what is my favorite food?
PUTER: Your favorite food is orange chicken.
what is my favorite pizza?
PUTER: I do not know!
...

General Questions

Any input that does not begin with what is my favorite, but does end with a ? should be looked up in the question table. When such a question is asked, puter3 will search through the rows of the question table and see if the question columns matches. If it finds a match, the answer column will be printed.

...
what is the diameter of the earth?
PUTER: 7,917.5 miles
how much does a gallon of milk weigh?
PUTER: About 8.6 lbs
what is the best place to get a tan?
PUTER: I do not know!
...

Examples

Say that puterdb is populated with the following rows:

INSERT INTO question VALUES ('what is the largest city in the world?', 'Shanghai');
INSERT INTO question VALUES ('how tall is the great wall of china?', 'around 7.5 meters');
INSERT INTO question VALUES ('who is john wick?', 'a legendary assassin');

INSERT INTO favorite VALUES ('food', 'orange chicken');
INSERT INTO favorite VALUES ('NBA player', 'Eric Bledsoe');
INSERT INTO favorite VALUES ('drink', 'COFFEEEEE');

INSERT INTO contact VALUES ('John Wick', 'J-Wick', 'wick@gmail.com');
INSERT INTO contact VALUES ('James Bond', '007', '007@gmail.com');
INSERT INTO contact VALUES ('Sherlock Holmes', 'Lockey', 'sherlock@england.com');
INSERT INTO contact VALUES ('Endeavour Morse', 'DC Morse', 'dcm@police.com');

Below are several example puter sessions, given this database.

$ python3 puter3.py
PUTER: Hello and welcome! Let's talk...
exit
PUTER: Bye!
$
$ python3 puter3.py
PUTER: Hello and welcome! Let's talk...
who is John Wick?
PUTER: I do not know!
who is john wick?
PUTER: a legendary assassin
show me my contacts
PUTER: Here are your contacts:
  John Wick
    nickname: J-Wick
    email: wick@gmail.com
  James Bond
    nickname: 007
    email: 007@gmail.com
  Sherlock Holmes
    nickname: Lockey
    email: sherlock@england.com
  Endeavour Morse
    nickname: DC Morse
    email: dcm@police.com
exit
PUTER: Bye!
$
$ python3 puter3.py
PUTER: Hello and welcome! Let's talk...
what is the largest city in the world?
PUTER: Shanghai
what is my favorite drink?
PUTER: Your favorite drink is COFFEEEEE.
what is my favorite DRINK?
PUTER: I do not know!
who is john wick?
PUTER: a legendary assassin
exit
PUTER: Bye!
$
$ python3 puter3.py
PUTER: Hello and welcome! Let's talk...
what is my favorite nba player?
PUTER: I do not know!
what is my favorite NBA player?
PUTER: Your favorite NBA player is Eric Bledsoe.
how tall is the empire state building?
PUTER: I do not know!
show me my contacts
PUTER: Here are your contacts:
  John Wick
    nickname: J-Wick
    email: wick@gmail.com
  James Bond
    nickname: 007
    email: 007@gmail.com
  Sherlock Holmes
    nickname: Lockey
    email: sherlock@england.com
  Endeavour Morse
    nickname: DC Morse
    email: dcm@police.com
how tall is the sears tower?
PUTER: I do not know!
exit
PUTER: Bye!
$
$ python3 puter3.py
PUTER: Hello and welcome! Let's talk...
WHO IS JOHN WICK?
PUTER: I do not know!
What Is My Favorite food?
PUTER: I do not know!
what is my favorite FOOD?
PUTER: I do not know!
show ME my contacts
PUTER: Huh?
exit
PUTER: Bye!
$
$ python3 puter3.py
PUTER: Hello and welcome! Let's talk...
show me contact john wick
PUTER: I do not know that contact.
show me contact John Wick
  John Wick
    nickname: J-Wick
    email: wick@gmail.com
show me contact DC Morse
PUTER: I do not know that contact.
exit
PUTER: Bye!
$
$ python3 puter3.py
PUTER: Hello and welcome! Let's talk...
show me my contacts
PUTER: Here are your contacts:
  John Wick
    nickname: J-Wick
    email: wick@gmail.com
  James Bond
    nickname: 007
    email: 007@gmail.com
  Sherlock Holmes
    nickname: Lockey
    email: sherlock@england.com
  Endeavour Morse
    nickname: DC Morse
    email: dcm@police.com
show me contact James Bond
  James Bond
    nickname: 007
    email: 007@gmail.com
SHOW ME CONTACT Endeavour Morse
PUTER: Huh?
show me contact Endeavour Morse
  Endeavour Morse
    nickname: DC Morse
    email: dcm@police.com
exit
PUTER: Bye!
$

Notice that pretty much everything about puter3 is case-sensitive. You should not be normalizing everything to lower or upper case.

Even More Examples

Say that puterdb is populated with the following rows:

INSERT INTO question VALUES ('how many feet in a meter?', 'about 3.2 feet');
INSERT INTO question VALUES ('what is my favorite soup?', 'red-bean');
INSERT INTO question VALUES ('what is my favorite RESTAURANT?', 'PeiWei');

INSERT INTO favorite VALUES ('soup', 'chicken-noodle');
INSERT INTO favorite VALUES ('pizza', 'Hawaiian');
INSERT INTO favorite VALUES ('restaurant', 'china phoenix');

INSERT INTO contact VALUES ('JOHN MICHAEL', 'JM', 'john@gmail.com');
INSERT INTO contact VALUES ('JOHN michael', 'Jm', 'michael@gmail.com');
INSERT INTO contact VALUES ('john michael', 'jm', 'jm07@email.az.com');

Below are additional example puter sessions using this database.

$ python3 puter3.py
PUTER: Hello and welcome! Let's talk...
what is my favorite soup?
PUTER: Your favorite soup is chicken-noodle.
what is my favorite restaurant?
PUTER: Your favorite restaurant is china phoenix.
what is my favorite RESTAURANT?
PUTER: I do not know!
exit
PUTER: Bye!
$
$ python3 puter3.py
PUTER: Hello and welcome! Let's talk...
show me contact JOHN MICHAEL
  JOHN MICHAEL
    nickname: JM
    email: john@gmail.com
show me contact JOHN michael
  JOHN michael
    nickname: Jm
    email: michael@gmail.com
show me contact john michael
  john michael
    nickname: jm
    email: jm07@email.az.com
show me contact john MICHAEL
PUTER: I do not know that contact.
exit
PUTER: Bye!
$
$ python3 puter3.py
PUTER: Hello and welcome! Let's talk...
how many feet in a meter?
PUTER: about 3.2 feet
what is my favorite pizza?
PUTER: Your favorite pizza is Hawaiian.
show me my contacts
PUTER: Here are your contacts:
  JOHN MICHAEL
    nickname: JM
    email: john@gmail.com
  JOHN michael
    nickname: Jm
    email: michael@gmail.com
  john michael
    nickname: jm
    email: jm07@email.az.com
exit
PUTER: Bye!
$

Testing for correctness

Some of the problems in this assignment requires printing out precise text. We will only take minimal points off for very minor differences (for example, minor character spacing issues). However, we will take points off for any non-trivial differences, so try to be as precise as you can!

Submission and grading

This problem will be graded out of 100 points.

This was assigned on Friday, April 7, 2017. It is due Friday, April 14, 2017, at 6:00pm.

Turn-in instructions:

Following these turn-in instructions closely is very important, because our grading scripts will depend on some of the details. You may lose points if these instructions are not followed precisely!