196k views
1 vote
Write a function called hasadjacentrepeat() that uses loops to determine if any two adjacent numbers in an array are the same. The input argument is an integer precision array called inarray. The output argument is an integer scalar called adjacentrepeat. This value is set to an integer value of 1 if two adjacent values in the array have the same value. Otherwise, this value is set to 0

User CoreyRS
by
8.6k points

1 Answer

2 votes

Answer:

def hasadjacentrepeat(inarray):

# Initialize a variable to 0 to indicate that there are no adjacent repeated numbers

adjacentrepeat = 0

# Loop through the input array, except for the last element

for i in range(len(inarray)-1):

# Check if the current element is equal to the next element

if inarray[i] == inarray[i+1]:

# If they are equal, set the adjacentrepeat variable to 1 and break out of the loop

adjacentrepeat = 1

break

# Return the value of the adjacentrepeat variable, which is 1 if there are adjacent repeated numbers, 0 otherwise

return adjacentrepeat

Example:

a = [1, 2, 3, 4, 5, 5, 6]

b = [1, 2, 3, 4, 5, 6, 7]

print(hasadjacentrepeat(a)) # Output: 1

print(hasadjacentrepeat(b)) # Output: 0

User Envio
by
8.8k points

Related questions