Question: import random import numpy as np import matplotlib.pyplot as plt #%matplotlib inline class Bubble(): size = 20 def __init__(self, x, y): self.x = x self.y

 import random import numpy as np import matplotlib.pyplot as plt #%matplotlib

import random import numpy as np import matplotlib.pyplot as plt #%matplotlib inline

class Bubble(): size = 20 def __init__(self, x, y): self.x = x self.y = y self.dx = random.randint(1,4) * random.choice([1,-1]) self.dy = random.randint(1,4) * random.choice([1,-1]) self.life = random.randint(5,20) def getstate(self): return (self.x, self.y, self.size, self.life) def tic(self): self.x += self.dx self.y += self.dy self.life -= 1

def bplot(b, s): #enter the code here size = 100 numbubbles = 20

bubbles = [] for i in range(numbubbles): bubbles.append(Bubble(size/2, size/2))

xvalues = [b.getstate()[0] for b in bubbles] yvalues = [b.getstate()[1] for b in bubbles] sizes = [b.getstate()[2] for b in bubbles] plt.scatter(xvalues, yvalues, s=sizes) plt.xlim(0,size-1) plt.ylim(0,size-1) plt.pause(1) plt.clf()

while bubbles: popped = [] for i in range(len(bubbles)): b = bubbles[i] b.tic() gone = False state = b.getstate() if state[0] = size or state[1] = size: print("gone!") gone = True if state[3] Question 2 - Arrays, Grids, Files and Plotting (15 marks) . Question 2a (5 marks) Starting with the code below, modify and extend it to: 1. Ask the user for the size of the area and the number of bubbles, with validation of the inputs (2 marks) 2. Change the plotting code to use the bplot method, reducing repetition (1 mark) 3. At the end of the code, print the number of bubbles that popped, the number that drifted and the number of iterations taken before they were all gone Question 2 - Arrays, Grids, Files and Plotting (15 marks) . Question 2a (5 marks) Starting with the code below, modify and extend it to: 1. Ask the user for the size of the area and the number of bubbles, with validation of the inputs (2 marks) 2. Change the plotting code to use the bplot method, reducing repetition (1 mark) 3. At the end of the code, print the number of bubbles that popped, the number that drifted and the number of iterations taken before they were all gone

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!