(Pattern recognition: four consecutive equal numbers) Write the following function that tests whether the list has four consecutive numbers with the same value:
def isConsecutiveFour(values):
Write a test program that prompts the user to enter a series of integers and reports whether the series contains four consecutive numbers with the same value.
Program Code:
# Function to test whether the list has four consecutive numbers with the same value
def isConsecutiveFour(values):
count = 1
# Traverse the list starting from the second element
for i in range(1, len(values)):
# Check if the current number is equal to the previous number
if values[i] == values[i - 1]:
count += 1
else:
count = 1
# Check if there are four consecutive numbers with the same value
if count == 4:
return True
# If no four consecutive numbers are found
return False
# Test program
def main():
# Prompt the user to enter a series of integers
series = input("Enter a series of integer numbers: ")
# Convert the series to a list of integers
values = list(map(int, series.split()))
# Check if the series contains four consecutive numbers with the same value
if isConsecutiveFour(values):
print("The series contains four consecutive numbers with the same value.")
else:
print("The series does not contain four consecutive numbers with the same value.")
# Call the test program
main()
Executed Output :
Enter a series of integer numbers: 10 111 12 13
The series does not contain four consecutive numbers with the same value.