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:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
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.
Python: Tips of the Day
Executes the provided function once for each list element, starting from the list's last element:
Example:
def tips_for_each_right(itr, fn): for el in itr[::-1]: fn(el) tips_for_each_right([3, 6, 9], print)
Output:
9 6 3
- Weekly Trends
- 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
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook