Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

 

MIS41110 

Programming for Analytics


Question 1 – Compulsory – 30 Marks

 

a. Outline how the following statement is evaluated and state what value n is initialised to:

n = (48 + 16) / 2 ** 4 + (4 / 2)

[2 marks]

 

 

b. The following code results in an error message. What will the error message be and how can it be fixed?

 

def newfunction(n):

n = n + 5

return n

[1 mark]

 

 

c. Using the variable name

 

temp

 

write Python code which displays the text “Too cold!” when temp is less than 0 and “Too hot!” if temp is greater than 30, and “Just nice!” otherwise.

 

[2 marks]

 

 

d. Create a python class called BankAccount that has a class member variable called interest_rate with default value set to 0.05, and 3 member variables name, number, and balance.

[5 marks]

 

 

e. Create two instance methods for your BankAccount class called deposit() and withdraw() which increase and decrease the balance of the account. Ensure that the withdraw() method does not allow the account to go into an overdrawn balance

[5 marks]

 

 

f. Add a third method called add_interest() which adds interest to the  balance (the interest should be the interest rate multiplied by the current balance). 

[5 marks]

 

 

g. 

h. What is the output from the following:

 

a = 0

b = 5

 

is1 = (a == 0) or (b <= 5)

is2 = ((a != 1) and (b > a)) or (b <= 3) and (b < a + 3)

is3 = (a >= 0) and ((a*b) > 6) and (b <= 8)

is4 = (a % b == 0) and (b == 5) or (a != b)

is5 = (a != 0) and not (b == 7)

 

print(is1)

print(is2)

print(is3)

print(is4)

print(is5)

[5 marks]

 

 

i. In the following code sample, identify the line(s) that will never execute:

 

1: if x > 5:

2: if x > sqrt(25):

3: print("more than 5")

4: else:

5: print("5 or less")

6: elif x >= 10:

7: print("10 or more")

8: else:

9: print("Otherwise")

 

[5 marks]

 

 


Question 2 – 35 Marks

 

a) Write a program in Python that allows a user to convert degrees Celsius to a selected alternative scale by doing the following tasks:

 

1. The user should be presented with a menu which indicates the options available to him/her [5 marks]

2. The user must be able to convert from degrees Celsius to degrees Fahrenheit and degrees Kelvin [5 marks]

3. Once the user selects which measurement they wish to convert to, they must then be asked for the temperature in Celsius they wish to convert [5 marks]

4. Once the user is presented with the results, they should be asked if they would like to quit the program or return to the main menu. [5 marks]

5. Write a unittest class to test your function to convert from celsius to Fahrenheit [5 marks]

 

Use the following conversion rates:

● Degrees Celsius to degrees Fahrenheit (C * 9/5) + 32

● Degrees Celsius to degrees Kelvin C + 273.15

(20 Marks)

 

b) Write a program in Python that collects the weights of postal packages

(in grams) and classifies them according to that weight

 

The program should accomplish the following tasks:

 

1. The user should initially be asked to enter how many packages they wish to record weights for. The number of packages must be an integer value greater than 0. [5 marks]

2. The user must then be asked to enter a weight for each package. The weight must be greater than 0. [5 marks]

3. Once all the weights are entered, the program should display the category and number of packages per category. [5 marks]

For example, if there were three packages, 150g, 600g and 1600g the display would be:

Up to 100g : 0

Up to 250g : 1

Up to 500g : 0

Up to 1kg : 1

Up to 1.5kg : 0

Up to 2kg : 1

Up to 2.5kg : 0

Over 2.5kg : 0

(15 Marks)

 

 (Total: 35 Marks)

Question 3 – 35 Marks

 

This question tests your ability to load in a dataset as a data frame and create graphs using pandas in Python.

 

a. Access the dataset from the following url and read it into a data-frame.

 

https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv

 

How many variables and how many observations does the dataset contain?

[5 marks]

 

b. Implement the code to calculate and display the average and standard deviation for the miles per gallon variable.  

[5 marks]

 

c. Create a scatterplot graph of the horse power (on the x-axis) against the miles per gallon variable (y-axis).

[5 marks]


d. Create a second scatterplot, this time, for the weight (wt) variable against the miles per gallon (mpg) variable and ensure to display the fact there are three distinct groups in the plot, depending on the number of cylinders (cyl) of the cars.

 

You should:

 

● Select a different plotting character than the default one

● Colour the three groups differently

● Include a legend to explain these groups

● Include sensible x-axis and y-axis labels and a main title

● Rotate the numbers on the y-axis so they appear horizontal

 

Comment on the resulting graph. [15 marks]

 

e. Create a linear regression for the weight variable (x) against the miles per gallon variable (y) and determine the intercept and the x co-efficient.

[5 marks]


 

(Total: 35 Marks)


 

Question 4 – 35 Marks

 

This question tests your understanding of map, reduce, filter, list comprehension, and generators in Python.

 

a. Implement a python function to sum the number of integers from 1 to a given number n using list comprehension.

[4 marks]

 

 

b. Implement a python function to sum the number of integers from 1 to a given number n using numpy

[4 marks]

 

 

c. the following for loop into a while loop (which does the same thing as the for loop):

def print_to_number():

    for i in range(1,10):

        print("i = ", i)

[4 marks]

 

 

d. Implement a function in Python that will receive a list of the first 25 natural numbers and using the filter function will return a filtered list of the numbers that are perfect squares, i.e. 1, 4, 9, 16, 25 etc...

[4 marks]

 

 

e. Implement a generator function in python that will calculate perfect square numbers.

[4 marks]

 

 

f. Write some unittest code to test your new functions which you have written for parts a, b, c, d, & e of this question. Give some consideration to what sort of values you might want to use for your testing.

[9 marks]

 

 

g. Implement the functionality described in the following flowchart:

[6 marks]

 

 

 

 

 

 

 

 (Total: 35 Marks)