Python Jeopardy (Part 1)

Introduction

Once upon a time, I was tutoring a student who had taken an introduction to Java course who had a unique course project. The student’s job was to build an implementation of Jeopardy throughout the course. At the beginning, they would start simple with a basic program that read user input from the console. At the end of the semester, they had a full fledged GUI program that communicated over network sockets, communicated with a database, and even had multi-threading.

I loved this project because it literally covered every possible programming topic outside of web programming. This student had to learn out how read and react to user input from both the GUI and the console. The requirements of the program required them how to learn to read from a file. There was plenty of opportunity to learn Object Orientated Programming (OOP). I was so impressed with the material that was covered with this project that I asked the student to share the assignments with me when the semester was over. Thankfully, the student agreed!

Python Jeopardy

This is the beginning of a series of posts where we will recreate the Jeopardy program using the Python language. Our first task is to start by creating a plain text file that will contain our Jeopardy game board. Here is an excerpt of the file.

Earthquakes::Jeoportmanteau!::Ashes to Ashes::Dust To Dust::"K" Mart
400::800::1200::1600::2000
Earthquakes::400::This well-known fault is considered the main boundary between the North American & Pacific plates::the San Andreas Fault
...
FJ::Last name of Sir Clifford, whose crippling war injuries make life difficult for his wife::Chatterley

The first line of the input file lists our categories in our game of Jeopardy. Since it’s common to use commas in Jeopardy questions, we use :: to seperate each of our categories. Our requirements state that we have to have five categories or the file is invalid.

The next line of the file lists all of the possible point values. Like the categories line, we use :: to seperate each point. There have to be five points and they must be whole numbers.

The next twenty-six lines contain the questions for the game of Jeopardy. We start with the question’s category, followed by it’s point value. After the points we have the question (or the answer, depending how you wish to view it). and then finally we have the question’s answer. Our requirements state that we have to have 25 questions.

However we have a special question that can appear anywhere in this file exception for the first two lines. This is the final Jeopardy question. This line starts with FJ and the program needs to respond correctly when it sees the final jeopardy question. An example of the final jeopardy line is found on the last line of our example.

Implementation

Python supports object orientated programming, which lets us model components in the system as objects. At this point, we have two objects in our system. Our first kind of an object is our standard question. Let’s begin by making a class that models this component.

GameTile

class GameTile:
    def __init__(self, category='', points=0, question='', answer=''):
        self.category = category  # The question category
        self.points = points  # The point value of this question
        self.question = question  # The question
        self.answer = answer  # The answer

    def __str__(self):  # This method creates a string representation of this object
        # Let's store all of our properties in a dict object
        result = {'category': self.category,
                  'points': self.points,
                  'question': self.question,
                  'answer': self.answer}

        # Now we can convert the dict to a string which will give us friendly formatting
        return str(result)

    def __repr__(self):  
        # This method also creates a String representation of a Python object
        # The Python debugger calls this method rather than __str__
        # But we can just reuse our code by calling __str__
        return self.__str__()

So this is our first Python class of this project. One thing users who are familiar with other languages such as Java may notice is that none of the properties of this class are encapsulated. This may seem odd but keep in mind that Python is about readability and conciseness. We certianly could use Python’s psuedo-private techniques and then add getters and setters, but doing so will simply bloat the code.

At this point, we don’t have a lot of behavior in this class. This class defines a constructor (__init__) and it overrides the __str__ and __repr__ methods to provide us with string representations of this object. In __str__, we store all of our properties in a Python dict variable and then use str() to convert it to a String. __repr__ just simply uses the code in __str__ so that we can get String representations in the console.

Here is the next class in our script that represents the Final Jeopardy question.

FinalJeopardyTile

class FinalJeopardyTile:
    def __init__(self, question='', answer=''):
        self.question = question
        self.answer = answer

    def __str__(self):
        result = {'question': self.question, 'answer': self.answer}
        return str(result)

    def __repr__(self):
        return self.__str__()

Once again, we haven’t added much in terms of behavior in this class. That will come later. However both GameTile and FinalJeopardy serve a useful purpose in that we are grouping related information together into a single entity. Later on, we can add behavior to these classes to work on the data.

