CSCI 101 - Intro to Computer Science

Python Lab

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

Home |  Contact |  Schedule |  Assignments |  Syllabus |

Python Lab 9: Parse the Declaration
Due Tuesday, November 2nd, 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 managing all your Python Labs this semester. Please save this file with the following name: Lab9-parse.py.

Assignment

Nicholas Cage once said the bold phrase, "I'm going to steal the Declaration of Independence." In this lab you need to read in the Declaration of Independence and allow the user to either (1) output the number of occurrences of a given word or (2) output the number of words that have a specific length and then the count of unique words of that same length. The focus of this lab is on the fundamentals of file I/O.

To begin this lab, download the Declaration_of_independence.txt file and ensure you place it in the same folder/directory as your Python code for this lab. Your task is to write a program that :
  • Reads in the Declaration of Independence file. During this step, you should also remove all punctuation marks from the string.punctuation list: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
  • Asks the user whether they would like to print the number of times a specific word appears OR print the number of words of a specific length.
  • If print the number of times a specific word appears:
    • Ask the user for a word to count.
    • Remove punctuation and capitalization from user input.
    • Count the number of times that word appears in the file.
    • Output the count.
  • If print the number of words of a specific length (do not count any words that only contain numeric characters):
    • Ask the user for a specific length (i.e., N).
    • Count the number of words that appear in the file that have length N. If a word appears more than once, count it more than once.
    • Output this count.
    • Count the number of unique words that appear in the file that have length N. This time if a word appears more than once, only count it once.
    • Output this count.
Note: You should ignore all casing, e.g., "We" and "we" should be considered the same word.

Hints

  • Python functions that might be useful to research and/or utilize:
    • file = open(fileName, 'r'), file.close()
    • with (fileName, 'r') as file:
    • string.punctuation
    • string.replace()
    • string.lower()
    • string.split()
    • string.count()
    • len(string)
    • isalpha(string)
  • You CANNOT change the name of the Declaration_of_independence.txt file and you must use a relative path to access it. Otherwise the autograder will fail.

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: CHOICE> and ( WORD> or LENGTH>) .
  • When providing output that will be graded, start the line with OUTPUT. Think of this as "boxing your answer" on a math worksheet; it lets us quickly find your answer. Our grading script will skip any output lines that do not start 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 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:

Would you like to print the number of times a specific word appears OR print the number of words of a specific length? (1 or 2)
CHOICE> 1
Enter a word:
WORD> America
The number of times America appears in the document is:
OUTPUT 2


Example Execution 2:

Would you like to print the number of times a specific word appears OR print the number of words of a specific length? (1 or 2)
CHOICE> 1
Enter a word:
WORD> self-evident
The number of times self-evident appears in the document is:
OUTPUT 1


Example Execution 3:

Would you like to print the number of times a specific word appears OR print the number of words of a specific length? (1 or 2)
CHOICE> 1
Enter a word:
WORD> consession
The number of times concession appears in the document is:
OUTPUT 0


Example Execution 4:

Would you like to print the number of times a specific word appears OR print the number of words of a specific length? (1 or 2)
CHOICE> 2
Enter a length:
LENGTH> 4
The number of words in the document with length 4 is:
OUTPUT 185
The number of unique words in the document with length 4 is:
OUTPUT 75


Example Execution 5:

Would you like to print the number of times a specific word appears OR print the number of words of a specific length? (1 or 2)
CHOICE> 2
Enter a length:
LENGTH> 19
The number of words in the document with length 19 is:
OUTPUT 0
The number of unique words in the document with length 19 is:
OUTPUT 0


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 message from Gradescope, this information is incorrect. 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:
        # Tracy Camp
        # CSCI 101 Section B
        # Python Lab 9
        # References: Instructor Kenzie helped me debug
        # References: Learned I/O syntax at https://www.geeksforgeeks.org/file-handling-python/
        # 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 > Lab9 and upload Lab9-parse.py.

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

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