Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to sqeeze list elements from left or right to make it single element in Python
Suppose we have a list of numbers called nums, we have to squeeze it from both the left and the right until there is one remaining element. We will return the states at each step.
So, if the input is like nums = [10,20,30,40,50,60], then the output will be ?
[ [10, 20, 30, 40, 50, 60], [30, 30, 40, 110], [60, 150], [210] ]
Algorithm
To solve this, we will follow these steps ?
- ret := a list with only one element nums
- while size of nums > 1, do
- if size of nums is same as 2, then
- nums := make a list with element (nums[0] + nums[1])
- otherwise when size of nums is same as 3, then
- nums := make a list with element (nums[0] + nums[1] + nums[2])
- otherwise,
- nums := make a list with elements (nums[0] + nums[1]) then insert another list from nums from index 2 to third last element of nums then add another list with element (second last element of nums + last element of nums)
- insert nums at the end of ret
- if size of nums is same as 2, then
- return ret
Example
Let us see the following implementation to get better understanding ?
class Solution:
def solve(self, nums):
ret = [nums]
while len(nums) > 1:
if len(nums) == 2:
nums = [nums[0] + nums[1]]
elif len(nums) == 3:
nums = [nums[0] + nums[1] + nums[2]]
else:
nums = [nums[0] + nums[1]] + nums[2:-2] + [nums[-2] + nums[-1]]
ret.append(nums)
return ret
ob = Solution()
print(ob.solve([10, 20, 30, 40, 50, 60]))
[[10, 20, 30, 40, 50, 60], [30, 30, 40, 110], [60, 150], [210]]
How It Works
The algorithm squeezes the list by combining adjacent elements from both ends ?
- Step 1: [10, 20, 30, 40, 50, 60] ? Combine first two (10+20=30) and last two (50+60=110)
- Step 2: [30, 30, 40, 110] ? Combine first two (30+30=60) and last two (40+110=150)
- Step 3: [60, 150] ? Combine remaining two (60+150=210)
- Step 4: [210] ? Single element remaining
Alternative Implementation
Here's a more readable version with detailed steps ?
def squeeze_list(nums):
result = [nums[:]] # Start with original list
while len(nums) > 1:
if len(nums) == 2:
# Only two elements left, combine them
nums = [nums[0] + nums[1]]
elif len(nums) == 3:
# Three elements left, combine all
nums = [sum(nums)]
else:
# More than three elements, squeeze from both ends
left_sum = nums[0] + nums[1]
right_sum = nums[-2] + nums[-1]
middle = nums[2:-2]
nums = [left_sum] + middle + [right_sum]
result.append(nums[:])
return result
# Test the function
nums = [10, 20, 30, 40, 50, 60]
print("Squeezing steps:")
for i, step in enumerate(squeeze_list(nums)):
print(f"Step {i}: {step}")
Squeezing steps: Step 0: [10, 20, 30, 40, 50, 60] Step 1: [30, 30, 40, 110] Step 2: [60, 150] Step 3: [210]
Conclusion
The squeeze algorithm iteratively combines elements from both ends of the list until only one element remains. Each iteration reduces the list size by combining adjacent pairs from the left and right sides, preserving all intermediate states.
