P4DS: Assignment 0 (formative) (Autumn 2020)

Warm Up

Last modified: 7th Oct, 2020

Overview

This formative coursework has the following objectives:

Your work will consist of defining a number of functions according to given specifications.

You will be given a bare skeleton function just giving the function name, arguments and a dummy return value. To answer the questions you need to fill in code within thes template functions in order to create a working function that fulfils the given requirements.

Automatic Testing

You will be able to check that your functions are working correctly, by running a testing function defined in the Python file A0_Warm_Up_tests.py. You should download this and put it in the same directory as this file (A0_Warm_Up.ipynb).

An example question is given below, with an explanation of how to answer it and test your answer. After that are 3 questions you need to answer yourself.

Grading and Submission

This is formative coursework which means that the mark you obtain does not count towards your module grade. Hence there is no submission deadline. However, an autograding submission system will be made availble via Gradescope. You are strongly advised to try this as it will enable you (and me) to test the submission and autograding system, to ensure it works smoothly for later assessments (which will count towards your grade). You will receive a notification when the Gradescope submission system is ready for use.

At the end of this file a Grading section, which provides a function for carrying out grading of all parts of the assignment and calculating a final grade. This should indicate the grade you are likely to obtain by submitting the file to Gradescope, which will carry out similar testing. But note that the gradescope automarker will carry out different specific tests than those provided in A0_Warm_Up_tests.py for your own testing. It will check your functions using different input values.

Eg1: Count the number of vowels in a string

Define a function number_of_vowels, which takes one string argument and returns the number of vowels that occur in that string. More specifically, the number returned should be the total number of occurences of all vowels in either lower or upper case. The following table shows the required outputs for various inputs:

INPUT OUPUT
"yyyykz" 0
"A cat sat on a mat" 6
"Abracadabra" 5
"Eutopia" 5

Below is a template for the function, that you need to modify to implement the specified function:

In this case an answer that fulfils the requirements is the following:

Note: When answering the questions below, you can just modify the given template cell. You don't need to create a new cell for your definition. But make sure you do not alter the name of the function or the number and type of its arguments, otherwise the automatic testing/grading function will not work correctly.

Automatic testing:

To run tests on your number_of_vowels function, run the following code cell.
(Make sure you first run the cell above that defines number_of_vowels.)

Q1: Count the number of different vowels in a string

To answer this question you need to write code that will determine the number of different vowels in a string input by the user. By "different vowels" we mean that if the same vowel occurs more than once it is only counted once. Moreover, small and captial versions of the same letter do NOT count as different vowels. Thus, the number given as the answer will always be from 0 to 5 (inclusive).

Examples:

INPUT OUPUT comment
"yyyykz" 0 no vowels
"hello you" 3 three different vowels
"abracadabra" 1 the only vowel is "a"
"Ooops!" 1 only vowel is "o" (both small and capital)
"A cat sat on a mat" 2 contains "a" and "o"
"Eutopia" 5 contains all five volwels

Q2: Password Strength (6 marks)

An institution uses the following rules to classify the strength of passwords:

You need to code a function password_strength that will take a string argument and will print the 'strength' of that string as a password, according to the rules given above. So it should output one of the strings "STRONG", "WEAK" or "MEDIUM".

Examples:

INPUT OUPUT
"hello" "WEAK"
"7Kings8all9Pies" "STRONG"
"brandon123" "MEDIUM"

Q3: Megabyte Energy Bars (5 marks)

The SnackShack convenience store sells Megabyte energy bars according to the following pricing rules:

To answer this question, write a Python function megabyte_bars_cost that takes one input, which is the number of Megabyte bars a customer wants to buy, and gives 1 output, a float, which is the total cost in pounds of buying that number of Megabyte bars. (You should assume that the buyer alway minimises the cost buy buying as many Sixpacks as possible.)

Examples

Num Bars (input) Cost in £s (output) Cost Breakdown
3 3.75 3 single bars
12 10 2 six packs
15 13.75 2 sixpacks and 3 single bars
26 20.25 4 sixpacks, 2 singles (22.50), 10% discount (-2.25)

Grading

The following code will run tests for all functions you have defined for this assessmend and will calculate your total mark.

To get your mark simply select the "Run All" option from the Jupyter "Cell" menu above. Test results followed by your overal grade will be shown below.
You can do this at any point to see the marks you would get for what you have done so far.