import random import numpy as np import matplotlib.pyplot as plt #%matplotlib inline class Bubble(): size...

70.2K

Verified Solution

Question

Accounting

image

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

Answer & Explanation Solved by verified expert
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students