Class: Mutants::Dna
- Inherits:
-
Object
- Object
- Mutants::Dna
- Defined in:
- lib/mutants/dna.rb
Instance Attribute Summary collapse
-
#counter ⇒ Object
readonly
Returns the value of attribute counter.
-
#last_character ⇒ Object
readonly
Returns the value of attribute last_character.
-
#sequence ⇒ Object
readonly
Returns the value of attribute sequence.
Instance Method Summary collapse
- #bottom_diagonals_from_left_read_without_main_diagonal(sequence) ⇒ Object
- #bottom_diagonals_from_right_read_without_main_diagonal(sequence) ⇒ Object
- #horizontal(sequence) ⇒ Object
-
#initialize ⇒ Dna
constructor
A new instance of Dna.
- #read_diagonals(read_direction, read_type, base_n, base_m) ⇒ Object
- #read_horizontal_or_vertical(direction, initial_character, index) ⇒ Object
- #top_diagonals_from_left_read_with_main_diagonal(sequence) ⇒ Object
- #top_diagonals_from_right_read_with_main_diagonal(sequence) ⇒ Object
- #vertical(sequence) ⇒ Object
Constructor Details
#initialize ⇒ Dna
Returns a new instance of Dna.
7 8 9 |
# File 'lib/mutants/dna.rb', line 7 def initialize @counter = 0 end |
Instance Attribute Details
#counter ⇒ Object (readonly)
Returns the value of attribute counter.
5 6 7 |
# File 'lib/mutants/dna.rb', line 5 def counter @counter end |
#last_character ⇒ Object (readonly)
Returns the value of attribute last_character.
5 6 7 |
# File 'lib/mutants/dna.rb', line 5 def last_character @last_character end |
#sequence ⇒ Object (readonly)
Returns the value of attribute sequence.
5 6 7 |
# File 'lib/mutants/dna.rb', line 5 def sequence @sequence end |
Instance Method Details
#bottom_diagonals_from_left_read_without_main_diagonal(sequence) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mutants/dna.rb', line 45 def bottom_diagonals_from_left_read_without_main_diagonal(sequence) @sequence = sequence row = 1 loop do break unless row < sequence.length @last_character = sequence[row][0] return true if read_diagonals(Mutants::Orientation::FROM_LEFT, Mutants::Orientation::BELOW_MAIN_DIAGONAL, row, 0) row += 1 end false end |
#bottom_diagonals_from_right_read_without_main_diagonal(sequence) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mutants/dna.rb', line 63 def bottom_diagonals_from_right_read_without_main_diagonal(sequence) @sequence = sequence row = 1 loop do break unless row < sequence.length @last_character = sequence[row][sequence.length - 1] return true if read_diagonals(Mutants::Orientation::FROM_RIGHT, Mutants::Orientation::BELOW_MAIN_DIAGONAL, row, sequence.length - 1) row += 1 end false end |
#horizontal(sequence) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/mutants/dna.rb', line 11 def horizontal(sequence) @sequence = sequence row = 0 loop do break unless row < sequence.length @last_character = sequence[row][0] return true if read_horizontal_or_vertical(Mutants::Orientation::HORIZONTAL, last_character, row) row += 1 end false end |
#read_diagonals(read_direction, read_type, base_n, base_m) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/mutants/dna.rb', line 147 def read_diagonals(read_direction, read_type, base_n, base_m) offset = 1 characters_count = 1 last_character = sequence[base_n][base_m] current_character = nil # This condition is valid when you can still read below the main diagonal bottom_read_condition = base_n + offset < sequence.length # This condition is valid when you can still read from the main diagonal to the top. top_read_condition = (read_direction.eql?(Mutants::Orientation::FROM_LEFT) && base_m + offset < sequence.length || read_direction.eql?(Mutants::Orientation::FROM_RIGHT) && base_m - offset >= 0) # From the verification of the previous conditions it decides if it is correct to continue reading the diagonal. while (read_type.eql?(Mutants::Orientation::ABOVE_MAIN_DIAGONAL_INCLUDING) && top_read_condition) || (read_type.eql?(Mutants::Orientation::BELOW_MAIN_DIAGONAL) && bottom_read_condition) current_character = if read_direction.eql?(Mutants::Orientation::FROM_LEFT) sequence[base_n + offset][base_m + offset] else sequence[base_n + offset][base_m - offset] end if last_character == current_character characters_count += 1 if characters_count == Mutants::Configuration::COUNTER_SEQUENCES @counter += 1 characters_count = 0 # If a find the minimum sequences to determine if it is Mutant, I finish the search. return true if counter >= Mutants::Configuration::MIN_SEQUENCES_TO_BE_MUTANT end else last_character = current_character characters_count = 1 end offset += 1 # I will verify the read conditions. bottom_read_condition = base_n + offset < sequence.length top_read_condition = (read_direction.eql?(Mutants::Orientation::FROM_LEFT) && base_m + offset < sequence.length || read_direction.eql?(Mutants::Orientation::FROM_RIGHT) && base_m - offset >= 0) end # The subject is Human false end |
#read_horizontal_or_vertical(direction, initial_character, index) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mutants/dna.rb', line 117 def read_horizontal_or_vertical(direction, initial_character, index) characters_count = 1 last_character = initial_character current_character = nil subindex = 1 while sequence.length - subindex + characters_count >= 4 && subindex < sequence.length current_character = if Mutants::Orientation::HORIZONTAL == direction sequence[index][subindex] else sequence[subindex][index] end if last_character == current_character characters_count += 1 if characters_count == Mutants::Configuration::COUNTER_SEQUENCES @counter += 1 characters_count = 0 if counter >= Mutants::Configuration::MIN_SEQUENCES_TO_BE_MUTANT # If a find the minimum sequences to determine if it is Mutant, I finish the search. return true end end else last_character = current_character characters_count = 1 end subindex += 1 end false end |
#top_diagonals_from_left_read_with_main_diagonal(sequence) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mutants/dna.rb', line 81 def top_diagonals_from_left_read_with_main_diagonal(sequence) @sequence = sequence col = 0 loop do break unless col < sequence.length @last_character = sequence[0][col] return true if read_diagonals(Mutants::Orientation::FROM_LEFT, Mutants::Orientation::ABOVE_MAIN_DIAGONAL_INCLUDING, 0, col) col += 1 end false end |
#top_diagonals_from_right_read_with_main_diagonal(sequence) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mutants/dna.rb', line 99 def top_diagonals_from_right_read_with_main_diagonal(sequence) @sequence = sequence col = 1 loop do break unless col < sequence.length @last_character = sequence[0][sequence.length - col] return true if read_diagonals(Mutants::Orientation::FROM_RIGHT, Mutants::Orientation::ABOVE_MAIN_DIAGONAL_INCLUDING, 0, sequence.length - col) col += 1 end false end |
#vertical(sequence) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mutants/dna.rb', line 28 def vertical(sequence) @sequence = sequence col = 0 loop do break unless col < sequence.length @last_character = sequence[0][col] return true if read_horizontal_or_vertical(Mutants::Orientation::VERTICAL, last_character, col) col += 1 end false end |