This repository has been archived on 2025-12-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LUT-yliopisto/BM40A1500 Data Structures and Algorithms/Assignments/Week 8/sums_DP.py
2023-01-08 18:43:01 +02:00

33 lines
1.1 KiB
Python

def sums(arr):
# Create a set to store all the unique sums
sums = set()
# Initialize a list with length equal to the length of arr
# to store the previous results
prev_results = [0] * len(arr)
# Iterate through each element in arr
for i in range(len(arr)):
# Initialize a temporary set to store the new sums
temp = set()
# Iterate through all the previous results
for j in range(i):
# Add the current element to each of the previous sums
# and add the new sum to the set
temp.add(arr[i] + prev_results[j])
# Add the current element to the set
temp.add(arr[i])
# Update the list of previous results with the new sums
prev_results[i] = temp
# Update the set of all sums with the new sums
sums = sums.union(temp)
return len(sums)
# Test
if __name__ == "__main__":
print(sums([1, 2, 3])) # 6
print(sums([2, 2, 3])) # 5
print(sums([1, 3, 5, 1, 3, 5])) # 18
print(sums([1, 15, 5, 23, 100, 55, 2])) # 121