The following exercises are intended to get you started with Python. At the very least, you must complete the prerequisites before the next lecture. If youve programmed in python before, you can probably skip the first few exercises. If youve not programmed in python before, get as far as you can, but we dont expect you to complete everything.
This assignment is not assessed, but you do need to hand it in via Moodle by Monday 7th October at 12 noon so we can see how youve got on. Feel free to collaborate with your friends, but list who you have collaborated with in your submission.
Prerequisite 1
Install python 3.7 on the machine you intend to use to do your coursework. If you have problems, come and see the ENGF0002 programming clinic.
Prerequisite 2
Install git on your machine. This will allow you to check out software and slides from github. Familiarise yourself with the basics of checking out and updating from a repository.
Prerequisite 3
Ensure you can run tkinter packages using your python 3 installation, as well need these for next weeks assignments. Tkinter usually comes pre-installed with python installations, but in some cases youll need to install a separate package.
Test your install by checking out
mhandley/ENGF0002-2019/Assignments/assignment1/tkintertest.py from github and checking it runs properly.
From the command line, if python is installed as python3, you can run this as:
python3 tkintertest.py
Exercise 1: GCD example from class
Type in the GCD example from class (its also in gcd-function.png in the git respository), and get it to run.
Why type it in, rather than cut and paste, or checkout from github? Simply, we want you to get set up with an appropriate editor, and understand the very basics of python. If you type it in, youll remember it. Likely youll mistype something, and have to understand what pythons error messages are telling you. And at the least, youll need to get to grips with pythons use of whitespace to indicate semantic grouping of statements. Doing this with code where you know what the answer should be is useful if youve not programmed in python before.
Exercise 2: Odd or Even
Write a program that asks the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user.
Hint: The python3 input() function will read input from the keyboard, but in python3 the result will be a text string, not an integer. You may need to use the int() function to convert it to an integer type.
Exercise 3: Fizzbuzz
Write a program that prints the numbers from 1 to 100. But for multiples of three print Fizz instead of the number and for the multiples of five print Buzz. For numbers which are multiples of both three and five print FizzBuzz.
This is one of those classic interview questions that are supposed to distinguish candidates that can program from those than cannot. There are lots of versions of FizzBuzz online, but wed like you to attempt your own before you go hunting for online solutions.
Hint: modular arithmetic might be useful.
Hint: you can do this with a while loop, as in the GCD example, but its more elegant to use a for loop and range operator.
Exercise 4: Calculating Pi
We want to compute an approximation of (3.141592) up to a certain precision. One way to do this is by using a Monte Carlo method as follows. Consider the square of edge length 1 and the circle of radius 1/2 inscribed in the square, as depicted below. Generate random points within the square. Suppose Nhits points hit the circle, out of a total of Ntot points.
| A B C D E F G H I J K L M N O P Q R S T U V W X Y Z | (original alphabet) |
| N O P Q R S T U V W X Y Z A B C D E F G H I J K L M | (ciphertext alphabet) |
Then we can approximate by noting that Nhits estimates the area of the circle and Ntot estimates the area of the square. In fact, recalling that the area of the circle is r2, for r=1/2 we have
Acircle Nhits
Asquare 4 Ntot =
Implement a function estimatepi(precision) that returns an approximation of up to precision decimal positions. Use the function random() from the module random to generate random numbers between 0 and 1. (Hint: the more points you generate, the more precise your approximation will be.)
Exercise 5: Breaking Caesars Cipher
A simple substitution cipher is an encryption method where each letter of the alphabet is replaced by another one, according to a ciphertext alphabet. The Caesars Cipher is a very simply substitution cipher, where each letter is replaced by one n places later in the alphabet, wrapping back from Z to A if necessary. For instance, in the cipher
A is replaced by N, B by O and so on. Applying this cipher to the word HELLO produces the word URYYB.
Implement a function breakcipher(text) that takes a string which is assumed to be encrypted using a Caesar cipher finds the ciphertext alphabet used to encrypt it and returns the decrypted string. Assume that the decrypted string will be in English, with letters A-Z and no spaces.
As there are only 26 possible Caesars Ciphers, you can easily break them by hand. But how would a program automatically decide which of the 26 possible offsets is the correct one?

![[Solved] ENGF0002 Assignment1](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip.jpg)

![[Solved] ENGF0002 Assignment2-Debugging the Bomber Game](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip-1200x1200.jpg)
Reviews
There are no reviews yet.