python clearing files, programming homework help

Anonymous
timer Asked: Jul 6th, 2017
account_balance_wallet $50

Question Description

Everything is clear on the file

Put comment for every thing you did.

Unformatted Attachment Preview

CS 112 – Project #4 2D Lists, nested Loops th Due Sunday July 9 , 11:59pm. Background The purpose of this assignment is to practice building, inspecting, and modifying 2D lists effectively. This often requires nested for-loops, but not always. It involves thinking of the structure like an N x M matrix of labeled spots, each with a row- and column-index. As before, we are restricting ourselves to the most basic functionalities, and any others that you want, you should implement yourself. Two-dimensional lists aren't conceptually more difficult than single-dimension lists, but in practice the nested loops and more complex traversals and interactions merit some extra practice. We will create grids of cells and simulate Conway's "Game of Life", which is not really a game but a cellular automaton. Refer to our "Project Basics" in its entirety for information on naming your file, performing testing, grading details, and more. Consider it a part of this project's requirements, because it is. • Project Basics document: http://cs.gmu.edu/~marks/112/projects/project_basics.pdf • download this samples file, which has typed out many example GridStrings and some extra functionality that makes the project animated. Suggestion: paste this into your project immediately. http://cs.gmu.edu/~marks/112/projects/p4provided.py • Project Four tester file: http://cs.gmu.edu/~marks/112/projects/tester4p.py • Remember: your grade is significantly based on passing test cases – try to completely finish individual functions before moving on. The easiest way to implement many functions is to call the earlier/easier functions, so it will pay off twice to complete functions before moving on. Don't let yourself be "almost done" with a function, but be missing all the test cases! Grading Rubric Code passes shared tests: Well-documented/submitted: TOTAL: 90 10 100 +5 extra credit What can I use? You may only use the following things. You may ask about adding things to a list, but we are unlikely to add anything. If you use something disallowed, it's not an honor code violation, you just won't get points. Restrictions • no modules may be imported. Allowed basic statements, variables, operators, del, indexing, slicing, are all allowed any form of control flow we've covered is allowed (if/else, loops, etc) only these built-in functions: range(), len(), int(), str() only these built-in methods: s.split(), s.join(), s.pop(), xs.append(), xs.extend(), xs.insert(), s.format() • calling other functions of the project (and your own helper functions). Please do this! J • • • • Conway's Game of Life The wiki page is a decent introduction, too: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Conway's Game of Life is actually a simulation, not a game. We start with a grid of squares (any number of rows and columns). In each spot, we consider it "alive" or "dead". Each cell has 8 neighbors surrounding it (the horizontal, vertical, and diagonal cells). We consider the current grid to be one "generation", and the next generation is created based on the current cells in the current generation: we check whether a cell was living or dead, and how many living neighbors it had, to determine its next state: • a dead cell must have exactly 3 living neighbors to be born (become alive); otherwise, it remains dead. • a living cell must have exactly 2 or 3 living neighbors to survive; otherwise, it dies from isolation or overcrowding. Indexing a Grid These rules can be described as "B3/S23", meaning "born with 3 living (0,0) (0,1) … (0,M-1) neighbors, survives with 2 to 3 neighbors." We will be using these exact rules, though other rules are possible. When we have a grid, we have two dimensions. The first dimension indicates the row (top row to bottom row), and the second dimension indicates the column (left column to right column). Thus with N rows and M columns, we have a grid with indexes as shown to the right. (1,0) … … (1,M-1) … … … … (N-1,0) (N-1,1) … (N-1,M-1) If we saw the Grid below (left), with live cells in blue, then it has live cells at (1,2), (1,3), (2,1), (2,2), and (3,2). The next generation is shown to the right of it. Think through the B3/S23 rules for a few cells. original grid next generation (applying B3/S23 to original grid) When we watch a grid from generation to generation, sometimes it quickly dies out entirely; other times, it settles into an oscillating pattern (often very few patterns in the cycle); other times, it goes on seemingly forever before finally doing something less chaotic. The Grid above (with blue live cells), which is named the "rpentomino", has pretty chaotic-seeming behavior for many steps. If we give it a large enough grid (so that it doesn't hit the edges and mis-calculate), we can get many unique patterns. On a 10x12 board (10 rows tall, 12 columns wide), shown on the last page of this document, we can see the patterns of each successive generation, playing by our chosen rules of B3/S23. The edge eventually causes some trouble, (not enough places coming alive, not as many neighbors, and some die-offs result), and 52 generations later the grid is finally extinct. Goal We will implement a series of functions that build up grids and calculate a next generation. Along the way, we will be creating grids (from scratch or based on previous grids), inspecting grids, and modifying grids. First – Some Definitions We define a Grid and a GridString. They are just 2D lists and strings with particular structure inside, but the names will be convenient in describing the purpose of the functions we need to write. Python doesn't know about these names, it's just our own convention. Grid: a list of lists of Booleans. We expect all inner lists to be the same length. The outer (first) dimension represents the rows (from top to bottom), and the inner lists represent the items in that row, from left to right. The boolean value represents if the current cell is alive; True means alive, False means dead. • Grids are zero-indexed for both dimensions. No negative indexes are allowed for them; only use zero and up as the indexes. • Examples: o tiny_grid = [[True,False], [True, True]] o sample_grid = [[False, False, False, False, False], [False, False, True, True, False], [True, True, True, False, False]] GridString: a string of periods (.), capital O's, and newlines ('\n'). When printed, it would look like a rectangle of periods (dead cells) and O's (living cells), and we think of this as the grid representation. • Though a GridString is supposed to have the same number of items in each row, our program will carefully check this fact when we interact with GridString values. • blank lines in a GridString are ignored. This helps us create them with triple-quote strings. • We can reliably convert between a GridString and a Grid, in either direction. They store the same information in different ways. • triple-quote strings can represent newlines by actually typing the ENTER key. Removing empty lines (assumedly at the beginning/end) lets us create GridString values like the examples below. • Examples (all in the samples file p4provided.py) blinkerA = """ ..... ..O.. ..O.. ..O.. ..... """ blinkerB = """ ..... ..... .OOO. ..... ..... """ boat = """ ..... .OO.. .O.O. ..O.. ..... """ r_pentomino = """ ............ ............ ............ ............ ........OO.. .......OO... ........O... ............ ............ ............ """ Functions • read_coords(s): Given a GridString s, read through it and create a list of int pairs for all live cells. Each pair is a (row, column) coordinate. If the rows don't all have the same number of spots indicated, or if any unexpected characters are present, this function returns None. Must be ordered by lowest row, and lowest column when rows match. o Assume: s is a GridString. o Hint: s.split() is your friend. What should you split by? o o o read_coords("O..\n.OO\n") read_coords("\n\nO..\n.OO\n\n") read_coords(".....\n.....\n") → → → [(0,0), (1,1), (1,2)] [(0,0), (1,1), (1,2)] [] # ignore blank lines • get_dimensions(s): Given a GridString s, find out how many rows and columns are represented in the GridString, and return them as a tuple: (numrows, numcols). Remember that any blank lines must be ignored (skipped). If the rows don't all have the same number of items in them, or any unexpected characters are present, this function returns None. o Assume: s is a GridString. o o o get_dimensions("O..\n.OO\n") → get_dimensions("OOOOO\n...\nOO\n......") → get_dimensions("\nOO\n..\n.O\nO.\n..\n\n") → (2,3) None (5,2) # not rectangular! # note ignored blank lines • build_empty_grid(height, width): Given positive int values for the height and width of a grid, create a Grid that has False values at each location (representing dead cells). o Assume: height and width are positive integers. o o o o build_empty_grid(2,3) build_empty_grid(1,4) build_empty_grid(3,1) build_empty_grid(1,3) → → → → [[False, False, False], [False, False, False]] [[False, False, False, False]] [[False], [False], [False]] [[False, False, False]] • build_grid(s): Given a GridString s, determine the dimensions, build a grid of that size, and make alive each cell that should be alive. o Assume: s is a GridString. o Hint: try to use read_coords, get_dimensions, and build_empty_grid in your solution. o build_grid("O..\n.OO\n") → [[True, False, False], [False, True, True]] o build_grid("..\n.O\nOO\n") → [[False,False], [False, True], [True, True]] o build_grid("OO.O..") → [[True, True, False, True, False, False]] • show_grid(grid, live='O', dead='.'): Given a Grid, and the option to indicate what representation to use for live and dead cells, create the GridString that has no blank lines in it and represents the indicated grid. o Assume: grid is a Grid; live and dead are strings. o Hint: You'll likely want to call print(show_grid(someGrid)) when trying it out. o o o o show_grid([[True,False,False], [False,True,True]]) show_grid([[False,False],[True,True]]) show_grid([[False,False],[True,True]],'A','#') show_grid([]) → → → → "O..\n.OO\n" "..\nOO\n" "##\nAA\n" "" • count_living(grid): Given a Grid, count how many live cells there are; return that number. o Assume: grid is a Grid. o o count_living([[True,False,False], [True,False,True], [False,False,True]]) → 4 count_living([[True,True]]) → 2 • any_living(grid): Given a Grid, determine if any live cells are present. Return the bool answer. o Assume: grid is a Grid. o o any_living([[True,False,False], [True,False,True]]) → any_living([[False,False,False],[False,False,False]]) → True False • on_grid(grid, r, c): Given a Grid and two integers indicating row/column position, determine if (r,c) is on the grid and return the answer as a boolean. o Assume: grid is a Grid; r and c are integers. o Hint: Remember, the grid is zero-indexed, and negative indexes are not to be used. o Examples use as non-code placeholders for Grids, so that the examples are small. o on_grid(<3x5 grid>, 0, 0) → True o on_grid(<3x5 grid>, -1,-1) → False # negatives not allowed o on_grid(<3x5 grid>, 2, 4) → True # bottom-right corner o on_grid(<1x3 grid>, 1, 3) → False # zero-indexing, remember? • count_neighbors(grid, r, c): Given a Grid and two integers indicating row/column position, count how many living neighbors there are. When a cell is on the edge or corner of our Grid, treat all nonexistent neighbor positions as dead (they don't contribute to the returned count). o Assume: grid is a Grid; live and dead are strings of length one. o Hint: Use your on_grid definition! o Examples all use the grid g drawn to the right. g: § § § § § count_neighbors(g, count_neighbors(g, count_neighbors(g, count_neighbors(g, count_neighbors(g, 0, 0, 2, 3, 2, 0) 4) 4) 2) 3) → → → → → 0 1 2 2 5 • next_gen(grid): Given a Grid, create and return a new Grid that represents the next generation. Note that you will not be modifying the original Grid value – each cell's next state is dependent on its neighbors' previous state, so updating one cell at a time would incorrectly mix generation info. rB) rA) o Assume: grid is a Grid. nke nke at) bli bli (bo ( ( d d i ri d r ri g g g o Hint: Use previous definitions! _ _ _ ld ld ild bui bui =bu dC= dB= o Examples based on the GridStrings gridA gri gri shown in GridString definition. o o o o next_gen(gridA) → next_gen(gridB) → next_gen(gridC) → gridBcopy gridAcopy gridCcopy Notes § gridA and gridB are two parts of an "oscillator"; a stable cycle of grids. This one is called "blinker", and was included in the Definitions section. § gridC is a "still life" example, a grid that equals its next generation. This one is called "boat". § In each case, we should be generating a new Grid value – hence the copy annotations. • n_gens(grid, n=20): Given a Grid and a positive integer of how many generations to store, utilize your next_gen function to create a list with n generations in it, where the given grid is included as the first generation. o Assume: grid is a Grid; n is a positive integer that may default to 20 when not given. o Hints: § Use your next_gen definition! § you are returning a list of Grid values – it is a list that is 3 levels deep! o The examples would get pretty large – rn indicates the same numbered generations from the page of r_pentomino steps; gridA, gridB, and gridC are also defined above (blinker and boat). o o o o o n_gens(r_pentomino,3)→ n_gens(gridA,5) → n_gens(gridB,3) → n_gens(gridC,4) → n_gens([[False]]) → [r0, r1, r2] # see the last page [gridA, gridB, gridA, gridB, gridA] [gridB, gridA, gridB] [gridC, gridC, gridC, gridC] [[[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]], [[False]]] #20 iterations provided by default • is_still_life(grid, limit=100): Given a single Grid, determine if its sequence of generations becomes a "still life", where after some point, each generation matches the previous generation. Note that the chosen cut-off number of generations can change the answer (if a Grid takes 30 generations to settle into a still life, but we only look at 10 generations, we won't realize it, and report False). o Assumes: grid is a Grid, and limit is a positive integer that may default to 100. o Hint: try comparing two different (consecutive) Grid values with the == operator. o Examples use names of provided GridStrings. o o o o o is_still_life(build_grid(boat),5) is_still_life(build_grid(boat),1) is_still_life(build_grid(blinkerA),10) is_still_life(build_grid(r_pentomino),50) is_still_life(build_grid(r_pentomino),55) → → → → → True False False False True # one grid: can't see still-life # period is 2; this isn't still. # all-dead equals all-dead. • is_cycle(grid, limit=100): Given a single Grid, determine if its sequence of generations becomes an "oscillator", where after some point, a repeating sequence of at least two distinct grids occurs. Note that still life grids are not cycles! Again, the chosen number of generations to inspect (limit) may be short enough to prohibit our finding the cycle. o Assumes: grid is a Grid, and limit is a positive integer that may default to 100. o Hint: use your n_gens function. Also – what function on a previous project could be useful?? o Examples use names of provided GridStrings. o o o o o is_cycle(build_grid(boat),5) is_cycle(build_grid(blinker),10) is_cycle(build_grid(r_pentomino),50) is_cycle(build_grid(pulsar),5) is_cycle(build_grid(pulsar),3) → → → → → False True False True False # still-lifes ≠ oscillators Some provided definitions (to animate your code!) • PROVIDED CODE: print_gens(gs, live='O', dead='.'): Given a list of Grid values, prints each one and calls input() with no arguments afterwards to pause for effect. When running this in the terminal, if you mash down the ENTER button, you'll have a very cheap animation effect! o Assumes: gs is a list of Grid values. live and dead are strings. o print_gens(n_gens(anyGrid)) → • PROVIDED CODE: go(s, limit=100): Given a GridString, this function will build the Grid, calculate limit number of generations, and print them all. As this function does printing and user interaction, we're just providing it for you to play with your finished project. o Assumes: s is a GridString; limit is a positive integer that may default to 100. o go(s,n) → Extra Credit Solve this problem for extra credit (up to +5%) and good practice. • next_gen_growable(grid): Re-implement the next_gen function so that the border never interacts with the simulation. We'll do this by extending the size of the Grid whenever there's a live cell in the border. This means we must add an extra row or column of dead cells. We need to check for live border cells both before calculating the next generation to avoid incorrect calculations, and also after calculating the next generation, so that we never report a grid with live border cells. This means your Grid will possibly grow, and never shrink, in dimension. It also may stay the same size when all the action is in the center. o Assume: grid is a Grid. o next_gen_growable([[True]]) → [[False,False,False], [False,False,False], [False,False,False]] # original True required addition of borders, and then that cell died a lonely death. o >>> print(show_grid(next_gen_growable(build_grid(""" .O... .O..O ...OO .O..O """)))) # ← end of the multi-line command. size was 4x5. ....... ....... ..OOO.. ..OOOO. ...OO.. ....... # ← new size is 6x7. New top/bottom rows and right column added before, and new right column added after. • n_gens_growable(grid, n=20): Re-implement n_gens to allow the grid to grow from generation to generation. o Assumes: grid is a Grid; n is a positive integer that may default to 20 when not given. o print_gens(n_gens_growable(anyGrid)) → • PROVIDED CODE: go_growable(s, limit=100): Given a GridString, this function will build the grid, calculate limit number of growable generations, and print them all. As this function does printing and user interaction, we're just providing it for you to play with your finished project. o Assumes: s is a GridString; limit is a positive integer that may default to 100. o go(s,n) → Many iterations of r_pentomino >>> go(r_pentomino) 0 ............ ............ ............ ............ ........OO.. .......OO... ........O... ............ ............ ............ 5 ............ ............ ............ ........O... ......OO.OO. ......O..O.. .......OO... ............ ............ ............ 10 ............ ............ ............ .....OO..... ....OO...... .....OO..... ......OOO... ........O... ........O... ............ 15 ............ ............ ....OO...... ..OO.OO..... ..O....O.... ..O...OOO... ...OO....O.. .......O.O.. .......OO... ............ 1 ............ ............ ............ ............ .......OOO.. .......O.... .......OO... ............ ............ ............ 6 ............ ............ ............ .......OOO.. ......OO.OO. ......O..OO. .......OO... ............ ............ ............ 11 ............ ............ ............ ....OOO..... ....O....... ....O....... .....OO.O... ........OO.. ............ ............ 16 ............ ............ ...OOOO..... ..OO.OO..... .OO..O..O... ..O...OOO... ...O..O..O.. .......O.O.. .......OO... ............ 2 ............ ............ ............ ........O... .......OO... ......O..O.. .......OO... ............ ............ ............ 7 ............ ............ ........O... ......OO.OO. ......O..... ......O...O. .......OOO.. ............ ..... ...
Purchase answer to see full attachment

Tutor Answer

criss53
School: Purdue University

At...

flag Report DMCA
Review

Anonymous
Thank you! Reasonably priced given the quality not just of the tutors but the moderators too. They were helpful and accommodating given my needs.

Similar Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors