CSCI 150: Lab 2

The Basics
Due: 10PM on Tuesday, September 17th

In this lab you will create programs which put to use some of the week's topics. You'll aso be introduced to some useful new constructs. By the end of the lab, you will have:

  • Become comfortable using variables and mathematical expressions
  • Used for-loops to solve a variety of programming problems
  • Generated simple ASCII graphics with nested loops.
As with last week's lab you should create a lab02 directory in which to store all your work for this assignment:
  cd                 # takes you to your home directory 
  cd cs150           # enters your 'cs150' folder
  mkdir lab02        # creates lab02 folder cwd
  cd lab02           # goes into that folder     
  pwd                # confirms you are in the appropriate directory
                

For future labs, the above steps won't be stated explicitly, but in general you should create the appropriate folder at the start of each new lab.

Part 1 - Loopin'

beer.py: 4 points
sequence.py: 4 points
fibonacci.py: 4 points
interest.py: 4 points

In this portion of the lab you'll be building a bunch of short programs that utilize for-loops.


Beer!

You might be familiar with the countdown song 'Ninety-Nine Bottles of Beer on the Wall', whose first two verses are

99 bottles of beer on the wall
99 bottles of beer!
Take one down, pass it around
98 bottles of beer on the wall!

98 bottles of beer on the wall
98 bottles of beer!
Take one down, pass it around
97 bottles of beer on the wall!

and eventually ends with the line

0 bottles of beer on the wall!

Define the Problem:

Write a program called beer.py that prints out all 99 verses of the song.

Input: None.
Output: All verses of the beer song to the terminal.

Understand the Problem:

You should think about the first few verses of the song. The first verse is given above; what changes between the first and the second verse? Between the second and the third? What does the last verse look like?

Design the Algorithm:

Now it is time to think precisely about how you will solve this problem. Your solution should not copy and paste hundreds of lines of print statements. Instead, create a much shorter program by taking advantage of the power of the for-loop. Again, don't worry about grammar.

Implement a Design:

Once you think you have an algorithm written down in pseudocode, start writing your actual program in Python.

Use a constant NUM_BOTTLES to represent the number of bottles and use this in the range of your for-loop.

A constant is just a variable whose value never changes throughout the course of the program. Although this is not enforced in Python (it won't stop you from changing a contant's value), other languages do, and it is a useful practice regardless. By convention, we use ALL CAPS for the names of constants, so that you can recognize them easliy. In general, using constants makes your programs easier to maintain, and helps to prevent bugs by reminding you not to change values you don't intend to change. As an added benefit, they typically make your code more readable.

Test the Program:

Once your program works with 99 bottles, try changing your constant to other values (for example: 10 bottles, 0 bottles, 1 bottle, etc.) and make sure that it works.

Handin:

It is a good habit to handin your files every time you finish a program, to make sure that we have up-to-date copies (in case something happens to your files, or you forget to handin at the end). So take a minute now to run the handin command (instructions are at the bottom of every lab), then continue with the next portion of the lab. You can hand in as many times as you want; each submission is distinct and time-stamped, and the graders will grade the latest one available.

Sequence

Define the Problem:

Write a program called sequence.py that lists the squares of all integers from some start number down to one.

Input: A number start from the user.
Output: A list of squares starting from start2 down to 12.

Understand the Problem:

For example, if start is 6 then the output of the program would be

   Squares from 36 down to 1:
   36, 25, 16, 9, 4, 1
                          
Be sure to use the formatting given above, with a comma and a space after each number except for the final 1. (If you need a refresher in how to format your print statements nicely, take a look back at lab 01.)

Design an Algorithm:

Think about the main steps of the program. Presumably step 1 is to get the input from the user. Then you have to figure out how to print the squares in the right order to the console, with the correct formatting. Write out the pseudocode before you touch the keyboard!

Implement a Design:

Now that you have a promising algorithm, it is time to put it into Python code. Go ahead and write up that program.

Test the Program:

If your syntax is correct, it is time to run your program with a variety of inputs. Try n=6 (because you know what the answer should look like), but also try other values of n, such as n=0, 1, 10, etc.

Handin:

