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 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started