Advertisement
Bad_Programist

Untitled

Mar 2nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import numpy as np
  2. from random import randint
  3.  
  4.  
  5. class Snake(object):
  6.     def __init__(self, genotype, gender, population):
  7.         assert type(genotype) == np.ndarray and len(genotype) == 4,"Invalid genotype. It must be np.array with 4 elements"
  8.         assert isinstance(population, int), "Population must be integer"
  9.         check = (genotype >= 0) & (genotype <= 10)
  10.         assert population > 0, "Invalid population. It must be > 0"
  11.         assert check.all(), "Invalid genotype values. It must be between 0 and 10"
  12.         assert gender in ['Male', 'Female'], "Invalid gender. Gender should be 'Male' or 'Female'"
  13.         self.genotype = genotype
  14.         self.gender = gender
  15.         self.population = population
  16.    
  17.     def __repr__(self):
  18.         return f'Snake({self.genotype} x {self.population})'
  19.    
  20.     def __str__(self):
  21.         text = f'{self.population} Snakes of genotype {self.genotype}. Closest to the {self.guess_breed()}.'
  22.         return text
  23.    
  24.     def __add__(self, second):
  25.         if isinstance(second, Snake):
  26.             assert self.gender != second.gender, "Error, you can't crossbreed the same genders"
  27.             genom = np.array((self.genotype + second.genotype) / 2, dtype=int)
  28.             return Snake(genom, ['Male', 'Female'][randint(0, 1)], self.population + second.population)
  29.         elif isinstance(second, int):
  30.             assert second >= 0, "Use '-' for this operation"
  31.             self.population += second
  32.             return self
  33.    
  34.     def __sub__(self, num):
  35.         assert isinstance(num, int) and num < self.population, "Error. Population <= 0"
  36.         self.population -= num
  37.         return self
  38.    
  39.     def __mul__(self, num):
  40.         assert isinstance(num, int) and num >= 1, "You can only multiply Snake by integer"
  41.         self.population *= num
  42.         return self
  43.    
  44.     def __eq__(self, second):
  45.         assert isinstance(second, Snake), "Error. One of parameters isn't a Snake"
  46.         return list(self.genotype) == list(second.genotype) and self.gender == second.gender
  47.    
  48.     def __ne__(self, second):
  49.         assert isinstance(second, Snake), "Error. One of parameters isn't a Snake"
  50.         return not (self == second)
  51.    
  52.     def guess_breed(self):
  53.         Samples = np.array([[9, 2, 8, 6], [1, 10, 5, 4], [0, 8, 6, 4], [0, 5, 3, 2]])
  54.         types = {0: 'Cobra', 1: 'Anaconda', 2: 'Python', 3: 'Boa'}
  55.         diff = abs(Samples - self.genotype)
  56.         res = sorted(np.arange(4), key=lambda x: sum(diff[x]))[0]
  57.         return types[res]
  58.    
  59. # Samples:
  60. # Pure_Cobra = [9, 2, 8, 6]
  61. # Pure_Anaconda = [1, 10, 5, 4]
  62. # Pure_Python = [0, 8, 6, 4]
  63. # Pure_Boa = [0, 5, 3, 2]
  64.  
  65. New_Snake = Snake(np.array([0, 7, 2, 4]), 'Male', 1)
  66. An_Snake = Snake(np.array([9, 2, 8, 6]), 'Female', 4)
  67. N_Snake = Snake(np.array([0, 7, 6, 4]), 'Female', 6)
  68. print(An_Snake + New_Snake)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement