联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2019-03-13 10:23

7Task 3: Quilts

In this problem, having a partner is optional, but is strongly recommended.

The CS1 Etsy shop wants to expand the items offered on their site. Quilts would be an

item that could generate a lot of revenue. Below is an example of one of the CS1 Etsy

quilt designs, which we will call winter quilt.

Another example is the following, which we will call the autumn quilt:

You and several other CS1 students have been hired as interns at the CS1 Etsy shop to

help design quilts. Your project is to use pictures to generate the quilt designs shown

above. You'll notice that the quilts are the same except for the color scheme.

These quilts are just two sample of the quilt design. In your code, you will write functions

that will take color parameters, and therefore can generate lots of different quilts, in the

same pattern as above, but with color variation.

The picture.py module, which contains picture functions you have learned about in

Lecture 05 and Lab 04, is provided in the ps04 folder. You may use the functions

defined in that module to help create your quilts. All the functions that you will define for

this problem will be in a file that you will create called ps04Quilt.py.

Your goal is to write a quilt function that takes several color parameters and returns a

picture corresponding to the quilt shown above when quilt is invoked with appropriate

colors. This picture is ultimately generated by combining primitive pictures created by

the following two functions that you have already seen in class. They can be found in

the picture.py file:

patch (c):

"""Returns a rectangular patch of color c with a

black border that fills a given picture frame.

"""

triangles(c1,c2):

"""Returns a picture that consists of two triangles filling the

given picture frame: a black-bordered triangle of color c1

in the lower left corner of the frame; and a black-bordered

triangle of color c2 in the upper right corner of the frame.

"""

For example, below are the pictures generated by some sample invocations of these

functions:

patch('red') triangles('red', 'blue')

Divide, Conquer, and Glue

The key to solving the problem of defining the quilt function is to note that the picture

it returns can be decomposed into smaller pictures that are used more than once in the

larger picture. For example, by wishful thinking we can image that the upper right

quadrant is a picture returned by a function we'll call block:

block('navy', 'lightskyblue', 'darkseagreen', 'papayawhip', 'plum', 'orchid')

The block is clearly decomposable into subquadrants of two kinds, which we've

dubbed rose and diamond. Here are two roses:

