CSCI 102 - Intro to Computer Science LAB

Python Assignment

Suggested Order: 102 Week06 Studio; 102 Week6A; 101 Lab9; 102 Week6B; 101 Lab10

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


Week 06 Python Assignment
Due by Monday, August 16th, 11:45PM
        Part A: Word Search
        Part B: Parse Depth Range


Welcome to your assignment for Week 06 of CSCI 102! Each week, after Studio, you should work on that week's homework assignment (lab). This lab is to be done on your own (not pair programming), and needs to be completed and submitted to Canvas no later than the due date. Unlike CSCI 101, we will place all assignments for a given week on one HTML page. This is your Week 6 CSCI 102 Assignment page.

You have TWO problems to do this week, both of which you will SUBMIT to Canvas. Details on Part A and Part B are below.

Part A: Word Search (5 points)

Introduction

To get started, open IDLE and create a New File via the File menu. We suggest you immediately save this file in the directory managing all your 102 Python Labs this semester. Please save this file with the following name: Week6A-word_search.py.

Problem Statement

For this lab, you'll need this dictionary.txt file in the same directory as where you are developing the Python code for this lab. This file contains 127,142 English words (where each word is placed on a new line). Your task is to write a program that has the user input an integer, which represents the length of a word the user desires. Your code should then output to the console (1) the number of words in the dictionary that have the same length as the user input and (2) a random word of that length. Specifically:
    • Read in the dictionary file.
    • Prompt the user to enter the word length of interest (i.e., N).
    • Output the number of words in the dictionary that have length N.
    • Seed the random function with N.
    • Of the words that have length N, choose one word randomly and output it.
If there are no words of length N, output "None".

Lab I/O Format

Throughout this semester we will often 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, this lab might choose: 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.
  • You are welcome (and should!) have other output lines that are informational to the user of the program.

Example Execution 1 (depending on your implementation, your lab may not choose "trivia")

Enter the length of the words to find:
LENGTH> 6
The number of words with length 6 is:
OUTPUT 14383
Here is one random word with length 6:
OUTPUT trivia

Example Execution 2

Enter the length of the words to find:
LENGTH> 29
The number of words with length 29 is:
OUTPUT 1
Here is one random word with length 29:
OUTPUT floccinaucinihilipilification

Example Execution 3

Enter the length of the words to find:
LENGTH> 45
The number of words with length 45 is:
OUTPUT 0
There are no words of length 45 in the dictionary.
OUTPUT None

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 102 – Section B
        # Week 6A - Word Search
        # References: Instructor Rena for file reading and TA Tori for random number generation
        # Time: 25 minutes

Part B: Parse Depth Range (9 points)

Introduction

To get started, open IDLE and create a New File via the File menu. We suggest you immediately save this file in the directory managing all your 102 Python Labs this semester. Please save this file with the following name: Week6B-depth_range.py.

© Mines Geology Trail

Problem Statement

When analyzing subsurface geologic formation data, often the data is presented in the following comma-delimited format:

Depth,Formation
0-600.5,Green Mountain Conglomerate
600.5-1505.65,Denver Formation

Unfortunately, this format is undesirable because it is not ideal to graph a "range" (e.g., 0-600.5) on x and y axes; one would much rather graph one number, typically the formation tops. Your task for this problem is to parse the depth ranges within the formations.csv file and add three new columns: start depth, end depth, and average depth. For example, if you parsed the example data given above, it would look like the following:

Depth,Start Depth,End Depth,Average Depth,Formation
0-600.5,0.0,600.5,300.2,Green Mountain Conglomerate
600.5-1505.65,600.5,1505.65,1053.1,Denver Formation

Write a program that parses the depth ranges in the formations.csv file. You need to extract the start and end depths from the range (e.g., take the string 600.5-1505.65 and extract the floats 600.5 and 1505.65), calculate the average depth (rounded to 1 decimal place), and add these three new columns for each formation. Write both the original data and the three new columns of data to a new CSV file, formations_parsed.csv. Include the new header row in the new file as well.

A few notes:
  • You should save the formations.csv file in the same directory as your Python file.
  • The header of each file (the first row) should always be identical for each input file as well as identical for each output parsed file.
  • Your program should create a new file called formations_parsed.csv.
  • We encourage you to open formations_parsed.csv in a text editor (such as Notepad, NOT Excel) and make sure the contents of formations_parsed.csv match the sample output.

Lab I/O Format

There is not any user input/ouput for this lab. Your program should read in a file and create a new file without any user input.

Example Execution #1

Sample Input 1 (File formations.csv)
Depth,Formation
0-600.5,Green Mountain Conglomerate
600.5-1505.65,Denver Formation
1505.65-1805.65,Arapahoe Formation
1805.65-2410.83,Laramie Formation

Sample Output 1 (File formations_parsed.csv)
Depth,Start Depth,End Depth,Average Depth,Formation
0-600.5,0.0,600.5,300.2,Green Mountain Conglomerate
600.5-1505.65,600.5,1505.65,1053.1,Denver Formation
1505.65-1805.65,1505.65,1805.65,1655.7,Arapahoe Formation
1805.65-2410.83,1805.65,2410.83,2108.2,Laramie Formation


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 102 – Section B
        #  Week6B - Parse Depth Range
        #  References: TA Erin helped me read in the CSV file
        #  Time: 60 minutes

Submit Solutions

Follow these steps to submit your files to Canvas.
  1. In Canvas, go to Assignments > Week6A and upload Week6A-word_search.py.
  2. In Canvas, go to Assignments > Week6B and upload Week6B-depth_range.py.
To receive full credit, your code must execute in Python 3, and you must submit a single file for each portion of the assignment (your Python code file). In addition, your code must follow the Lab I/O Format.

Whenever you submit something to Canvas, 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.