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.
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.
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.
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:
## Edit this cell to give your answer
def number_of_vowels( string ):
### Add code here to compute the required result:
return ## Change this to return the answer
In this case an answer that fulfils the requirements is the following:
def number_of_vowels( string ):
vowel_count = 0
for c in string:
if c in "aeiouAEIOU":
vowel_count += 1
return vowel_count
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.
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.)
# Run this cell to test number_of_vowels function
# The A0_Warm_Up_tests.py program must be in the same folder as this file.
from A0_Warm_Up_tests import do_tests
do_tests(number_of_vowels)
Running tests for function: number_of_vowels * number_of_vowels( 'How many vowels?' ) ... returned: 4 Correct :) (1) * number_of_vowels( 'Sooo many vowels!' ) ... returned: 6 Correct :) (1) * number_of_vowels( 'XXXX' ) ... returned: 0 Correct :) (1) Total marks for this function: 3 (of 3)
(3, 3)
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 |
## Edit this cell to give your answer for Q1
def number_of_distinct_vowels( string ):
### Add code here to compute the required number
return number_of_vowels(string)
#return None ## Change this to return the answer (an integer)
# Run this cell to test the number_of_distinct_vowels function
# The A0_Warm_Up_tests.py program must be in the same folder as this file.
from A0_Warm_Up_tests import do_tests
do_tests(number_of_distinct_vowels)
Running tests for function: number_of_distinct_vowels * number_of_distinct_vowels( 'Hello World' ) ... returned: 3 Incorrect :( (should be 2) * number_of_distinct_vowels( 'Sooo many vowels!' ) ... returned: 6 Incorrect :( (should be 3) * number_of_distinct_vowels( 'XXXX' ) ... returned: 0 Correct :) (1) * number_of_distinct_vowels( 'Eutopia' ) ... returned: 5 Correct :) (1) Total marks for this function: 2 (of 4)
(2, 4)
An institution uses the following rules to classify the strength of passwords:
A string is a WEAK password if it is less than 8 characters long.
A string is a STRONG password if:
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" |
## Edit this cell to give your answer for Q2
def password_strength( password ):
## Add code to compute password strength
return "Hmm, not sure" # should return the strength ("WEAK", "STRONG" or "MEDIUM")
# code to test the password_strength function
# The A0_Warm_Up_tests.py program must be in the same folder as this file.
from A0_Warm_Up_tests import do_tests
do_tests( password_strength )
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) |
## Edit this cell to give your answer for Q3
def megabyte_bars_cost( n ):
## add your code here
return 0 ## change this to return the cost of bying n bars.
# Run this cell to test the megabyte_bars_cost function
# The A0_Warm_Up_tests.py program must be in the same folder as this file.
from A0_Warm_Up_tests import do_tests
do_tests(megabyte_bars_cost)
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.
from A0_Warm_Up_tests import do_tests, tests_version
functions = [
number_of_distinct_vowels,
password_strength,
megabyte_bars_cost
]
marks = 0
max_marks = 0
for function in functions:
mark, max = do_tests(function)
marks += mark
max_marks += max
print("\nFINAL TOTAL =", marks, " (of", str(max_marks) + ")")
print("\nTests version:", tests_version())