Reference no: EM133952516
Programming Fundamentals
Learning Objectives
In this Workshop you will:
define functions that return results;
run unit tests to measure the correctness of some functions;
use randomness to control functions, to generate random poems
Task 1
Each week (week 1 to 8) you will be required to submit into Canvas your solution. Each submission will be marked out of 5. Task 1 will be the total of your best 6 submissions.
Theme: Functions
As well as taking input parameters, functions can return output results. In this tutorial, you will practice writing some of these 'fruitful' functions. Get expert-level assignment help in any subject.
To make it more interesting, We have some automatic unit tests that will test your functions to see if they are returning the correct results. So you can just run these tests to see what mark you will get for the workshop.
Function Puzzles
In this task, you are going to write six 'fruitful functions', and some automatic tests will be run to see if your function is calculating the correct output values. The tests are like a series of six small puzzles, and the functions you write are the solutions to those puzzles.
Download the function_tests.py file. Then edit that file and write the missing functions. After you have written or updated each function, run the file which will execute the test method for it - test_all() function to see how many tests you have passed or failed.
You get marks for documenting each of your functions (triple-quoted docstring inside each function).
Can you pass all 30 of the tests???
(Your score for this workshop is the number of tests you pass, divided by 6)
Crazy poetry [OPTIONAL - no marks]
If you finished those functions quickly, you might like to play with writing some simple Python functions that generate crazy poems. Like this:
funny clown leaves sad dog
funny clown shoots mad dog
sad dog eats black gown
funny clown leaves sad dog
Create a new Python program called poems.py Copy the following code into it.
This program generates crazy poems, using simple noun and verb phrases.
def line(n1, v1, n2):
"""Generates one line of crazy poetry."""
print(noun_phrase(n1), verb_phrase(v1), noun_phrase(n2))
def poem(lines):
"""Generates a crazy poem with 'lines' lines."""
for i in range(0, lines):
line(i, i + 1, 7 - i)
poem(4)
Your task is to write the noun_phrase(n) and verb_phrase(v) functions, so that they return an English phrase that will be used to build your poems.
The noun_phrase function should return a noun phrase (a name of an object, or something that describes an object), for example "Mad Dog Moody". Try to make some of your noun phrases rhyme, so that the output will be more like a poem.
The verb_phrase function should return a verb phrase (a doing word/phrase), for example "runs" or "runs far from".
The input number should control which phrase is returned. For example, if you want to have three different verbs in your poem, then each value of v (0, 1, 2) should return a different verb. Try to handle larger input values in a nice way too, so that your program doesn't crash. Hint: use an if statement, or the modulo operator (%) to limit the input number to your number of phrases.
After you've defined those two functions (make sure you add documentation strings!), run your program to see what poem you get?
Crazy poetry part 2 [OPTIONAL - no marks]
Here are some ideas for making more realistic/fun poems:
Change the code in the poem function so that it chooses random numbers to pass as inputs to each line? Or experiment with a mixture of random numbers and loop variable formulae (like 'i'). Hint: to generate a random number between x and y (inclusive), import the random library, then call random.randint(x, y).
Change the line function so that it doesn't choose the same input value for both noun phrases?
Change your noun_phrase function so that it returns some noun phrases more often than others, to get some repetition in your poem?
Go back to the earlier workshops where you wrote pseudo code for sorting M&M. Write a function that will sort and count M&M. Think about the data structure that you will store your original M&Ms (You might have to jump ahead to next week's module on Lists and Dictionaries.
Create a function that return counts the red M&Ms from a given input of M&Ms.
Create a function that return counts a given colour M&M from a given input of M&Ms.
Create a more general function that returns all the counts or all the colours from a given input of M&Ms.