CS 100  Programming I
Project 0

Revision Date: January 8, 2017

Printable Version


You may develop your code anywhere, but you must ensure it runs correctly on a Linux distribution before submission.


Efficiency is doing things right; effectiveness is doing the right things - Peter Drucker

Dr. Lusth, perhaps the oddest member of the faculty, is at it again. Last fall, he was obsessed with hypermiling; this fall, it's fixed-gear cycling. With a fixed-gear bike, there are no shifters and, more importantly, the pedals are always moving if the bike is moving. In other words, you cannot coast on a fixed-gear bike. Because of the direct tie between the rear axle and the pedals, it is possible to slow down a fixed gear bike by resisting the circular motion of the pedals. Indeed, accomplished fixed-gear cyclists (in the venacular, known as hipsters) can lock the rear wheel and skid to a halt, using just the pedals. In fact, use of a hand-brake at all is considered whimpy.

The Killer Hill

On Dr. Lusth's commute into work, there is one killer hill that has a stop sign at the bottom. Based upon an experiment he performed (don't try this at home, kids), he knows that it takes him 340 feet to completely stop by resisting pedal motion, starting at the top of the hill. A stop sign, however is only 120 feet away from the top of the hill.

Not wanting wimp out by using the brakes, Dr. Lusth had a brilliant idea. What if he slalomed down the hill, in essence, tracing out a sine wave? This would increase the distance he travelled dramatically and should allow him to stop, without using brakes, before zipping out into cross traffic and joining the bugs on a grill of a passing Yukon Denali 4x4.

[For those of you who fear for Dr. Lusth's safety when zig-zagging down the hill, know that there is hardly ever any traffic on this hill.]

Excited by this prospect, Dr. Lusth scanned the interwebs to see if there was a formula that would give him the length of a path traced out by a sine wave. Unfortunately, not being a math wiz, he could not find a formula he understood. He did realize, however, that he could use Python to write a program to approximate this value by taking the distance between points along the curve. And so he started on a module that estimated the length of a sine wave. Here is what he wrote:

import math
# A module for approximating the length of the path traced out
# by a sine wave.
# written by John C. Lusth

# compute the length of the wavy line traced out by a sine wave
# via approximation. The value of 'step' controls the accuracy of
# the approximation. The 'xDistance' is the distance along the
# x-axis.

def sineLength(amplitude,period,xDistance,step):
    x = 0.0
    total = 0.0
    lastx = 0
    lasty = 0
    while x <= xDistance + (step / 2):
        y = amplitude * math.sin(2 * math.pi / period * x)
        d = distance(lastx,lasty,x,y)
        #print("    distance from (",lastx,",",lasty,") to (",x,",",y,") is",d)
        total = total + d
        lastx = x
        lasty = y
        x = x + step
    return total

# compute the distance between point (a,b) and point (c,d)

def distance(a,b,c,d):

As you can see, Dr. Lusth only got so far before school started and he got pulled away. Your task is to finish his program. You may not change the sineLength function; you should only finish the distance function.

You can search the Internet to find out how to compute the distance between two points, if you don't know how to do so already.

Program Organization

Create a project0 directory off of your cs100 directory and move into project0.

Name your main python file stopping.py. Provide one more file named support.py Dr. Lusth's code should go into the support.py file. The main function in stopping.py should call a function that prompts the user of your program for the following information, in the order given.

Your main function should call at least one of the functions in the support file. You will need to import these functions into your main program by placing this line at the top of stopping.py.

    from support import *

NOTE: Only your main function should print the results; the functions in support.py should only compute the values to be displayed, not display them:

    def main():
        # read in the information
        # compute distance
        # print out the three distances, the two
        # original distances plus the length of
        # the weaving path


Also, your stopping.py file should have two function definitions in it, the definition of main and the definition of a function that obtains information from the user.

If you wish, your function to get the information can be called getInfo(), or you can name it something else. But all your function names should be meaningful and appropriate for the program being written.

Getting the numbers

Here is how to get two numbers and return them:

    def getInfo():
        m = eval(input("Give me a number: "))
        n = eval(input("Give me another number: "))
        return m,n

Here is how to use the function getInfo:

    x,y = getInfo()
    print("the first number is",x)
    print("the second number is",y)

What is a good value for step?

Note that the value of the last formal parameter in Dr. Lusth's distance function is not given by the user. You will need to determine a good value for this parameter via experimentation.

Here are some examples of different step values in calculations:

Two steps Four steps Nine Steps

Compliance Instructions

To make sure that you have implemented your program correctly, create a file named test0 that contains four numbers.

You should then be able to run the following command:

    cat test0 | python3 stopping.py

Using the data in test0, the output of the program should be 131.5 feet to the stop sign, 183 feet for the needed stopping distance, and 214.5 feet for the weaving distance. Don't worry if your distances have more than two decimal places. Adjust the numbers in test0 until you get the specified results.

This method of running the program is called "piping in the input from a file." When you actually do this, the prompts your program makes for information will all be strung together on a single line. Don't worry about it; it's a natural consequence of the way the program was run.

Your program should print out three clearly marked distances. Confirm the correctness of your program by comparing your results (not code!) with others via the mailing list. If your code fails with a runtime error while running this test, then you will receive a zero for this assignment.

Note that your answers do not have to be correct for your program to be graded, only that it not crash. Of course, correct answers will yield a much higher grade.

Note also, that a prerequisite for receiving full credit is that your functions computing results must return numbers, not strings.


Try these refinements to your program, if you are inspired!

Report whether or not Dr. Lusth can stop in time, given the input data.

See if you can print numbers like 268.750506042 as 268.75 (two digits after the decimal point).

Submission Instructions

Change to the project0 directory containing your assignment. Do an ls command. You should see something like this:

    stopping.py   support.py   test0

Extra files are OK. Submit your program like this:

    submit cs100 xxxxx project0

Remember to replace xxxxx with your instructor name. Note that project0 ends in a zero, not a capital O.