The Main Event

main.py: 4 points


Objective

In this step we’ll set up main.py. This file will be the entrypoint to our sorting visualization: it’s the file users should run when they want to use our program, and it will give them access to all of the functionality we want to make available.

How to Complete This Section

Most of main.py is written already—you’ll only need to change one function to get it working. Of course, you first need to understand what you’re editing, so take a moment to read through the file. Using what you’ve learned so far, try to figure out how the program works and what it’s going to do by reading the code itself.

When you’ve finished, continue through the instructions below to verify the program is doing what you expect and to implement the missing functionality.

main()

You’ll notice this function has been written for you: you need to understand it, but you should not edit it. Note that it makes use of the Menu class you just wrote. Try running python3 main.py to see what happens—if your Menu class is written correctly you should be prompted to choose an algorithm and a visualization mode. Here’s an example of the expected output:

Welcome to the sorting algorithm visualization tool!
The following algorithms are available:
    1: Selection Sort
    2: Insertion Sort
Please enter the number corresponding to your choice: 1
You selected: Selection Sort
Supported visualization modes:
    1: Text
Please enter the number corresponding to your choice: 1
You selected: Text

Crucially, you’ll observe that once the requested input is received the program promptly quits without running any demo whatsoever. This is because the function responsible for actually running the demo (cleverly named run_demo) has not yet been implemented. We will now implement it in accordance with the requirements described in the next section.

Caution: please do not edit main()

Your program should use the provided main() function without modification. Changing main() will cause you to lose points on this section.

run_demo(choices)

This is the part you need to write yourself. The purpose of this function is to run the visualization the user is requesting. To accomplish this, we must do three things:

  1. Understand the user’s input.
  2. Generate some data to sort.
  3. Run the appropriate demo on the data.

See below for further details about each of these requirements.

1. Understanding user input

The behavior of our function should be determined by the content of the parameter choices, which is a list of valid user inputs collected in main(). Since we only ask the user for two inputs, there are only two items in the list, corresponding to:

  1. the algorithm selected, and
  2. the desired visualization mode.

For example, if the user requested the algorithm “Insertion Sort” and the visualization mode “Text,” choices will be the list ["2","1"]. (Don’t worry about the fact that there is only one choice of visualization mode. We’ll add another in the next section.)

2. Generating data to sort

Before we can run our sorting visualization, we need some data to sort. To keep things simple, create a DataSet object containing 15 random values ranging between 0 and 15.

Some remarks about this design:

  • Since only Text mode is currently supported, the DataSet Class has all the features we need for now. This will change soon when we add graphical support.
  • The numbers 0 and 15 are arbitrary, but picking some fixed suitable values greatly reduces the complexity of our program, and using relatively small numbers helps to avoid running out of space in the terminal. In one of the extra credit options, you’ll have the opportunity to come back and give the user greater control over the data to be sorted.
3. Run the right demo

To honor the user’s request, we need to run the sorting demo that corresponds to the algorithm they are requesting. Once you have processed the user’s input, run the visualization corresponding to their selection on the DataSet object you created.

Looking Ahead: Extensible Design

As mentioned above, we’ll come back to update this part of the program in subsequent sections to make new features available to the user as we complete them. To make life better for your future self, try to write your code in such a way that makes it easy to extend with new features that may come later. For example, you should consider what needs to be done to support new menu options, including the ability to choose additional sorting algorithms or request a different class for generating and displaying data.