w3resource

Python: Check the length and the fifth element occurs twice in a list

Python Programming Puzzles: Exercise-2 with Solution

Write a Python program that accepts a list of integers and calculates the length and the fifth element. Return true if the length of the list is 8 and the fifth element occurs thrice in the said list.

Input:
[19, 19, 15, 5, 5, 5, 1, 2]
Output:
True

Input:
[19, 15, 5, 7, 5, 5, 2]
Output:
False

Input:
[11, 12, 14, 13, 14, 13, 15, 14]
Output:
True

Input:
[19, 15, 11, 7, 5, 6, 2]
Output:
False

Visual Presentation:

Python: Check the length and the fifth element occurs twice in a list.
Python: Check the length and the fifth element occurs twice in a list.
Python: Check the length and the fifth element occurs twice in a list.

Sample Solution:

Python Code:

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

# Define a function named 'test' that takes a list 'nums' as input
def test(nums):
    # Check if the length of 'nums' is 8 and the count of the fifth element in 'nums' is equal to 3
    return len(nums) == 8 and nums.count(nums[4]) == 3

# Create a list 'nums' with specific elements
nums = [19, 19, 15, 5, 5, 5, 1, 2]

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

# Print the result of the test function applied to the 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))

# Create a different list 'nums' with specific elements
nums = [19, 15, 5, 7, 5, 5, 2]

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

# Print the result of the test function applied to the modified 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))

# Create another list 'nums' with specific elements
nums = [11, 12, 14, 13, 14, 13, 15, 14]

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

# Print the result of the test function applied to the modified 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))

# Create one more list 'nums' with specific elements
nums = [19, 15, 11, 7, 5, 6, 2]

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

# Print the result of the test function applied to the modified 'nums' list
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))

Sample Output:

Original list:
[19, 19, 15, 5, 5, 5, 1, 2]
Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :
True

Original list:
[19, 15, 5, 7, 5, 5, 2]
Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :
False

Original list:
[11, 12, 14, 13, 14, 13, 15, 14]
Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :
True

Original list:
[19, 15, 11, 7, 5, 6, 2]
Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :
False

Flowchart:

Flowchart: Python - Check the length and the fifth element occurs twice in a list.

Python Code Editor :

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

Previous: List of integers with exactly two occurrences of nineteen and at least three occurrences of five.
Next: Whether an integer greater than 4^4 which is 4 mod 34.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/puzzles/python-programming-puzzles-2.php