rose('darkseagreen','papayawhip) rose('navy','lightskyblue')

Here are two examples of the pattern we called diamond:

diamond('plum',

'orchid','darkseagreen')

diamond('navy',

'lightskyblue','darkseagreen')

Implement each of these, then combine these subproblem solutions to solve the whole

quilt. How to solve these? Each of these patterns can be broken down into quadrants

and those subproblems solved and combined with operations like rotation to solve the

pattern.

This is an excellent illustration of the divide, conquer, and glue problem solving strategy

we will use throughout this course:

1. Divide the problem into subproblems. Here, the whole quilt problem can be

solved if we can solve the problem of generating the pictures in the different

quadrants and sub-quadrants.

2. Conquer the subproblems by solving them. In this case, you'll want to solve

subproblems like rose and diamond that will help solve the whole problem.

3. Glue the solutions to the subproblems together to form the solution to the whole

problem. Here, we combine four rotated versions of the picture returned by block

to construct the picture returned by quilt.

But how do we solve the problems of defining the rose and diamond functions? By

applying the divide, conquer, and glue strategy again! Of course, all of these functions

will take appropriate color parameters.

Continue this way, decomposing each problem into smaller subproblems, until you

reach problems that can be solved using our primitives (e.g., patch and triangles).

For an example of this sort of decomposition, see the quilt example from Lecture 05.

Starting the Assignment

Begin by creating a file named ps04Quilt.py. As usual, your file must start with

comments at the top, identifying the author(s), username(s), problem set and task

number, filename, and date. Follow this format:

# Your name:

# Your username:

# Your partner's name (if applicable):

# Your partner's username (if applicable):

# CS111 PSO4, Task 3

# ps04Quilt.py

# Submitted:

Remember to include the line:

from picture import *

at the top of your file, so that all of the functions in the picture.py file are available to

you.

Then use incremental programming to make progress on your quilt. That is, make

approximations to your quilt picture or parts of your quilt picture and use displayPic

to display these pictures along the way.

You can work in one of two directions:

1. In the bottom-up strategy, you start with smaller helper functions, like those

defined in the Helper Functions section below, and build up pictures of

increasing complexity, including rose and diamond, until you have correctly

defined the full quilt picture.

2. In the top-down strategy, you start by defining the quilt function in terms of the

block function or the rose and diamond functions. But those are complicated,

so you might put off the diamond subproblem while you work on the rose

subproblem. So for testing purposes, you instead provide a so-called stub

picture for the diamond function. For example, as a very crude approximation,

you might define an initial version of the diamond function that returns the result

of calling triangles on two of its parameters. You can then test quilt with this

approximation. Then you continue the top-down descent by defining

initially-crude-approximations for functions that return the quadrants of the block

picture. You continue this top down process until you reach pictures that are

correctly implemented by patch and triangles, at which point you now have

the correct quilt function rather than just an approximation.

Colors and Parameters

There are exactly six colors used in the Winter quilt and a different six colors in the

Autumn quilt. All of the colors for the Winter quilt are listed in the examples above. The

Autumn quilt uses the colors in the following examples:

rose('red','gold')

rose('darkred','coral')

diamond('darkred','coral','red')

diamond('darkorange','bisque','red')

Your top-level quilt function will take six colors as arguments and return the

appropriate quilt. You get to choose the order of the six color parameters.

However, you must define zero-argument functions winterQuilt and autumnQuilt

that return the result of calling the quilt function on six appropriate color arguments.

def winterQuilt():

'''Returns the first quilt with the blue-green-purple theme'''

return quilt( ... six color arguments go here ... )

def autumnQuilt():

'''Returns the second with the red-orange-yellow theme'''

return quilt( ... six color arguments go here ... )

The quilt pictures returned by these functions should match the ones depicted at the

beginning of this task. Codder will test the results of these two functions.

Note: Ideally, color parameters should have meaningful names, but with functions like

quilt and block that take lots of (six) color parameters, it's challenging to design

such names. In our solution, the main quilt function takes six parameters, which we

named c1, c2, ... c6. Then in the various subfunctions, which take fewer colors,

we didn't want to use the same numbers, because it might be confusing. If we think of

c1 as navy for example, we don't want to use c1 in another function where it might

mean plum. So some functions have arguments named cx, cy and cz. Of course,

Python doesn't care what we call our parameters, and we can use the same parameter

names in several different functions. But remember that programs are written for

humans to read and only incidentally for computers to execute. Even reading our own

code, we want to keep names clear in our minds.

Helper Functions

As we have seen, a general principle of computer science is Don't Repeat Yourself

(DRY), which means you should avoid writing the same pattern of code more than once.

If you find yourself writing the same or similar code more than once in your program,

you should write functions that capture the patterns of the repeated code and invoke the

functions instead.

The divide, conquer, and glue process of defining quilt naturally exposes the need for

numerous auxiliary functions. Above, we have seen the block, rose, and diamond

functions, which you must define. As part of working on this task, you must also define

and use the following helper functions:

def bandana(c1, c2):

"""Returns a 2x2 picture with solid c1-colored patches in the lower left

and upper right quadrants, and triangles(c1, c2) in the upper left

and lower right quadrants."""

def lowerLeft(p):

"""Returns a picture which divides the picture space into four

quadrants and places the given picture in the lower left quadrant.

Nothing is in the other quadrants."""

def lowerLeftNest(p1, p2):

"""Returns a picture in which picture p2 is placed in the the lower left

quadrant of picture p1."""

def lowerLeftNestSelf4(p):

"""Returns a picture that has four copies of picture p: one full size one,

and three successively smaller versions descending in the

lower left quadrant."""

Here is an example invocation of bandana:

bandana('red', 'blue')

Note that the lowerLeft, lowerLeftNest, and lowerLeftNestSelf4 functions

take pictures as arguments, not colors. To illustrate these helper functions, suppose that

pictures picP1 and picP2 are defined as:

picP1 picP2

Then here are examples of calling the lowerLeft, lowerLeftNest, and

lowerLeftNestSelf4 functions with these pictures:

lowerLeft(picP1) lowerLeftNest(picP1,

picP2)

lowerLeftNestSelf4(picP2)

Notes:

● In this problem, you should not use any conditional statements.

● In this problem, you should use functions from PictureWorld (like fourPics,

rotations, and clockwise90). You should not directly create any

cs1graphics objects (such as layers), nor should you use any cs1graphics

methods (such as .clone, .rotate, .scale, and .moveTo).

● You must define the following functions in your program: bandana,

lowerLeft, lowerLeftNest, lowerLeftNestSelf4, rose,

diamond, block, quilt, winterQuilt, autumnQuilt. Additionally,

you should define one or more other functions that help you abstract over other

patterns.

● You can use lowerLeftNestSelf4 in conjunction with triangles to create

one of the patterns that appears in the quilt:

lowerLeftNestSelf4(triangles('darkslateblue', 'navy'))

● Stepping back, your high-level goal in this problem is not only to generate the

correct quilt picture, but to do so in a way that takes advantage of the power of

abstraction to eliminate unnecessary repeated patterns in your code to make it

as simple and elegant as possible. You want solution code that has the same

level of abstraction and elegance as the code in Lecture 05 quilt.

Testing

As you implement the above required helper functions and your own helper functions,

you'll want to test them! You should put the following testing code at the end of

ps04Quilt.py:

# ------------------------- Testing -----------------------

if __name__ == "__main__":

'''All code that displays pictures or prints output should be nested

within this special "main" header at the end of ps04Quilt.py

'''

closeAllPics() # Use this to close all previously created pictures.

# Uncomment particular lines below to test your code.

# Add more test cases for functions that you define!

#displayPic(bandana('red', 'blue'))

#displayPic(lowerLeft(triangles('red', 'blue')))

#displayPic(lowerLeftNest(patch('red'), triangles('red', 'blue')))

#displayPic(lowerLeftNestSelf4(triangles('navy', 'lightskyblue')))

## Roses

#displayPic(rose('navy', 'lightskyblue'))

#displayPic(rose('darkseagreen', 'papayawhip'))

#displayPic(rose('red','gold'))

#displayPic(rose('darkred','coral'))

## Diamonds

#displayPic(diamond('plum', 'orchid', 'darkseagreen'))

#displayPic(diamond('navy', 'lightskyblue', 'darkseagreen'))

#displayPic(diamond('darkred','coral','red'))

#displayPic(diamond('darkorange','bisque','red'))

## Blocks

#displayPic(block('navy', 'lightskyblue', 'darkseagreen',

# 'papayawhip', 'plum', 'orchid'))

## Quilts

#displayPic(winterQuilt())

#displayPic(autumnQuilt())

# Extra invocations to displayPic to fix picture glitch

# (a simple picture is sometimes needed to have previous pictures display)

displayPic(patch('red'))

displayPic(patch('blue'))

Recall that in order to see your pictures, you'll need to to invoke displayPic with an

argument which corresponds to the picture you want to display. As indicated in the

above code, you should put all invocations of displayPic at the very end of

ps04Quilt.py indented within the special header if __name__ == "__main__":

Notes:

● There is a glitch that sometimes happens with pictures as follows: a complex

picture may not display unless one or two simple pictures are displayed

afterwards. To play it safe, it is wise to always display two simple pictures after a

complex one, like this:

displayPic(complex picture here) # this is the one you want to see

displayPic(patch('red')) # 1st simple picture here

displayPic(patch('blue')) # 2nd simple picture here

● The closeAllPics() function call closes all previously created pictures. The

above testing code calls this first so you will only see the pictures from clicking

the Run button, and not pictures from previous Runs.

Completing the Assignment

You should define all the helper functions specified above. In addition to that, you

should define other functions that capture other patterns that appear in the quilt. It is

also helpful to define local variables within your functions to give names to pictures that

you generate as part of your solution.

Each of your functions should be short: no more than a few statements long (say 7 or

8). If you find yourself defining a longer function, consider how you can break it up into

smaller parts (preferably in a way that permits one or more of the smaller functions to be

used multiple times).

Both Quilts

Remember that your quilt function takes six colors as arguments, so you will be able to

create both the winter and autumn quilts with the same function, just supplying different

sets of colors.


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp