w3resource

Python: List integers containing exactly three distinct values, such that no integer repeats twice consecutively


Four Distinct Values Non-Consecutive

Write a Python program to find a list of integers containing exactly four distinct values, such that no integer repeats twice consecutively among the first twenty entries.

Note: The list needs to have length greater than ten.)

Input:
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Output:
True

Input:
[1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3]
Output:
False

Input:
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
Output:
False

Visual Presentation:

Python: List integers containing exactly three distinct values, such that no integer repeats twice consecutively.
Python: List integers containing exactly three distinct values, such that no integer repeats twice consecutively.
Python: List integers containing exactly three distinct values, such that no integer repeats twice consecutively.

Sample Solution:

Python Code:

# License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a list of integers 'nums' as input
def test(nums):
    # Check if no integer in 'nums' repeats consecutively and if there are exactly four distinct values in 'nums'
    return all([nums[i] != nums[i + 1] for i in range(len(nums) - 1)]) and len(set(nums)) == 4

# Create a list of integers 'nums' with specific elements
nums = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]

# Print the original list
print("Original list:")
print(nums)

# Print a message indicating the condition being checked on the list
print("Check said list of integers containing exactly four distinct values, such that no integer repeats twice consecutively:")

# Print the result of the test function applied to the 'nums' list
print(test(nums))

# Create a different list of integers 'nums' with specific elements
nums = [1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3]

# Print the original list
print("\nOriginal list:")
print(nums)

# Print a message indicating the condition being checked on the list
print("Check said list of integers containing exactly four distinct values, such that no integer repeats twice consecutively:")

# Print the result of the test function applied to the modified 'nums' list
print(test(nums))

# Create another list of integers 'nums' with specific elements
nums = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

# Print the original list
print("\nOriginal list:")
print(nums)

# Print a message indicating the condition being checked on the list
print("Check said list of integers containing exactly four distinct values, such that no integer repeats twice consecutively:")

# Print the result of the test function applied to the modified 'nums' list
print(test(nums))

Sample Output:

Original list:
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Check said list of integers containing exactly four distinct values, such that no integer repeats  twice consecutively:
True

Original list:
[1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3, 1, 2, 3, 3]
Check said list of integers containing exactly four distinct values, such that no integer repeats  twice consecutively:
False

Original list:
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
Check said list of integers containing exactly four distinct values, such that no integer repeats  twice consecutively:
False

Flowchart:

Flowchart: Python - List integers containing exactly three distinct values, such that no integer repeats twice consecutively.

For more Practice: Solve these Related Problems:

  • Write a Python program to verify that a list contains exactly four distinct values and that no value is repeated consecutively.
  • Write a Python program to count unique elements in a list and check that none appear twice in a row within the first twenty entries.
  • Write a Python program to iterate over a list and ensure it has exactly four unique numbers and that no adjacent pair are identical.
  • Write a Python program to use slicing and set operations to confirm that the first twenty elements of a list have four distinct values with no consecutive duplicates.

Go to:


Previous: Split a string of words separated by commas and spaces into 2 lists: words and separators.
Next: Separate Parentheses Groups Perfectly.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.