MAS212 Class Test 1 (Mock)

(This is a mock test. The real Class Test 1 will look similar and will be held in Week 2.)

Aim:
  • To help you learn enough about Python to enjoy the remainder of this course.
How it works:
  • Complete the notebook below, as follows. First, click on a question, then from the menu select Insert -> Insert Cell Below. From the dropdown box (next to the "stop" symbol) choose whether you wish to insert 'Code' or 'Markdown' (i.e. text). Save regularly!

  • Press Shift-Enter to execute the code in a cell.

  • Submit via http://mas212.group.shef.ac.uk/uploads by 5pm on Mon 29th Oct.

  • Your lecturer will mark each question as either 2 (good), 1 (needs revision), 0 (not attempted/wrong). You will then be given one chance to revise your answers and resubmit.

  • This is an open-book test, which means you may consult books, notes and internet resources. However, copy-and-pasting is strongly discouraged, and would reflect badly on a Level 2 University of Sheffield student. Please give answers in your own words.
Key Resources:
  • Chapter 1 in https://scipy-lectures.github.io/

  1. The Python language

(Please answer the following questions in "raw text" or "markdown" cells)

Q. Python is case-sensitive: True or false?

Q. Python has four numeric types -- what are they?

Q. Describe in your own words the int and float data types.

Q. Python has several container types. Give three examples.

Q. What does mutable mean? Give an example of mutable and unmutable sequences.


  1. Simple Python code

(Please answer the following questions in "code" cells).

Q. Python as a calculator: Calculate \(3 + 4\), \(3 \times 4\), \(3 / 4\), \(3^2 + 4^2\) and \((3 + 4i)^{3}\).

Q. Use for loop to find the sum-of-squares \(1 + 4 + 9 + \ldots + 100\)

Q. Make a list of square numbers \([1, 4, 9, \ldots, 100]\) and find the sum another way

Q. Print the value of \(\pi\) to 10 decimal places. (Hint: first import the "math" package).

Q. Compute the first 20 terms in the Fibonacci sequence, defined by \(f_{k+1} = f_k + f_{k-1}\) with \(f_{0} = f_{1} = 1\).

Q. Print the value of Pi to 20 decimal places. (Hint: import the math package).

Q. Write a function to find all the divisors of a positive integer. Test it by finding all divisors of 2014.

Q. Calculate all perfect numbers less then 10000.


  1. String processing

In [1]:
s = "The horse has a long neck"

Q. How many characters are in this string?

Q. Replace the word "horse" with "giraffe".

Q. Use list slicing to (a) print every third character, (b) print the second half of the string, (c) reverse the string.

Q. How many times does the letter "e" appear in this string?

Q. Which words in the sentence contain the letter "e"?


  1. A slightly harder question

In [2]:
s2 = "Ju jt b usvui vojwfstbmmz bdlopxmfehfe uibu b tjohmf nbo jo qpttfttjpo pg b hppe gpsuvof nvtu cf jo xbou pg b xjgf"

Q. Decipher the string above. From which novel is it taken?

Note that characters may be turned into integers (ASCII codes) using ord() and back again using chr().