Python Exercises: Divide a list of integers with the same sum value
Python List: Exercise - 273 with Solution
Write a Python program to find an element that divides a given list of integers with the same sum value.
Sample Solution-1:
Python Code:
def test(nums):
for i, x in enumerate(nums[1:-1]):
if sum(nums[:i+1]) == sum(nums[i+2:]):
return nums[i+1]
return "No such element!"
nums = [0, 9, 2, 4, 5, 6]
print("Original list:")
print(nums)
print("Element that devides the said list of integers with the same sum value:\n", test(nums))
nums = [-4, 0, 6, 1, 0, 2]
print("\nOriginal list:")
print(nums)
print("Element that devides the said list of integers with the same sum value:\n", test(nums))
nums = [1, 2, 3, 4]
print("\nOriginal list:")
print(nums)
print("Element that devides the said list of integers with the same sum value:\n", test(nums))
nums = [-4, 0, 5, 1, 0, 1]
print("\nOriginal list:")
print(nums)
print("Element that devides the said list of integers with the same sum value:\n", test(nums))
Sample Output:
Original list: [0, 9, 2, 4, 5, 6] Element that devides the said list of integers with the same sum value: 4 Original list: [-4, 0, 6, 1, 0, 2] Element that devides the said list of integers with the same sum value: 1 Original list: [1, 2, 3, 4] Element that devides the said list of integers with the same sum value: No such element! Original list: [-4, 0, 5, 1, 0, 1] Element that devides the said list of integers with the same sum value: 1
Flowchart:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Sample Solution-2:
Python Code:
def test(nums):
for i in range(len(nums)):
if sum(nums[0:i+1]) == sum(nums[i:]):
return nums[i]
return "No such element!"
nums = [0, 9, 2, 4, 5, 6]
print("Original list:")
print(nums)
print("Element that devides the said list of integers with the same sum value:\n", test(nums))
nums = [-4, 0, 6, 1, 0, 2]
print("\nOriginal list:")
print(nums)
print("Element that devides the said list of integers with the same sum value:\n", test(nums))
nums = [1, 2, 3, 4]
print("\nOriginal list:")
print(nums)
print("Element that devides the said list of integers with the same sum value:\n", test(nums))
nums = [-4, 0, 5, 1, 0, 1]
print("\nOriginal list:")
print(nums)
print("Element that devides the said list of integers with the same sum value:\n", test(nums))
Sample Output:
Original list: [0, 9, 2, 4, 5, 6] Element that devides the said list of integers with the same sum value: 4 Original list: [-4, 0, 6, 1, 0, 2] Element that devides the said list of integers with the same sum value: 1 Original list: [1, 2, 3, 4] Element that devides the said list of integers with the same sum value: No such element! Original list: [-4, 0, 5, 1, 0, 1] Element that devides the said list of integers with the same sum value: 1
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 Python Exercise: Generates a list of numbers in the arithmetic progression within a range.
Next Python Exercise: Count lowercase letters in a list of words.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Returns True if the provided function returns True for every element in the list, False otherwise:
Example:
def tips_every(lst, fn=lambda x: x): return all(map(fn, lst)) print(tips_every([2, 4, 3], lambda x: x > 1)) print(tips_every([1, 2, 3]))
Output:
True True
- 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