0% found this document useful (0 votes)
8 views

Assignment #5 (Loop)

This document outlines an assignment for an introductory programming course in Python, focusing on loops and their applications. It includes exercises on understanding loops, debugging code, implementing various loop structures, and optional problems like Zeller's algorithm and a cyclic cipher. Students are required to submit their code and ensure proper documentation within their programs.

Uploaded by

GHAYUR ABBAS
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment #5 (Loop)

This document outlines an assignment for an introductory programming course in Python, focusing on loops and their applications. It includes exercises on understanding loops, debugging code, implementing various loop structures, and optional problems like Zeller's algorithm and a cyclic cipher. Students are required to submit their code and ensure proper documentation within their programs.

Uploaded by

GHAYUR ABBAS
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CS106-Intro.

To Programming (Python)
Assignment No. 5: Loop
Exercise 1 – Understanding loops
For each of the following fragments of code, write what the output would be. Again, do this without running
the code (although feel free to check yourself when you’re done).
1. num = 10
while num > 3:
print num
num = num - 1

2. divisor = 2
for i in range(0, 10, 2):
print (i/divisor)

3. num = 10
while True:
if num < 7:
break
print num
num -= 1

4. count = 0
for letter in ’Snow!’:
print ’Letter #’, count, ’is’, letter
count += 1

Exercise 2 – Buggy loop (aka Find The Bug!)


Consider the following program that Ben Bitdiddle handed in to the course staff (again, try to do this
exercise without running the code in IDLE!):

n = 10
i = 10
while i > 0:
print i
if i % 2 == 0:
i=i/2
else:
i=i+1
What do you think this code is doing? Without comments it is hard to guess what Ben’s intention was
(*cough* this is why the staff loves to look at commented code!!), so read through it and make a sensible
guess as to what it is doing. There’s a lot of mistakes in the code so your guess is as good as ours!

1. Draw a table that shows the value of the variables n and i during the execution of the program. Your
table should contain two columns (one for each variable) and one row for each iteration. For each row
in the table, write down the values of the variables as they would be at the line containing the print
statement.

2. Ben made a lot of mistakes. State what you think Ben was trying to do and suggest one or more ways
he could fix his code (there’s a few good answers for this depending on what you think the code should
be doing).

Exercise 3 – For & While Loops


Create a new file called loops.py and use it for all parts of this exercise. Remember the difference between
input and raw input?
Be sure to test your code for each part before moving on to the next part.

1. Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4,..., 1/10.
2. Write a program using a while loop that asks the user for a number, and prints a countdown from that
number to zero. What should your program do if the user inputs a negative number? As a
programmer, you should always consider “edge conditions” like these when you program! (Another
way to put it- always assume the users of your program will be trying to find a way to break it! If you
don’t include a condition that catches negative numbers, what will your program do?)
3. Write a program using a for loop that calculates exponentials. Your program should ask the user for a
base base and an exponent exp, and calculate baseexp.
4. Write a program using a while loop that asks the user to enter a number that is divisible by 2. Give the
user a witty message if they enter something that is not divisible by 2- and make them enter a new
number. Don’t let them stop until they enter an even number! Print a congratulatory message when
they *finally* get it right.

When you are done, print a copy of the file and turn it in. Make sure your name and section number is in the
comment section of your program.

Exercise 4 – Zeller’s Algorithm


OPTIONAL!- Some problem sets will have optional exercises at the end. Feel free to work on these problems
if you have time at the end of the assignment, but you certainly don’t have to do them. However, you will get
excellent practice in Python, and we will give you feedback on any optional work you turn in.
Zeller’s algorithm computes the day of the week on which a given date will fall (or fell). In this exercise, you
will write a program to run Zeller’s algorithm on a specific date. You will need to create a new file for this
program, zellers.py. The program should use the algorithm outlined below to compute the day of the week on
which the user’s birthday fell in the year you were born and print the result to the screen.
Ask for the month as a number between 1-12 where March is 1 and February is 12. If born in Jan or Feb,
enter previous year (see the notes below). In the end, print out the name of the user and on what they of the
week they were born.
Zeller’s algorithm is defined as follows:
Let A, B, C, D denote integer variables that have the following values:

