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
Pictorial Presentation:



Sample Solution:
Python Code:
#License: https://bit.ly/3oLErEI
def test(nums):
return len(nums) == 8 and nums.count(nums[4]) == 3
nums = [19,19,15,5,5,5,1,2]
print("Original list:")
print(nums)
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))
nums = [19,15,5,7,5,5,2]
print("\nOriginal list:")
print(nums)
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))
nums = [11,12,14,13,14,13,15,14]
print("\nOriginal list:")
print(nums)
print("Check whether the length of the said list is 8 and fifth element occurs thrice in the said list. :")
print(test(nums))
nums = [19,15,11,7,5,6,2]
print("\nOriginal list:")
print(nums)
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:

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.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join