CSCI 150: Lab 2

For Loops
Due: 11:59 PM on Tuesday, February 23

In this lab you will create programs that use some of our recent ideas, especially for loops. By the end of the lab, you will have:

  • Become more comfortable using variables and mathematical expressions
  • Used for-loops to solve a variety of programming problems
  • Generated simple ASCII graphics using patterns with nested loops

In case it is helpful, we have put together a set of completely optional prelab questions (with an answer key) that might help you work through some of the ideas related to this lab before you start doing any programming. You can find these questions here in Prelab 2.

Part 1 - Loopin'

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

In this portion of the lab you'll be building several short programs that each utilize a single for loop.


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.

Test the Program:

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

Sequence

Define the Problem:

Write a program called sequence.py that lists the squares of all integers from some starting 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. You might find it helpful to include your pseudocode as comments in your program (but you certainly do not have to do so).

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.

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, where 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
   After month 2 you would have $142.21
   After month 3 you would have $163.63
                          

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.

As a hint, you will need to create a new variable that keeps track of the current balance in the user's savings account, which will be updated each month (i.e., each iteration of your for loop).

Implement a Design:

Translate your pseudocode into a Python program named interest.py. Please feel free to use your code for simple.py in Lab 1 as a starting point, if you wish.

As pointed out above, you'll need to truncate your output 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.

Part 2 - Patterns, Patterns Everywhere

patternA.py: 2 points
patternB.py: 2 points
patternC.py: 2 points
patternD.py: 4 points (partner encouraged)
patternE.py: 4 points (partner encouraged)
patternN.py: 4 points (partner encouraged)

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 (i.e., letter, numbers, or punctuation) and is associated with an index (a number). Your task is to first determine the rules that describe how a particular pattern in the sequence relates to the corresponding index. Then you'll write a program that generates an arbitrary pattern from the given sequence. As a friendly reminder, the prelab provides hints of how to start thinking about patterns A-C below.

Describe the Problem:

For each pattern A-E and N, write a program called patternA.py, patternB.py, ..., patternN.py that generates a specified pattern.

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

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 maybe already see what's happening in Pattern A: 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 figures from the patterns below, depending on the index n input by the user.

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.   *    *
      **   *
      * *  *
      *  * *
      *   **
      *    *
                              

For Patterns E and N, it might help to break the patterns into multiple parts. For example, in Pattern E, you might think about how to draw the top horizontal line, then the vertical line below it, then the middle horizontal line, then another vertical line, and finally the bottom horizontal line. Please feel free to work with a partner, if you wish, on Patterns D, E, and N. To help you find a partner, the chat during lab can be a good method of finding a partner.

Test the Programs:

Don't forget to test your programs on a variety of inputs! Your programs will be graded using additional inputs (e.g., n = 6 or n = 10)

Part 3 - Wrap Up

Congratulations! You have finished the second lab. As with every lab, your last job prior to submission is to complete a brief write-up by filling out a Google Form, which is also how you submit your Honor Code statement (so please do not forget to do this).

Handin

Finally, all you need to do is submit your solution to the assignment. To do so, please click on the "Submit" button in the top right of your programming environment. This will save all of your programs for myself and the graders to look at. You are welcome and encouraged to submit partial solutions to your assignment -- you can always resubmit by pushing the "Resubmit" button in the top right, which will save the latest version of your programs. We will only grade the latest version that you submitted. By submitting multiple times, you avoid accidentially forgetting to turn in any completed part of your assignment, especially in case you become busy around the lab due date.



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