Functions

Functions. They make programmers look good. Instead of having a single main() function with 1,200 lines of code in it, you can now break up your code into nice, reusuable pieces of code. Commander Data is fully functional, he knew it was a good idea.

Your Assignment

In this assignment you will revisit reading data from a file, and use that data as arguments (parameters) for a number functions you will write.

You will need to:
  1. Write and test a function square_each(nums), where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, return will not be allowed!
  2. Write and test a function sum_list(nums), where nums is a (Python) list of numbers. It returns the sum of the numbers in passed-in the list.
  3. Write and test a function to_numbers(str_list), where str_list is a list of strings, each of which represents a number. Modify each entry in the list by converting it to a number. You can choose to use return the list, or just modify str_list in-place, similar to #1, above.
  4. Take the previous three functions and construct a full program (that is, a main() function that will call these functions.) The program computes the sum of the squares of numbers read from a file. Your program should prompt for a file name and print out the sum of the squares of the values in the file. Hint: use readlines()

You may download two example files of numbers to read in, numbers (space seperated) or numbers (comma seperated). You may choose to read in numbers in another format of your choosing, so long as you choose one method and document it in your program comments.

Submission Requirements

There are a few standards to adhere to when submitting your lab. Please read, and follow them carefully.

  1. Submission Filename: Submit a file named lab_functions.py.

  2. Documentation String (“docstring”): The following docstring (properly edited with your name) needs to be the very first thing in your Python program file.

    """CSC 161 Lab: Functions
    
    This lab...
    
    Fox Mulder
    Lab Section TR 2:00-3:15pm
    Fall 2015
    """
    

    Read more about docstrings: Program Documentation String

  3. Comments (lines prefaced with #): Unclear parts of your code can receive point deductions, so be judicious about using comments. Do not overuse comments. Too much unnecessary commenting can complicate reading otherwise clear code!

    Read more about comments: Program Comments

  4. Python Code Style: You will need to run your Python program file through the online PEP 8 style checker found in CSC 161 Style.

The Blackboard post for this lab assignment will contain the details regarding the due date and other details regarding class administration for this assignment.

Grading

The following is the grading rubric for this lab to be used by the course graders. The lab is worth 100 points.

Reasonable variations of this lab should be acceptable. In particular, we would accept programs that take input files with different formats (comma separated, space separated, one number per line, …)

Here’s a brief grading rubric:
  • Syntax errors: -30 points
  • A bad/missing docstring (i.e. no name, lab section, etc): -10 points
  • Missing main() function: -10 points
  • Code doesn’t attempt to adhere to Python coding style: -10 points
  • The submitted filename is wrong: -5 points
  • No Python comments used when they are needed to clarify code: -5 points
  • Logical errors in the functions: -5 points
  • The program is required to have four functions (main(), square_each(nums), sum_list(nums), to_numbers(strlist)). Failure to define your functions exactly as described: -15 points for each instance.
    • The function square_each(nums) is required modify its parameter, the list nums. This demonstrates that you can “pass-by-value” and still modify an function parameter. Failure to do this (e.g. by using return instead): -7 points.
    • The function sum_list(nums) takes as a parameter the list nums, and computes a single sum value of all the numbers in this list. Failure to return this sum as a int or a float value: -7 points.
    • The function to_numbers(strlist) can modify its parameter, the list nums or construct a new list and return it. Failure to do this: -7 points.
    • The function:samp:main() needs to properly call your three functions, in the right order and also print out the sum of the squares if the numbers you read in. If not, -7 points.
  • You need to demonstrate you understand how functions work together to solve a program. This means using function parameters and return statements to pass information back and forth. Failure to do so will incur point deductions by the graders.
Extra Credit:

Can you make your program flexible enough to read in data that is separated by either commas or spaces? If so, +5 points.

I will allow you to add something creative and interesting to your program for extra credit. Your extra credit additions needs to be relevant to the techniques we are learning. You are not guaranteed to receive extra credit, it will be up to the lab TA to decide. If your extra credit code is not clearly documented using comments, you risk losing points instead of receiving bonuses.

Note: Extra credit is added your normal submission file, do not submit a second file. Extra credit supersedes any of the above grading rubric if it conflicts.

Note 2: There may be additional criteria for grading, this is just a summary of the major rubric items.