![]() In the case asked about here, a = -1 and n = 60. We can re-arrange the division equation above for r to get r = a - nq. One of the useful applications is the ability to use % to "wrap" co-ordinates so that they loop back on themselves at the edges, as done in the implementation of the Game of Life seen in the above code. This turns out to have a bunch of useful applications in coding, which is why the creator of Python chose to implement the modulus operator % in this way, as explained in detail here. But there's no reason why we can't go with the other value, taking the "remainder" to be the difference between the dividend and the next higher multiple of the divisor instead! To do this, we just need to use floored division, which can be simplified as "rounding down" the quotient q = a/n to the nearest integer below it. When doing basic arithmetic, we usually go with the values that give a positive r, as this corresponds to our more typical notion of what a "remainder" is (I'm sure there are many deeper number theory reasons as well!). A great visualisation and explanation of this can be seen at, which I highly recommend reading to help understand this better. The positive remainder corresponds to the difference between the multiple of n immediately below the dividend a and a itself, while the negative remainder corresponds to the difference between the multiple of n immediately above the dividend a and a itself. Unless the remainder is exactly 0, for any a and n, there are two options for q and r which will satisfy the equation - one positive, and one negative. Paraphrasing, when some number a (the 'dividend') is divided by another number n (the 'divisor'), we can define the quotient q and remainder r with the following equation: There are several options for implementing the division and modulus operators, both in programming and in math. I'm just going through Automate the Boring Stuff myself, and was also curious why (-1) % 60 evaluates to 59. I'd like to answer Artyom's question for anyone else who is interested and stumbles across this question. Here is my code maybe it will help you and a question for you why is (-1) % 60 equal to 59(I didn't get it)? import random, time, copy Time.sleep(1) # Add a 1-second pause to reduce flickering. # Dead cells with 3 neighbors become alive: # Living cells with 2 or 3 neighbors stay alive:Įlif currentCells = ' ' and numNeighbors = 3: If currentCells = '#' and (numNeighbors = 2 or numNeighbors = 3): # Set cell based on Conway's Game of Life rules: NumNeighbors += 1 # Bottom-right neighbor is alive. NumNeighbors += 1 # Bottom neighbor is alive. NumNeighbors += 1 # Bottom-left neighbor is alive. NumNeighbors += 1 # Right neighbor is alive. NumNeighbors += 1 # Left neighbor is alive. NumNeighbors += 1 # Top-right neighbor is alive. NumNeighbors += 1 # Top neighbor is alive. NumNeighbors += 1 # Top-left neighbor is alive. # `% WIDTH` ensures leftCoord is always between 0 and WIDTH - 1 # Calculate the next step's cells based on current step's cells: Print() # Print a newline at the end of the row. ![]() Print(currentCells, end='') # Print the # or space. Print('\n\n\n\n\n') # Separate each step with newlines. NextCells.append(column) # nextCells is a list of column lists. can you please review the code and briefly decode it for me. i intended to get a result of the conway program to be in grid. ![]() i have this code given below from book "how to automate boring stuff with python" i had run this code but the output doesn't seem that the code is correct. I need to create a python conways game of life.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |