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

Computing 3 Exercises

The document provides coding exercises for an ENG 1P13 course at McMaster University, emphasizing the importance of regular coding practice. It includes various tasks such as creating functions for drawing shapes, rolling dice, and implementing encryption methods. Additionally, it features code tracing exercises to enhance understanding of variable scope and function execution.

Uploaded by

oreozhang318
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Computing 3 Exercises

The document provides coding exercises for an ENG 1P13 course at McMaster University, emphasizing the importance of regular coding practice. It includes various tasks such as creating functions for drawing shapes, rolling dice, and implementing encryption methods. Additionally, it features code tracing exercises to enhance understanding of variable scope and function execution.

Uploaded by

oreozhang318
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ENG 1P13: Computing 3 Exercises

© Sam Scott, McMaster University, Spring 2024

About these Exercises


When you are learning to program there is absolutely no substitute for writing code.
Every time you do a coding exercise or activity, you are studying for the upcoming
test and improving your coding and debugging skills.
You should set aside coding time every day, even if there is no assignment
due, and try some coding exercises either here or in the textbook. You will
not regret it.
Some of the exercises below will be assigned as practice during lab or lecture time.
The others are there so that you have something to do during your daily coding
sessions J. Solutions to these exercises will be in the current week’s folder on
Avenue.

Output (“Chatterbox”) Functions


1. Write a print_signature function that returns nothing but outputs the following
to the shell, nicely formatted:
 your name,
 your student number and program,
 ENG 1P13: Integrated Cornerstone Design Projects in Engineering,
 Your professor’s name, and
 the current term (e.g. Spring 2024)
Save this function somewhere. You’ll be using it in your assignments from now
on.

2. Try exercise 2 from Chapter 6: Define a draw_square function that accepts


parameters for the center location and size of a square, then write code to call it
repeatedly to produce the pattern shown below.
3. Go to exercise 9 from Chapter 6 and grab the drawFivePointStar function from
the solution code you find there.
 Modify the function so that you can send in the side length of the star it
draws as a parameter.
 Write a function drawStarField(t, num) that uses turtle t to draw num
stars by calling drawFivePointStar. Each star should be in a randomly
determined x and y location, should be a random size, and should be drawn
in a randomly determined shade of gray (use random.random to get a
value from 0 to 1 then use that value as the red, green, and blue component
of the color to get a grayscale value).
 The output below shows the result of a 900 x 450 starfield containing 1000
stars.
 Tip: Bracket your drawing code with screen.tracer(0) and
screen.update() or be prepared to wait for a long time!
 Tip: Test your change to drawFivePointStar first. Then test drawStarField
with one star, then maybe 5, then when it seems to be working, try 1000.
 Improvements: Change drawStarField so that it also accepts parameters for
the left, right, top, and bottom coordinates of the starfield. Why is this an
improvement?
Black Box Functions
4. Write a function roll_one_die(sides) that returns a random integer from 1 to
sides.
5. Write a function roll_dice_set(n, sides) that returns the result of rolling n dice
with the given number of sides. Call roll_one_die repeatedly to make this
happen (using the accumulator pattern). Give the parameters default values
so that the default roll is two six-sided dice.
6. Write a function average(num_list) that accepts a list of numbers as a
parameter and returns the average of the numbers in the list (using the
accumulator pattern).
7. Write a function called reverse_int(n) that accepts a 4-digit integer n and uses
//, %, and * to return the result of reversing the digits. Assume the number
always has 4 digits. For example, reverse_int(2351) returns the integer 1532,
and reverse_int(1) returns the integer 1000, and
reverse_int( reverse_int(1234) ) should return 1234
8. Write a function that approximates the sum of the geometric series
1 1 1 1
+ + + + … using the first m terms (m is a parameter to the function).
2 4 8 16
https://en.wikipedia.org/wiki/Geometric_series
9. Write a function that returns nothing but prints a table of approximations of the
geometric series using 1 to p terms, where p is a parameter. Call the function
from the previous question to accomplish this.
p sum
----------------------
1 0.5
2 0.75
3 0.875
4 0.9375
5 0.96875
6 0.984375
7 0.9921875
8 0.99609375
9 0.998046875
10 0.9990234375
Main Functions and Importing
10.Put your answers to exercises 4 and 5 into a file called dice.py. Then write a
main function in a separate file that imports roll_dice_set by name. In the
main function, you should allow the user to define and roll a set of dice a
number of times and then report the results (the user chooses the number of
dice, number of sides, and number of times to roll that dice set).
11.To encrypt an integer is to apply a reversable mathematical function to it so
that the original number will be unknown to anyone who does not know how it
was encrypted. To decrypt an integer is to compute the original integer from its
encrypted version. For example, you might encrypt by simply adding 10 to the
number, then decrypt by subtracting 10 to get back to the original number. This
simple type of encryption is known as a “shift cipher”.
Write a program that can be used to encrypt and then decrypt an input integer
using either of two different methods (you define the encryption methods).
For each method of encryption, create a new file and write functions named
encrypt and decrypt. The encrypt function accepts an unencrypted integer
parameter and returns the encrypted version. The decrypt function does the
opposite.
Then, make another file with a main function that asks the user for the type of
encryption they want to use and the integer they would like to encrypt. Import
the appropriate encrypt and decrypt functions (you can use an if statement for
this), then show the encrypted and unencrypted versions of the integers.
TIP: Make sure all of your files are located in the same folder!
Code Tracing and Variable Scope
12. What will the program below print?

def foo2 (a, c, d):


if a+c > d:
return a+c+d
else
return d

def foo (a, c):


d = 0
while a < c:
d = a * c
a = a + 1
return foo2(d,a,c)

a = 1
b = 2
print(foo(a, b))
print(a)

#updated exercise (removed selection, and swapped while for a for


loop)

def foo2 (a, c, d):


return a+c-d

def foo (a, c):


d = 0
for _ in range(0,5):
d = a * c
a = a + 1
return foo2(d,a,c)

a = 1
b = 2
print(foo(a, b))
print(a)
13.Consider the following code with line numbers shown:

1 def mangle(a, b):


2 if a <= b:
3 return b
4 return a+b

5 a = 5.5
6 b = 12
7 a = mangle(b, a)
8 b = mangle(a, b)
9 a = mangle(mangle(a,25), mangle(b,25))

#updated exercise (removed selection)


1 def mangle(a, b):
2 return a-b

3 a = 13
4 b = 72
5 a = mangle(b, a)
6 b = mangle(a, b)
7 a = mangle(mangle(a,32), mangle(b,32))

Trace the execution of this code and show the values of the variables after each of
lines 5, 6, 7, 8, and 9 have executed:

Line a b
5
6
7
8
9
Updated table to reflect updated code

Line a b
3
4
5
6
7

You might also like