It is a good habit to handin your files every time you finish a program, to make sure that we have up-to-date copies (in case something happens to your files, or you forget to handin at the end). So take a minute now to run the handin command (instructions are at the bottom of every lab), then continue with the next portion of the lab. You can hand in as many times as you want; each submission is distinct and time-stamped, and the graders will grade the latest one available.

Fibonacci

The Fibonacci numbers are a sequence of integers defined as so: The first and second numbers are both 1, and each following number is the sum of the two previous numbers.

Describe the Problem:

Write a program called fibonacci.py that computes the nth Fibonacci number.

Input: A positive integer n > 2 from the user.
Output: The n-th Fibonacci number.

Understand the Problem:

For example, the first six Fibonacci numbers are 1, 1, 2, 3, 5, 8.

A run of the program might appear as follows:

 My incredible Fibonacci number generator!
 
 Please enter an integer: 8
 The 8th number in the Fibonacci sequence is 21.
                        

Design an Algorithm:

Write down pseudocode describing how you would solve this problem. Try to use enough detail so that it ressembles code, but not so much jargon that it is Python-specific and unintelligible to non-coders.

A few suggestions:
  • If you're stuck on your algorithm, try to "explain" to the computer what you were doing yourself, by hand.
  • Use two variables, say current and previous. Initialize current=1 and previous=1.
  • Now run a for-loop; in each iteration you should simultaneously update current and previous. Update current to be current+previous. What should we update previous to? And why is it important that we do these updates at the same time?

Implement a Design:

Translate your pseudocode into a Python program named fibonacci.py.

Although your final program should only print the requested Fibonacci number, you may want to temporarily include print statements within your loop so you can make sure the program is doing what you intended.

Test the Program:

Try running your program with a variety of inputs. Try n=8 (because you know the answer). Also try n=0,1,2, etc. and check that what it returns makes sense.

Handin:

Just another reminder to run the handin command to hand in your files.

Interesting

In this part of the lab, we will extend our simple.py program from Lab 01 to make it more useful for finanical planning. Assuming you make reguar, monthly deposits as well, you'd like to know how much you'll have in your savings account after a given number of months, assuming your monthly contribution occurs after interest is accrued.

Describe the Problem:

Write a program called interest.py with the following functionality.

Input: Get an initial deposit, a monthly interest rate, a monthly deposit, and a number of months n from the user.
Output: The total money in your possession at the end of n months.

Understand the Problem:

For example, suppose your initial savings is $100, the monthly interest rate is 1%, and you plan on contributing $20 a month. Then during the first month, you'd get $1 of interest (100 * 0.01) followed by another $20 from your regular contribution. So you'd end the first month with $121. On the second month, your interest would be $1.21 (i.e., 121 * 0.01), which, together with you fixed $20, leaves you with $142.21.

After the third month, you'd have $163.63. Technically, the amount you'd have on month 3 would be $163.6321, but most accounts don't keep track of fractions of pennies (as fans of the movie Office Space may know), so you'll want to cut off anything below 2 decimal places.

A run of the program might appear as follows.

   Welcome to the Interest Calculator!
   
   Enter your initial savings: 100
   Enter the monthly interest rate: 0.01
   Enter your monthly contribution: 20
   How many months would you like computed: 3
   
   Initially you put in $100
   After month 1 you would have $121.0
   After month 2 you would have $142.2
   After month 3 you would have $163.62
                          

Design an Algorithm:

Write pseudocode that solves this problem. As with the previous problem, give enough detail to be using loops and variables, use Python-like indentation, but try to avoid Python-specific syntax.

Implement a Design:

Translate your pseudocode into a Python program named interest.py.

As pointed out above, you'll need to truncate your savings to 2 decimal places. One clever way to do this: multiply your value by 100 (now your number represents cents), convert it to an int (now you've tossed out any fraction of a cent that might have been there), and then divide by 100 (to get back to dollars again).

Test the Program:

Run your program on a variety of inputs. You should certainly try the example given above, up to 5 months.

Note: If your answers are off by a cent here or there, don't worry, it is most likely just a rounding error.

Handin:

Just another reminder to run the handin command to hand in your files.

Part 2 - Patterns, Patterns Everywhere

patternA.py: 4 points
patternB.py: 4 points
patternC.py: 4 points
patternD.py: 4 points
patternE.py: 4 points
patternN.py: 4 points

