CSCI 101 - Intro to Computer Science

Python Lab

Quick Links: zyBook | Piazza | Canvas | CS @ Mines

Home | Contact | Schedule | Assignments | Syllabus |

Python Lab 10: Math in Another Universe
Due Tuesday, November 9th, by 11:45pm


Introduction


To get started, open IDLE and then create a New File via the File menu. We suggest you immediately save this file in the directory you created to manage all your Python Labs this semester (e.g., CSCI101/PythonLabs). Save this file as Lab10-math.py.

Assignment:

The year is 2069. Thanks to Ludwig Musk's recent research at Colorado School of Mines, cars are able to make wormholes and travel through them to other universes at a very high speed! One day you decide to travel to another universe for fun. While there, you notice that your TI-84 Plus calculator (who would have thought that a calculator developed in 2004 would still be used in 2069?) failed to solve most of the math problems in this alternate universe. You discover that, in this universe, all operators (addition, subtraction, multiplication, and division) have the exact same precedence and are just performed left to right (e.g., 3+3*5=30). In addition, math is comma separated instead of space separated (e.g., 3,+,3,*,5). What an interesting universe!

Your task is to write a program that can solve math problems in this alternate universe. Your program should request the name of a math input file. (You can assume the file is available.) Each line of this file contains a comma-separated math expression consisting of +, -, *, /, and integers (no parentheses). As you read in the file, each numerical input should be processed as an INTEGER. Parse and evaluate each expression and write all of the results (each rounded to the nearest integer, i.e. 99.99 -> 100), comma-separated, to the first row of the output file (name chosen by the user). See the execution below for an example.

Note: You should round your result to the nearest integer after you have completed the entire calculation. You will need to the round function rather than an integer type conversion (it rounds down).

Notes

  • Some useful functions to research:
    • with open(fileName, 'r') as file:
    • csv.reader(file)
    • with open(fileName, 'w') as file:
    • csv.writer(file, delimiter=',')
    • round()
  • You must use a relative file path for your input and output .csv files. Any absolute paths that are specific to your computer will not function within the autograder.

Lab I/O Format

Throughout this semester we use a specific Lab Input/Output Format. This format is described below:
  • When prompting for input, use the prompt string WORD> where WORD is a single, uppercase word which describes the input. For example, your WORD in this lab might be: MATHFILE> and OUTPUTFILE>.
  • The graded output for this lab will be a csv file, not what is printed in the terminal. Unlike previous labs you will not need to print any lines that begin with OUTPUT.
  • You are welcome (and should!) have other output lines that do not begin with OUTPUT; while our grading script will ignore these, good programmers include print statements that are informational to the user of the program.
  • A submission without exactly correct output formatting of the csv file will receive an AUTOMATIC ZERO. This is because Gradescope is automated—it does not look at your code, only the results, and thus the format of the results must be consistent for all students.

Example Execution #1

Sample Input 1: (contents of math.csv)
5,*,5,/,5
5,+,-5,*,500,/,57,/,34,+,6
34,-,32,*,5,*,-1,/,-1,+,4
20,/,7
36
-6,-,-5
23,/,8,*,15

Program Execution:
Enter the name of the file containing the math problems.
MATHFILE> math.csv
Enter the name of the file in which to store the results.
OUTPUTFILE> results.csv

Sample Output 1: (contents of results.csv)
5,6,14,3,36,-1,43


Gradescope Submission Nuances

When you submit your Python file to Gradescope, multiple different test cases are run on your code. Passing all of the tests results in a 100% on the autograded portion of the lab.

You are allowed to submit to Gradescope four times (or less) for this lab. Please IGNORE the automated submission email from Gradescope, this information is not correct. The maximum grade of your submissions will be your grade for the lab. Note: If your code doesn’t work (e.g., a syntax error exists, or an error is thrown in execution), then you will receive an AUTOMATIC ZERO. You should test your code before submitting to ensure it executes correctly.

Comments

All Python files should include a header with your name, section, assignment info, references (i.e., who did you collaborate with on this assignment?; what resource did you use?), and approximate time taken to do the assignment. Be sure to cite any allowed external references used to complete the assignment. Any code without this header will lose 1 point. Here's an example:
        #  John Henke
        #  CSCI 101 – Section B
        #  Python Lab 10
        #  References: Used the following site for reading CSV file syntax
        #  References: https://realpython.com/python-csv/
        #  Time: 45 minutes

Submission

Once you are satisfied with your solution to this lab, you need to submit the file to Gradescope. In Gradescope, go to CSCI 101 > Lab10 and upload Lab10-math.py.

Note: this lab is worth 7 points. To receive full credit, your code must execute in Python 3, and you must submit a single file (your Python code file).

Whenever you submit something to Gradescope, we strongly recommend you always double check what you submitted actually got submitted correctly (e.g., did the file upload correctly? did you submit the correct file? etc.) If your submission is incorrect, it's on you.



Valid HTML 4.01 Strict Valid CSS! Level Triple-A conformance, W3C WAI Web Content Accessibility Guidelines 2.0