Question: Find and fix the bug/s in this Ruby solution DO NOT CHANGE MORE THAN 3 LINES OF CODE Instructions: Like I said, you can only
Find and fix the bug/s in this Ruby solution DO NOT CHANGE MORE THAN 3 LINES OF CODE
Instructions:

Like I said, you can only change up to 3 lines and nothing more, nothing less.
Here is the code that needs to be fixed

Consider N coins aligned in a row. Each coin is showing either heads or tails. The adjacency of these coins is the number of pairs of adjacent coins showing the same face. You are given an implementation of a function: def solution (a) that, given a non-empty array A consisting of N integers representing the coins, returns the maximum possible adjacency that can be obtained by reversing exactly one coin (that is, one of the coins must be reversed). Consecutive elements of array A represent consecutive coins in the row. Array A contains only Os and/or 1s: O represents a coin with heads facing up; 1 represents a coin with tails facing up. For example, given array A consisting of six numbers, such that: A[0] = 1 A[1] = 1 A[2] 0 A[3] A[4] 0 A[5] the function returns 4. The initial adjacency is 2, as there are two pairs of adjacent coins showing the same face, namely (0, 1) and (4,5). After reversing the coin represented by A[2] the adjacency equals 4, as there are four pairs of adjacent coins showing the same face, namely (0, 1), (1, 2), (2, 3) and (4,5), and it is not possible to obtain a higher adjacency. 1 0 def solution(a) n = a. length result = 0 for i in 0 .. (n - 2) if (a[i] = a[i + 1]) then result = result + 1 end end 6 9 r = 0 for i in 0 .. (n - 1) count = 0 if (i > 0) then if (ai 1] != a[i]) then count = count + 1 else count = count - 1 end end 10 11 12 13 14 15 16 17 18 19 29 21 22 23 24 25 26 27 28 29 30 31 if (i r) then r = count end end return result + r end Consider N coins aligned in a row. Each coin is showing either heads or tails. The adjacency of these coins is the number of pairs of adjacent coins showing the same face. You are given an implementation of a function: def solution (a) that, given a non-empty array A consisting of N integers representing the coins, returns the maximum possible adjacency that can be obtained by reversing exactly one coin (that is, one of the coins must be reversed). Consecutive elements of array A represent consecutive coins in the row. Array A contains only Os and/or 1s: O represents a coin with heads facing up; 1 represents a coin with tails facing up. For example, given array A consisting of six numbers, such that: A[0] = 1 A[1] = 1 A[2] 0 A[3] A[4] 0 A[5] the function returns 4. The initial adjacency is 2, as there are two pairs of adjacent coins showing the same face, namely (0, 1) and (4,5). After reversing the coin represented by A[2] the adjacency equals 4, as there are four pairs of adjacent coins showing the same face, namely (0, 1), (1, 2), (2, 3) and (4,5), and it is not possible to obtain a higher adjacency. 1 0 def solution(a) n = a. length result = 0 for i in 0 .. (n - 2) if (a[i] = a[i + 1]) then result = result + 1 end end 6 9 r = 0 for i in 0 .. (n - 1) count = 0 if (i > 0) then if (ai 1] != a[i]) then count = count + 1 else count = count - 1 end end 10 11 12 13 14 15 16 17 18 19 29 21 22 23 24 25 26 27 28 29 30 31 if (i r) then r = count end end return result + r end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
