The goal of this exercise is to write a \business phrase" generator, which each time it is called produces lines of business speak, such as:
It's time that we became uber-efficient with our interactive policy hardware At base level, this just comes down to holistic relative consulting Only geeks stuck in the 90s still go for functional modular capability We need to get on-message about our 'Outside the box' modular matrix approaches The idea is very similar to the \Eureka Machine" discussed in lectures: to produce a business speak phrase, your program should randomly select and print a phrase from each of four lists in turn.
These lists are stored in files named beginning.txt, adjective.txt, inflate.txt and noun.txt, which can be downloaded from the resources website;1 a business speak phrase is constructed by selecting a phrase from each of these in the order beginning.txt, adjective.txt, inflate.txt and noun.txt.
Write a program that will read the required words and phrases from the _les and print _ve business speak phrases. The program should be organised so that it can easily be changed to print any number of phrases.
You should submit:
1. A brief explanation of how your program is structured and why you have structured it that way.
2. An example of the output of your program, cut-and-pasted from the command prompt or a screenshot.
3. A hardcopy of the program.
4. Your program in a file business.py.
1. You may use the dice.choose function discussed in lectures.
2. As before, you can read all the lines in a file with lines = open ('myfile .txt ', 'r'). readlines () myfile.txt must be in the same directory as your program. Remember that each line will have a newline character at the end.
3. You can remove the newline and any other whitespace from a string using strip: for example:
>>> s = ' string with spaces at the beginning and end \n'
>>> s ' string with spaces at the beginning and end \n'
>>> stripped = s. strip ()
>>> stripped 'string with spaces at the beginning and end '
Shuffling. One way to shuffle a deck of cards or a list is to repeatedly choose a pair of cards/items at random and to swap their positions in the deck/list. Write a function shuffle(L, nswaps) which shuffles the list L in-place using this method, swapping pairs nswaps times. Write a test function that uses assert statements to check that none of the elements of a list of integers are lost and none are gained by the shuffling.
How many swaps are needed to properly shuffle the list? Clearly one or two swaps don't make much difference, but for a small list there's not much point in doing thousands of swaps. To answer this question we can define the quality of a shuffle as the fraction of times the second element of two adjacent elements is larger than the first. Thus the sorted list [0, 1, 2, 3, 4, 5, 6] has a quality 6=6 = 1 because every element is greater than the previous one, whereas the list [1, 4, 2, 3, 6, 5, 0] has a quality 3=6 = 0:5 because the pairs (1; 4); (2; 3); (3; 6) have the second element greater than the first, but the other pairs (4; 2); (6; 5); (5; 0) do not. A well-shuffled list will have a quality close to 0.5.
Write a function quality(L) that evaluates how well the list L is shuffled. Then write a function average quality(nswaps)that uses these functions to evaluate the average quality of a shuffle of the list of integers range(50), using nswaps swaps. To get reliable answers you will need to take the average over, say, 30 different shuffles. Finally, write a program that prints out the average quality of a shuffle of a list of length 50 using 5, 10, 15, . . . , 200 swaps.
The code should be in a file named shuffle.py. Organise the code so that the functions can be imported into other programs and the average qualities are printed out when the module is run as python shuffle.py. How many swaps do you think is a good number for a list of length 50? How many for a list of length N?
You should submit:
_ A paper copy of the code. (Via BART.)
_ A paper copy of the output of a run of your program, showing the qualities for different
numbers of swaps. (Via BART.)
_ A hard copy of your code. (Via BART.)
_ An electronic copy of your program in a _le named shuffle.py. (Electronic submission.)
The stylised picture of a fern (above) is an example of a fractal, an object with non-integer dimension, similar to the Koch curve from workshops. The dimension of this one is di_cult to calculate, but is something between a line, with dimension 1, and a plane, with dimension 2. Fractals are extensively used in describing and modelling natural objects with structure on many length scales, such as the coastline of Great Britain, the shape of clouds, the distribution of stars in the universe, the structure of intestinal walls, plants and so on. They are also used in computer graphics to make realistic models of terrain and natural scenery. The classic reference for fractals is Mandelbrot's book, The Fractal Geometry of Nature, which is in the library; there is lots of recent information on the web.
The details of the recursive construction are illustrated below.
The left hand picture shows the basic shape; in the full fern each of the branches is recursively replaced by a scaled down copy of the shape. The right hand picture shows the next level of the construction.
As shown in the pictures, the two top branches are each half as long as the original main trunk, while the branch on the left is situated halfway along the main trunk. The angles which the branches make with the trunk are not crucial, but suggested values are shown on the diagram. Thus to draw a fern with a turtle, the turtle should start at the base of the trunk and do the following:
(a) Draw the trunk;
(b) Turn left by 30_ and draw a fern at half the scale;
(c) Turn right by (30 + 40)_ and draw a fern at half scale;
(d) Move back to halfway along the trunk; turn left by 80_ and draw a fern at 0.3 scale;
(e) Move back to the base of the trunk.