Now we have to process our game file and create these objects. Python is a lot like C++ in that we don’t have put our code exclusively in class. At this point in the project, reading the game file isn’t the job of any one particular class so we are going to define a module level function that does the job of reading the file and will return a dict of questions.

Reading the Game File

Here is enter readGameBoard function. Don’t get overwhelmed by the size of it because we are going to break it up into sections and explain each portion.

# These are custom exceptions that we are going to use.
class InvalidGameFile(Exception):
    pass


class IllegalCategoryCount(Exception):
    pass


class IllegalPointsCount(Exception):
    pass

def readGameBoard(gameFile):
    # Read the entire file into memory
    rawLines = open(gameFile, 'r').readlines()

    # Check that we have 28 lines in the game file
    if len(rawLines) != 28:
        # Throw our custom exception to indicate the file is invalid
        raise InvalidGameFile

    # Now this is going to store our files
    fileLines = []
    for line in rawLines:  # Iterate through rawLines one item at a time
        # Now we need to remove any '\n' characters and store them in fileLines
        fileLines.append(line.strip())

    points = []  # Store the points to validate the file later
    gameTiles = {}  # Store all of the questions here

    # There has to be 5 unique categories
    # We are going to split the first line apart by the :: character
    categories = fileLines[0].split('::')

    # Now check that we have 5 unique categories. We use the set() function that
    # will remove any duplicates and then we are going to check the length. It has to be
    # 5 per the requirements
    if len(set(categories)) != 5:
        raise IllegalCategoryCount  # Raise one of our custom exceptions

    # There has to be 5 unique points
    # So now we read the second line and split it one our :: character
    pointValues = fileLines[1].split('::')

    # Now, we have Strings in pointValues, but we need them to be integers
    # So we iterate through pointValues and convert each item to an int
    # before storing it in points. We use the int() function to do the conversion
    for pv in pointValues:
        points.append(int(pv))

    # Now once again, we need to for 5 unique points so we use set again to remove
    # the duplicates and then len to check the size
    if len(set(points)) != 5:
        raise IllegalPointsCount  # Throw a custom exception

    # Now read everything else
    for line in fileLines[2:]:  # Slice off the first two lines
        #  Get a line and split it into its parts
        parts = line.split('::')

        #  Now we check that the category and points are valid
        if parts[0] in categories and int(parts[1]) in points:
            # We can create a GameTile object at this point
            gameTile = GameTile(category=parts[0],
                                points=int(parts[1]),
                                question=parts[2],
                                answer=parts[3])

            # If this our first insertion, we need to create a new list object to store our
            # GameTiles in our dictionary
            if parts[0] not in gameTiles:
                gameTiles[parts[0]] = []
            else:
                # Otherwise we can add our gameTile to gameTiles. Notice that the category is the key
                gameTiles[parts[0]].append(gameTile)

        # This handles the final jeopardy case
        elif parts[0] == 'FJ':
            # gameTiles uses FJ as the key for Final Jeopardy and then we store a FinalJeopardyTile
            gameTiles['FJ'] = FinalJeopardyTile(question=parts[1], answer=parts[2])
        else:
            # Throw our custom exception
            raise InvalidGameFile
    return gameTiles  # Return our dictionary that contains our question and final Jeopardy

Read a file

The first thing we do is open a file. Python makes it super easy to read an entire file into memory with just one line of code!


