Read chapter 4 of the book Blown To Bits, pages 109-137. (Feel free to read the whole chapter if the material interests you.)
from random import * def roll1(): return randint(1,6) # simulate a roll of a six-sided die def roll2(): return randint(1,6) # simulate a roll of a six-sided die def simple_game(): total = 0 for i in range(1,11): if roll1() != roll2(): total = total + roll1() + roll2() else: total = 0 return total
Suppose that Alice simulates the roll of a pair of dice by defining the roll function below, calling it twice and adding the results:
def roll(): return randint(1,6)Bob realizes that the roll of a pair of dice results in a sum of 2 through 12, inclusive, so he simulates the roll of a pair of dice by defining the roll function below, calling it only once:
def roll(): return randint(2,12)
Are these equivalent in terms of their behavior over time as we generate roll after roll? Why or why not?
[ [ [255,0,0] , [0,255,0] ],[ [0,0,255] , [255,255,255] ],[ [0,0,0] , [0,0,0] ] ]
We can remove the red components of an image using the following function in Python:
def remove_red(image): num_rows = len(image) num_columns = len(image[0]) for row in range(0,num_rows): for column in range(0,num_columns): green = image[row][column][1] blue = image[row][column][2] image[row][column] = [0, green, blue] return None
It is possible to accelerate the performance for the remove_red function by modifying it to perform the work on the different pixels concurrently instead of following the ordering given by the loops. Could the same be done for the problem of implementing a function that returns an array of n pseudorandom numbers generated using a linear congruential generator? Why or why not?
1. Write a personal message to the faculty member on the inside of a card (10 minutes). 2. Hand draw a scene with the club logo on the cover of the card with colored pencils. (15 minutes) 3. Cut out and form a special envelope for the card out of a sheet of premium paper. (3 minutes) 4. Seal the card in the envelope with glue, look up and write the address of the faculty member, and put a stamp on the card. (2 minutes)
Assume each comparator (i.e. the circles) takes time t to compare its two elements and output its results and that the time for a data value to go from one comparator to the next negligible. We wish to sort 1000 sets of 6 integers each.