Python Programming Assignment Help

Python Task on Island of Rats and Cats

from numpy import random
def get_rain():
if random.rand() < rain_chance:
return 0
return max(0, random.normal(rain_mean, rain_std, size=(1, 1))[0][0])
def init_rats_list():
for _ in range(0, rats_init):
rats_list.append(Rat(random.randint(1, rats_init + 1)))
def init_cats_list():
for _ in range(0, cats_init):
cats_list.append(Cat(random.randint(1, 1001)))
def remove_dead():
for berry in berries_list:
if not berry.is_eatable():
berries_list.remove(berry)

for rat in rats_list:
if rat.is_dead():
rats_list.remove(rat)

for cat in cats_list:
if cat.is_dead():
cats_list.remove(cat)
class Berry:
def __init__(self):
self.ageDays = 0

def pass_day(self):
self.ageDays += 1

def is_eatable(self):
return self.ageDays <= berry_persist
class Rat:
def __init__(self, age):
self.eat_days_ago = 0
self.days = age
self.berries_eaten = 0

def pass_day(self):
self.days += 1
self.eat_days_ago += 1

def try_eat(self):
berries_num = len(berries_list)
if berries_num > 0:
r = random.randint(0, berries_num)
self.eat_days_ago = 0
self.berries_eaten += 1
del berries_list[r]

def is_dead(self):
if self.eat_days_ago >= 3:
return True

if self.days > rats_old_begin_age:
if random.rand() < 0.01 * rats_old_step_prob * (self.days – 49):
return True
return False

def give_birth(self):
if self.berries_eaten < rats_birth_init_berries:
return
if (self.berries_eaten – rats_birth_init_berries) % rats_birth_step_berries == 0:
rats_born = random.randint(rats_birth_litter_lower_bound, rats_birth_litter_upper_bound + 1)
for _ in range(1, rats_born):
rats_list.append(Rat(0))
class Cat:
def __init__(self, days):
self.days = days
self.eat_days_ago = 0
self.rats_eaten = 0

def pass_day(self):
self.days += 1
self.eat_days_ago += 1

def try_eat(self):
rats_num = len(rats_list)
if rats_num > 0:
if random.rand() < (cats_catch_coefficient * rats_num)/island_area + self.days * cats_not_eat_days_limit:
r = random.randint(0, rats_num)
self.eat_days_ago = 0
self.rats_eaten += 1
del rats_list[r]

def is_dead(self):
if self.eat_days_ago >= cats_not_eat_days_limit:
return True

if self.days > cats_old_begin_age:
if random.rand() < 0.01*(cats_old_begin_prob + cats_old_step_prob*(self.days – cats_old_begin_age)):
return True
return False

def give_birth(self):
if self.rats_eaten < cats_birth_init_rats:
return
if (self.rats_eaten – cats_birth_init_rats) % cats_birth_step_rats == 0:
rats_born = random.randint(cats_birth_litter_lower_bound, cats_birth_litter_upper_bound + 1)
for _ in range(0, rats_born):
cats_list.append(Cat(0))
def simulate():
rain = 0
init_rats_list()
init_cats_list()

for i in range(1, days_limit):
new_berries = int(rain * island_area * berry_coefficient)
for _ in range(1, new_berries):
berries_list.append(Berry())

for berry in berries_list:
berry.pass_day()

for rat in rats_list:
rat.try_eat()
rat.give_birth()
rat.pass_day()

for cat in cats_list:
cat.try_eat()
cat.give_birth()
cat.pass_day()

rain = get_rain()
remove_dead()
def read_properties(filename):
fin = open(filename)
properties = {}
for line in fin.read().splitlines():
line_parts = line.split(“=”)
properties[line_parts[0].strip()] = line_parts[1].strip()
fin.close()
return properties
properties = read_properties(“input.txt”)
days_limit = int(properties[“days_limit”])
island_area = int(properties[“island_area”])
rain_chance = float(properties[“rain_chance”])
rain_mean = float(properties[“rain_mean”])
rain_std = float(properties[“rain_std”])
berry_coefficient = float(properties[“berry_coefficient”])
berry_persist = int(properties[“berry_persist”])
rats_init = int(properties[“rats_init”])
rats_init_age_upper_bound = int(properties[“rats_init_age_upper_bound”])
rats_not_eat_days_limit = int(properties[“rats_not_eat_days_limit”])
rats_old_begin_age = int(properties[“rats_old_begin_age”])
rats_old_step_prob = float(properties[“rats_old_step_prob”])
rats_birth_init_berries = int(properties[“rats_birth_init_berries”])
rats_birth_step_berries = int(properties[“rats_birth_step_berries”])
rats_birth_litter_lower_bound = int(properties[“rats_birth_litter_lower_bound”])
rats_birth_litter_upper_bound = int(properties[“rats_birth_litter_upper_bound”])
cats_init = int(properties[“cats_init”])
cats_init_age_upper_bound = int(properties[“cats_init_age_upper_bound”])
cats_not_eat_days_limit = int(properties[“cats_not_eat_days_limit”])
cats_catch_coefficient = float(properties[“cats_catch_coefficient”])
cats_age_catch_coefficient = float(properties[“cats_age_catch_coefficient”])
cats_old_begin_age = int(properties[“cats_old_begin_age”])
cats_old_begin_prob = float(properties[“cats_old_begin_prob”])
cats_old_step_prob = float(properties[“cats_old_step_prob”])
cats_birth_init_rats = int(properties[“cats_birth_init_rats”])
cats_birth_step_rats = int(properties[“cats_birth_step_rats”])
cats_birth_litter_lower_bound = int(properties[“cats_birth_litter_lower_bound”])
cats_birth_litter_upper_bound = int(properties[“cats_birth_litter_upper_bound”])

berries_list = []
rats_list = []
cats_list = []

simulate()

fout = open(“output.txt”, “w”)
fout.write(“By the end of Day ” + str(days_limit) + “\n”)

fout.write(“Berries: ” + str(len(berries_list)) + “\n”)
fout.write(“Rats: ” + str(len(rats_list)) + “\n”)
fout.write(“Cats: ” + str(len(cats_list)) + “\n”)
fout.close()