Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Python class to simulate an ecosystem containing two types of creatures,

ID: 3707475 • Letter: W

Question

Write a Python class to simulate an ecosystem containing two types of creatures, bears and fish.

Please!!!! Read the question carefully! There is no comparision about strength and gender!!!!!!!! Please do not copy some answers which do not actually fit my question. Thanks for your help.

Write a Python class to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large list Each element of the list should be a Bear object, a Fish object, or None. In each time step, based on a random process, each animal either attempts to move into an adjacent list location or stay where it is. If two animals of the same type are about to collide in the same cell, then they stay where they are, but they create a new instance of that type oi animal, which ls placed ?n a random empiyu.e", previously Norie location ln ihe list. If a bear and a fish collide, however, then the fish dies (i.e., it disappears) Write an initializer for the ecosystem class, the initializer should allow the user to assign the initial values of the river length, the number of fishes and the number of bears. Before the simulation, fishes and bears should be allocated randomly into the river. The ecosystem class should also contain a simulation() method, which will simulate the next N steps of the random moving process. N should be inputted by the user. In each step of your simulation, all animals in the river should try to take some random moves. For example, assume that before the simulation, the initial state of the river is: NNFNNNBNBNN In which, 'F, 'B' and 'N' denote fish, bear and empty location respectively. Assume that in the first step of simulation, the first fish will move to the left, the first bear will move to the right, and the second bear will remain still. Then after the first step, the state of the river is: NFNNNNNBBNN

Explanation / Answer

Please find the python class under below indentation is very very important in python.........
#!/usr/bin/env python3
"""
Simulation of river environment containing bears and fish.
"""
import random
class Animal:
"""Animals are the inhabitants of the river"""
def __init__(self, gender = None, strength = None):
"""Initialize animal
gender gender of the animal (M, F) determines mating or fighting (default random)
strength stregth of animal, determines winner of fight (default random)
"""
if not gender:
self._gender = random.choice(['M','F'])
else:
self._gender = gender
if not strength:
self._strength = random.randint(0,9)
else:
self._strength = strength
def get_gender(self):
"""Return the animals gender"""
return self._gender
def get_stren(self):
"""Return the animals strength"""
return self._strength

class Bear(Animal):
def __init__(self, gender = None, strength = None):
super().__init__(gender, strength)
class Fish(Animal):
def __init__(self, gender = None, strength = None):
super().__init__(gender, strength)
class River:
"""A river is in array containing animals"""
def __init__(self, length):
"""Initialize a river with a random assortment of bears, fish, and empty cells
length length of the river
"""
self._length = length
self._contents = []
for i in range(self._length):
rval = random.randint(1,3)
if rval == 1:
self._contents.append(Bear())
elif rval == 2:
self._contents.append(Fish())
else:
self._contents.append(None)
def __len__(self):
"""Return the length of the river"""
return self._length
def __getitem__(self, k):
"""Return the contents of the kth cell in the river list"""
return self._contents[k]
def __setitem__(self, k, val):
"""Set the contents of the kth cell in the river list"""
self._contents[k] = val
def count_none(self):
"""Count the number of empty cells in the river list"""
return self._contents.count(None)
def add_random(self, animal):
"""Add animal to empty cell of river list after mating occurs"""
if self.count_none() > 0:
choices = [i for i, x in enumerate(self._contents) if x==None]
index = random.choice(choices)
self._contents[index] = animal
def update_cell(self, i):
"""Update the cell based on rules defined above"""
if self._contents[i] != None:
move = random.randint(-1,1) #animal can either move forward, back, or stay in place
if move != 0 and 0 <= i + move < self._length:
if self._contents[i + move] == None:
self._contents[i + move] = self._contents[i]
self._contents[i] = None
elif type(self._contents[i]) == type(self._contents[i+move]):
if self._contents[i].get_gender() != self._contents[i+move].get_gender():
#two animals of the same type and different gender mate
if type(self._contents[i]) == Bear:
self.add_random(Bear())
else:
self.add_random(Fish())
else: #two animals of the same type and gender fight
if self._contents[i].get_stren() > self._contents[i+move].get_stren():
self._contents[i+move] = self._contents[i]
self._contents[i] = None
else:
#bear always kills fish if they encounter eachother
if type(self._contents[i]) == Bear:
self._contents[i + move] = self._contents[i]
self._contents[i] = None
def update_river(self):
"""Update each cell in the river"""
for i in range(len(self._contents)):
self.update_cell(i)
def print_river(self):
"""Print the river contents in human readable form
Each cell displays the animal type, strength, and gender between two pipes
Example: male bear with strength 8 |B8M|
female fish with strength 0 |F0F|
"""
s = '|'
for x in self._contents:
if x:
if type(x) == Bear:
s += 'B'
elif type(x) == Fish:
s += 'F'
s += str(x.get_stren())
s += x.get_gender()
else:
s += ' '
s += '|'
print(s)

if __name__ == "__main__":
river = River(10)
for i in range(10):
print("Year ", i)
river.print_river()
river.update_river()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote