SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
0.lists
Exercise:
Programming Excercises
Question:27 | ISBN:978013274719 | Edition: 6

Question

(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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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.

 

0 0

Discussions

Post the discussion to improve the above solution.