Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Activity Based Cost Management In Government

Authors: Gary Cokins

2nd Edition

1567261817, 978-1567261813

More Books

Students also viewed these Accounting questions

Question

=+ How can they be incorporated into social media content?

Answered: 1 week ago