A = the month of the year, with March having the value 1, April the value 2, . . ., December the value 10, and
January and February being counted as months 11 and 12 of the preceding year (in which case,subtract 1 from
C)
B = the day of the month (1, 2, 3, . . . , 30, 31)
C = the year of the century (e.g. C = 89 for the year 1989)
D = the century (e.g. D = 19 for the year 1989)

Note: if the month is January or February, then the preceding year is used for computation. This is because
there was a period in history when March 1st, not January 1st, was the beginning of the year.
Let W, X, Y, Z, R also denote integer variables. Compute their values in the following order using integer
arithmetic:

W = (13*A - 1) / 5
X =C/4
Y =D/4
Z = W + X + Y + B + C - 2*D
R = the remainder when Z is divided by 7

The value of R is the day of the week, where 0 represents Sunday, 1 is Monday, . . ., 6 is Saturday. If the
computed value of R is a negative number, add 7 to get a non negative number between 0 and 6 (you don’t
need to do this in the code). Print out R. You can check to be sure your code is working by looking at
http://www.timeanddate.com/calendar/.
Run some test cases- try today’s date, your birth date, and whatever else interests you!
Feel free to submit your zellers.py code if you wish, we’ll take a look at it if you do.

Exercise 5 – Secret Messages


OPTIONAL! This exercise is tricky! Be sure to ask the LAs for help if you need them!
The goal of this exercise is to write a cyclic cipher to encrypt messages. This type of cipher was used by
Julius Caesar to communicate with his generals. It is very simple to generate but it can actually be easily
broken and does not provide the security one would hope for.
The key idea behind the Caesar cipher is to replace each letter by a letter some fixed number of positions
down the alphabet. For example, if we want to create a cipher shifting by 3, you will get the following
mapping:

Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC
To be able to generate the cipher above, we need to understand a little bit about how text is represented
inside the computer. Each character has a numerical value and one of the standard encodings is ASCII
(American Standard Code for Information Interchange). It is a mapping between the numerical value and the
character graphic. For example, the ASCII value of ’A’ is 65 and the ASCII value of ’a’ is 97. To convert
between the ASCII code and the character value in Python, you can use the following code:

letter = ’a’

# converts a letter to ascii code ascii_code =


ord(letter)

# converts ascii code to a letter


letter_res = chr(ascii_code) print
ascii_code, letter_res

Start small. Do not try to implement the entire program at once. Break the program into parts as follows:

1. Create a file called cipher.py. Start your program by asking the user for a phrase to encode and the shift
value. Then begin the structure of your program by entering in this loop (we’ll build on it more in a
bit):

encoded_phrase = ’’

for c in phrase: encoded_phrase =


encoded_phrase + c

What does this loop do? Make sure you understand what the code does before moving on!
2. Now modify the program above to replace all the alphabetic characters with ’x’. For example:

Enter sentence to encrypt: Mayday! Mayday!


Enter shift value: 4
The encoded phrase is: Xxxxxx! Xxxxxx!

We are going to apply the cipher only to the alphabetic characters and we will ignore the others.
3. Now modify your code, so that it produces the encoded string using the cyclic cipher with the shift
value entered by the user. Let’s see how one might do a cyclic shift. Let’s say we have the sequence:

012345

If we use a shift value of 4 and just shift all the numbers, the result will be:

456789

We want the values of the numbers to remain between 0 and 5. To do this we will use the modulus
operator. The expression x%y will return a number in the range 0 to y-1 inclusive, e.g. 4%6 = 4, 6%6 =
0, 7%6 =1. Thus the result of the operation will be:
450123

Hint: Note that the ASCII value of ’A’ is 65 and ’a’ is 97, not 0. So you will have to think how to use the
modulus operator to achieve the desired result. Apply the cipher separately to the upper and lower
case letters.
Here is what you program should output:

Enter sentence to encrypt: Mayday! Mayday!


Enter shift value: 4
The encoded phrase is: Qechec! Qechec!

When you are done, print a copy of the file and turn it in. Make sure your name and section number is
in the comment section of your program.

You might also like