The open fucntion opens the file. It takes the path to the file (which is stored in gameFile and a flag that tells it kind of open operation we are performing. In this case 'r' means read. Then we just use the readlines() method to read the entire file into memory.

<h4>Check for the proper number of lines</h4>
Our file is only valid if it has 28 lines. Let's check that next.

if len(rawLines) != 28:
    raise InvalidGameFile

Whenever you need to know the len of a string, list, tuple, etc, you can use the built-in len function. In our case, we are calling len on our rawLines variable. If the result is anything beside 28, our file is invalid and we will raise our custom InvalidGameFile exception.

Remove unwanted characters

Our next job is to remove any special characters such as newline '\n' from the game file. The way we do it is below

fileLines = []
for line in rawLines:
    fileLines.append(line.strip())

This code starts by creating an empty list object to store the modified file lines. Then we enter a for loop to go through each item in rawLines one item at a time. We use String's strip() function to remove unwanted characters and we then add it to fileLines.

Validate Categories

Our requirements expect us to have five unique categories. Here is the code that validates the categories.

categories = fileLines[0].split('::')

if len(set(categories)) != 5:
    raise IllegalCategoryCount 

Python Strings have a split method that will split a string into parts based on a supplied character. In our case, we use the :: character to section off each part of our line. Doing so removes the :: characters in the line and it returns a collection of Strings. One item for each portion of our String.

Our next job is to make sure we have five unique categories. We already met the len() function above, but now we have the set() function. This function takes an iterable and removes all duplicates. By wrapping len() around a set() and checking for five, we can be sure that we only have 5 unique categories. Any more or less categories will cause us to raise our custom exception.

Validate Points

We also require that we have 5 unique values for points. We can do this very similar to categories with only a small change.

points = []
pointValues = fileLines[1].split('::')

for pv in pointValues:
    points.append(int(pv)) # Convert to integer here

if len(set(points)) != 5:
    raise IllegalPointsCount

When we start by spliting our string, we end up with hopefully five strings. Python is a strongly typed language. If we want to make a number out of a String, we have use the appropriate function to do so. In our case, we use int() to convert a String to an integer (whole number) value.

If you attempt to call int() on a String that isn't a number, you will get an exception and your program will crash. This is a good thing because we can trace the program back to the point of failure and debug it.

Other than converting the Strings to integers, the logic of this section of code is identitcal to categories.

Creating the GameTiles and Final Jeopardy

Our last order of business is to process the remainder of the file and create our game tile and final jeopardy objets.

gameTiles = {}

for line in fileLines[2:]:  # Slice off the first two lines
    parts = line.split('::')

    if parts[0] in categories and int(parts[1]) in points:
        gameTile = GameTile(category=parts[0],
                            points=int(parts[1]),
                            question=parts[2],
                            answer=parts[3])

        if parts[0] not in gameTiles:
            gameTiles[parts[0]] = []
        else:
            gameTiles[parts[0]].append(gameTile)

    elif parts[0] == 'FJ':
        gameTiles['FJ'] = FinalJeopardyTile(question=parts[1], answer=parts[2])
    else:
        raise InvalidGameFile
return gameTiles

We begin by creating a dict object that will hold our GameTile and FinalJeopardyTile objects. The categories will be used as the keys, and the value will be a list. Combining a dict and a list in this fashion let's use associate multiple values with a single key.

Now we are going to iterate through the remaining lines in the file. Notice that we use the slice operator [2:]. This is a neat Python trick that let's use exclude the first two entries in our list, which we do because we have already processed those lines at this point.

Next we need to validate the category and points that are associated with each question. We can check that parts[0] (the category) is in our category list that we created earlier. The same holds true for points (parts[1]) but notice that we convert to int here again. If the category and points are value, we move onto to create a GameTile using it's constructor.

Next, we need to check if gameTiles has a list associated with a category. If it doesn't have one yet, we need to create it. After that, we can look up a list with a category and then append the GameTile to that list. This keeps the questions organized by their category.

The elif block handles the final jeopardy case. If the parts[0] == 'FJ', we have ran into our final jeopardy question. In this case, we create a special FinalJeopardyTile and associate it with the 'FJ' key in the dictionary. There is no list here because we only want to hold one final jeopardy object.

Finally we can return our newly constructed dictionary.

Running the code

Python make it easy to develop applications quickly because we can easily run modules as standalone applications.

if __name__ == '__main__':
    print(readGameBoard('questions.txt'))

This code only executes when the module is ran on it's own. When it is run, it will look for a file called questions.txt in the same folder as the module. When run successfully, we get this output.

{'Earthquakes': [{'category': 'Earthquakes', 'points': 800, 'question': "... (continues)

You can get the entire code for this tutorial from here: python_tutorial_1

Advertisements

Java Data Types

Almost everything we do in computer programming involves data. In many cases, the data itself is more valuable then the program. Many tax programs are written, but they all work on the same data. We may write tax software that displays fancy charts of your AGI over a period of 5 years, or we may write a program that calculates your net income. These programs are very worthwhile without data.

Java has two main ways of representing data. The first set of data types are called primitives. They are as follows

  • byte (8-bit signed)
  • short (16-bit signed)
  • int (32-bit signed)
  • long (64-bit signed)
  • float (32-bit)
  • double (64-bit)
  • boolean (true/false)
  • char (16-bit unicode characters)

Whole Numbers

Let’s discuss each of these data types in turn starting with byte. A byte is smallest integer variable available to Java programmers and can hold values between -128 and 127. I generally use bytes in situations where I need to be conscious of my memory usage or when I’m programming on the web and need to transfer information from one site to another.

We can make a byte like this

byte b = 120; //This variable stores the number 120

Next up the ladder is the short. Like bytes, shorts contain whole numbers between -32768 and 32767. Use shorts when you need to be concerned about memory usage.

Create a short by

short s = 120; //This variable also store the number 120

The next primitive data type is int. This is one of the most commonly used numeric data type in Java programs. The int primitive data type stores values between -2147483648 and 2147483647.

Create an int by

int i = 1; //This int variable holds the number 1

The long is the final whole number data type. At 64-bits this variable comes in as a monster that can store values between -9223372036854775808 and -9223372036854775807. Longs make great candidate in scientific applications where the program may end up processing large sets of data. I also see them used to store primary keys in database programming.

Create a long by

long l = 100; //This long variable holds the number 100

Decimals and Fractions

There are times where you need to store numeric data that is not a whole number. For example, in many cases you may need to use the number PI (3.14159) in a calculation. Our next two data types hold such numbers.

The first is float, which is a floating point number (hence the name). This is a 32-bit variable that can store 1.4E-45 to 3.4028235E38. Double is the more commoly used data type (64-bits) and stores values ranging from 4.9E-324 to 1.7976931348623157E308.

float f = 5.99; //Make a float that stores the number 5.99
double d = -1.00; //Make a double that stores the number -1.00;

Never store money in these data types! The internal mechanics of these data types cause a loss in percision that can result in inaccuracies. Money should always get stored in the BigDecimal object.

Non Numeric Data

Java has the ability to store non-numeric data. Sometimes all a programmer needs to know if when something it True or False. The boolean data type is used for such a purpose.

boolean bTrue = true; //This boolean is true
bTrue = false; //Now it's false

We can also store unicode characters with the char type.

char c = 'a'; //Store the letter a
char tab = '\t'; //Stores  a tab character

Objects

Java also supports another data type that is not a primitive. These are called objects (to be discussed in another post). At this point there is one critical object to discuss that is called the String. String is a special kind of object that supports words and textual data.

//Since Strings are objects, so we use the 'new' keyword to create them
String str = new String("Hi! I'm a string of letters");

//But you can also do this since Java has special built-in support for Strings
String anotherString = "Hi! I'm another String!";

A working example

Below is a working example program that you can use to see the data types discussed above in action.


public class DataTypes {

    public static void main(String [] args){
        byte byteMin = Byte.MIN_VALUE;
        byte byteMax = Byte.MAX_VALUE;

        System.out.println("Bytes are the smallest data types (8-bit) and represent whole numbers.");
        System.out.println("The smallest value you can store in a byte is " + byteMin + " and" +
                " the maximum value is " + byteMax);
        System.out.println("");

        short shortMin = Short.MIN_VALUE;
        short shortMax = Short.MAX_VALUE;

        System.out.println("Shorts are one step larger than bytes (16-bit) and represent whole numbers.");
        System.out.println("The smallest value you can store in a short is " + shortMin + " and" +
                " the maximum value is " + shortMax);
        System.out.println("");

        int intMin = Integer.MIN_VALUE;
        int intMax = Integer.MAX_VALUE;

        System.out.println("Integers are one step larger than shorts (32-bit) and represent whole numbers.");
        System.out.println("The smallest value you can store in an integer is " + intMin + " and" +
                " the maximum value is " + intMax);
        System.out.println("");

        long longMin = Long.MIN_VALUE;
        long longMax = Long.MAX_VALUE;

        System.out.println("Longs are the largest of whole number data type (64-bit).");
        System.out.println("The smallest value you can store in a long is " + longMin + " and" +
                " the maximum value is " + longMax);
        System.out.println("");

        float floatMin = Float.MIN_VALUE;
        float floatMax = Float.MAX_VALUE;

        System.out.println("Floats represent decimal types. (32-bit)");
        System.out.println("The smallest value you can store in a float is " + floatMin + " and" +
                " the maximum value is " + floatMax);
        System.out.println("");

        double doubleMin = Double.MIN_VALUE;
        double doubleMax = Double.MAX_VALUE;

        System.out.println("Doubles also represent decimal types. (64-bit)");
        System.out.println("The smallest value you can store in a double is " + doubleMin + " and" +
                " the maximum value is " + doubleMax);
        System.out.println("");

        boolean booleanFalse = false;
        boolean booleanTrue = true;

        System.out.println("Booleans store true or false values.");
        System.out.println("booleanFalse = " + booleanFalse);
        System.out.println("booleanTrue = " + booleanTrue);
        System.out.println("");

        char charMin = Character.MIN_VALUE;
        char charMax = Character.MAX_VALUE;

        System.out.println("char represents a 16-bit unicode character");
        System.out.println("The smallest char value is " + charMin + " and the largest is " + charMax);
        System.out.println("");

        String string = new String("I'm a string of text");
        System.out.println("Java also has the Object data type. This is different from our other data types.");
        System.out.println("It this example, we have and object that is a String. Strings hold text.");
        System.out.println("string = " + string);
    }
}

This is the output of the program when executed

Bytes are the smallest data types (8-bit) and represent whole numbers.
The smallest value you can store in a byte is -128 and the maximum value is 127

Shorts are one step larger than bytes (16-bit) and represent whole numbers.
The smallest value you can store in a short is -32768 and the maximum value is 32767

Integers are one step larger than shorts (32-bit) and represent whole numbers.
The smallest value you can store in an integer is -2147483648 and the maximum value is 2147483647

Longs are the largest of whole number data type (64-bit).
The smallest value you can store in a long is -9223372036854775808 and the maximum value is 9223372036854775807

Floats represent decimal types. (32-bit)
The smallest value you can store in a float is 1.4E-45 and the maximum value is 3.4028235E38

Doubles also represent decimal types. (64-bit)
The smallest value you can store in a double is 4.9E-324 and the maximum value is 1.7976931348623157E308

Booleans store true or false values.
booleanFalse = false
booleanTrue = true

char represents a 16-bit unicode character
The smallest char value is  and the largest is ￿

Java also has the Object data type. This is different from our other data types.
It this example, we have and object that is a String. Strings hold text.
string = I'm a string of text

Getting Started

Java is one of the most widely used programming languages today. According to Oracle, over 3 Billion devices run Java. Java is used in a wide variety of applications. For example, many Android applications are written in Java. Java is used in Web applications that power eCommerece stores that connect to databases or even other websites using technologies such as REST. Many people have written desktop Java applications that make schedules or handle payroll. I have even seen Java powered cash registers.

The point is that anyone who grabs a solid understanding of Java will not struggle to find work in the foreseeable future. As of this writing, the Java platform is in its 8th version with JDK 9 on its way. Java development has a massive community of people who are happy to share programming techniques and help others improve their code. Companies such as Google contribute to Java by publishing software libraries to enhance the language.

However, to get started in Java, we need to install our build tools. The Java Development Kit (JDK) can be downloaded from Oracle’s website here. Alternatively, you can also use OpenJDK which is a community drive version of Java. Install either of these software packages will install the Java Runtime Environment (JRE), the Java compiler, and related software libraries on your system that make Java development possible.

Now let’s write our first Java application. To do this, you need to use a plain text editor (on Windows, that would be notepad. Apple users may use TextEdit). In the end, Java source files are files that end with the .java extension. Once you have your text editor open, start by typing the following code.

 
public class HelloWorld { 
    public static void main(String [] args){ 
        System.out.println("Hello World"); 
    } 
} 

Once you have this code inserted into your text editor, you should use the save as feature and save this file HelloWorld.java. This is a critical step because required the name of the file to match the name of the class (this is the name that follows the keyword class, so in our case, it’s HelloWorld). Once you have saved the file, you will need to open your system’s command prompt or terminal.

At this point, you need to compile your java file. Java is a compiled language that relies on the Java compiler to turn your *.java into an *.class file. To run the java compiler, navigate to the folder (or directory) that you saved your HelloWorld.java file in and then type.

javac HelloWorld.java

If all goes well, you will have a HelloWorld.class file in that folder next to HelloWorld.java. To run the program type:

java HelloWorld

Your system will print:

Hello World

Congratulations! You have wrote, compiled, and ran your first Java program!