In the next batch of problems, we're going to be looking at sequences of patterns. For each problem, a few patterns are given, each of which made out of ASCII characters and is associated with an index (a number). Your task is to first determine the rules that determine how a particular pattern in the sequence relates to the corresponding number. Then you'll write a program that generates an arbitrary pattern from the given sequence.

Describe the Problem:

For each sequence of patterns X, write a program called patternX.py that generates a specified pattern in sequence X.

Input: A positive integer n that represents an index in the sequence.
Output: The n-th pattern in sequence X.

Understand the Problem:

For example, below are some indices (the numbers on the left) and their associated figures for Pattern A.

Pattern A

For your first program (patternA.py), consider the pattern shown below

  1.   1 2 3
      1 2 3
      1 2 3
                              
  2.   1 2 3 4
      1 2 3 4
      1 2 3 4
      1 2 3 4
                              
  3.   1 2 3 4 5
      1 2 3 4 5
      1 2 3 4 5
      1 2 3 4 5
      1 2 3 4 5
                              

Design an Algorithm:

You can probably see what's happening; figure n consists of n rows, each of which includes all numbers from 1 to n. Therefore your algorithm will need to first get an integer n from the user. Then you'll loop over rows from 1 to n. And for each row, you'll use a nested loop to print the digits from 1 to n, separated by spaces. In particular, the psuedocode may be:

  • Get the index n from the user.
  • For each i from 1 to n:
    • Print out line i of figure n, which consists of all numbers from 1 to n separated by spaces


Similarly, your algorithm for patterns B, C, D, E and N should generate arbitrary figures from the patterns below.

Implement a Design:

Translate your pseudocode into Python programs named patternX.py (where X ranges over A, B, C, D, E, and N.)

Pattern B

  1.   1 1 1
      2 2 2
      3 3 3
                              
  2.   1 1 1 1
      2 2 2 2
      3 3 3 3
      4 4 4 4
                              
  3.   1 1 1 1 1
      2 2 2 2 2
      3 3 3 3 3
      4 4 4 4 4
      5 5 5 5 5
                              

Pattern C

  1.   1 2 3
      2 3
      3 
                              
  2.   1 2 3 4
      2 3 4
      3 4
      4
                              
  3.   1 2 3 4 5
      2 3 4 5
      3 4 5 
      4 5
      5
                              

Pattern D

  1.   1 
      1 2 2 
      1 2 2 3 3 3
                              
  2.   1 
      1 2 2 
      1 2 2 3 3 3
      1 2 2 3 3 3 4 4 4 4
                              
  3.   1 
      1 2 2 
      1 2 2 3 3 3
      1 2 2 3 3 3 4 4 4 4
      1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
                              

Pattern E

  1.   ***
      *
      **
      *
      ***
                              
  2.   ****
      *
      *
      ***
      *
      *
      ****
                              
  3.   *****
      *
      *
      *
      ****
      *
      *
      *
      *****
                              

Pattern N

  1.   *  *
      ** *
      * **
      *  *
                              
  2.   *   *
      **  *
      * * *
      *  **
      *   *
                              
  3.   *    *
      **   *
      * *  *
      *  * *
      *   **
      *    *
                              

Test the Programs:

Don't forget to test your programs on a variety of inputs!

Part 3 - Wrap Up

As with every lab, your last job prior to submission is to complete a brief write-up by filling out a Google Form.

Handin

You now just need to electronically handin all your files. As a reminder

 
    cd             # changes to your home directory
    cd cs150       # goes to your cs150 folder
    handin         # starts the handin program
                    # class is 150
                    # assignment is 2
                    # file/directory is lab02
    lshand         # should show that you've handed in something
                

You can also specify the options to handin from the command line

 
   cd ~/cs150     # goes to your cs150 folder
   handin -c 150 -a 2 lab02
                

File Checklist


You should have submitted the following files:
   beer.py
   sequence.py
   fibonacci.py
   interest.py
   patternA.py
   patternB.py
   patternC.py
   patternD.py
   patternE.py
   patternN.py
                


Last Modified: September 2019 - A. Eck, T. Wexler, B. Geitz, C. Taylor, A. Sharp, S. Semsioglu.