CS21 Lab 1: First Programs

Due Saturday, September 14, before midnight

Programming Tips

As you write your first programs, start using good programming practices now:

  • Use a comment at the top of the file to describe the purpose of the program.

  • Use variable names that describe the contents of the variables.

  • Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.

  • Don’t assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them.

Are your files in the correct place?

Make sure all programs are saved to your cs21/labs/01 directory! Files outside that directory will not be graded.

$ update21
$ cd ~/cs21/labs/01
$ pwd
/home/username/cs21/labs/01
$ ls
Questions-01.txt
(should see your program files here)

Goals

The goals for this lab assignment are:

  • write your first python programs!

  • get comfortable with input() and output (print())

  • get comfortable with python data types: int, float, str

1. burpees!

It takes me about 4.2 seconds to do a single burpee. Approximately how long (neglecting fatigue) will it take me to do 365 burpees?

Write a program called burpees.py to calculate the time, based on the user’s input.

Two examples of the running program are shown below. User input is shown in bold.

$ python3 burpees.py
seconds/burpee: 4.2
To do 365 burpees, that would take you approximately:
  - 1533.0 seconds, or
  - 25.55 minutes

$ python3 burpees.py
seconds/burpee: 6
To do 365 burpees, that would take you approximately:
  - 2190 seconds, or
  - 36.5 minutes

In the first example run, the user enters 4.2, and in the second example, the user enters 6.

2. python math operators

To get comfortable with some python3 math operators, write a program called mathops.py that asks the user for two integer operands, and shows the results of applying / (division), // (integer division), ** (exponentiation), and % (mod) to the operands.

Here are two examples of the running program:

$ python3 mathops.py
Please enter two integers.

Integer #1: 9
Integer #2: 5

9 / 5 = 1.8
9 // 5 = 1
9 ** 5 = 59049
9 % 5 = 4
$ python3 mathops.py
Please enter two integers.

Integer #1: 5
Integer #2: 9

5 / 9 = 0.5555555555555556
5 // 9 = 0
5 ** 9 = 1953125
5 % 9 = 5

Note: you can use print() with no arguments to print a blank line.

Also note: if you haven’t talked about it in class yet, the mod operator just gives you the remainder, after applying integer division. So in the first example above, 9 divided by 5 is 1 (9 // 5 = 1), with a remainder of 4 (9 % 5 = 4).

3. banner generator

Write a program called banner.py that asks for a title, a character, and a length, and then generates (prints to the screen) a simple banner: the title between two lines of the given character. The length should be used to print that many of the given character.

Here are two examples of the running program:

$ python3 banner.py
 title: CPSC 021
  char: =
length: 20

====================
CPSC 021
====================

$ python3 banner.py
 title: Welcome to CS21!
  char: $
length: 30

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Welcome to CS21!
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

4. permutations

It’s often useful to find all possible permutations of a string or just a few characters. Write a program called perms.py that asks the user for three characters, and then shows the 6 possible permutations of those three characters.

Here are two examples of the running program:

$ python3 perms.py
Please enter 3 characters:
#1: A
#2: B
#3: C
Here are your permutations:
A B C
A C B
B A C
B C A
C A B
C B A

$ python3 perms.py
Please enter 3 characters:
#1: 0
#2: 1
#3: 2
Here are your permutations:
0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0

Note: later in the course you will learn better ways to do something like this (e.g., using loops or recursion). For now, we only know enough to write the brute force algorithm for this (i.e., write print statements for each possible permutation — you do not need for loops for this one).

Tip: to make this program easier to write, learn your editor’s copy line (Cntl-Insert) and paste line (Shift-Insert) keyboard shortcuts!

5. Answer the Questionnaire

Each lab will have a short questionnaire at the end. Please edit the Questions-01.txt file in your cs21/labs/01 directory and answer the questions in that file.

Once you’re done with that, run handin21 again.

Turning in your labs…​.

Remember to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.

Logging out

When you are all done working in the lab, you should log out of the computer you are using. First quit any applications you are running, like the browser and the Terminal. Then click on the logout icon logout icon and choose "log out".

If you plan to leave the lab for just a few minutes, and then come right back to work, you do not need to log out. It is, however, a good idea to lock your machine while you are gone. You can lock your screen by clicking on the lock xlock icon.