Sum of consecutive data in List - Python - python-3.x

I have a long list of number of which a sample look something like shown below:
L = [0, 1, 2, 0, 0, 2, 3, 1, 0, 10, 0, 2, 4, 5, 0, 0, 7, 8, 9, 0, ...]
singleData = []
sumofTwo = []
sumofThree = []
sumofFour = []
.
.
What I want to be able to do is categorize a data OR sum of two or more consecutive data into the respective lists based on the COUNT of numbers involved in the sum operation. So, if there is an occurrence of zero between two numbers, then their sum would not be considered.
For example, if I take the list above, the sum of 1 and 2 is to be added to the list sumofTwo. Similarly, if it is the sum of three consecutive numbers without the occurrence of 0's then the sum is expected to be in the sumofThree list (sum of 2,3,1; 2,4,5 and 7, 8, 9). If a number occurs between two or more 0's then it is to be appended to the singleData list (eg. 10).
How can i achieve this considering that in the list(L) there can be a sum of random consecutive numbers? Example, sum of 6 or 7 or 8 or any consecutive numbers?
I was able to segregate only a single number between 0's and the sum of two numbers. Following is the code:
for i in range(len(l)):
try:
if i == 0:
if l[i] == 0:
continue
elif l[i] != 0 and l[i+1] == 0:
singleData.append(l[i])
elif l[i] != 0 and l[i+1] != 0:
sumofTwo.append(l[i]+l[i+1])
elif i == len(l)-1:
if l[i] != 0 and l[i-1] == 0:
singleData.append(l[i])
else:
if l[i] != 0:
if l[i+1] == 0 and l[i-1] == 0:
singleData.append(l[i])
elif l[i+1] != 0:
sumofTwo.append(l[i]+l[i+1])
except IndexError:
print("Index out of range")
I realized that my code will only get messier with more cases of the sum of consecutive numbers and ultimately end up with error.
Can anybody help me out? Thanks in advance :))))

I would recommend using a dictionary to store the results:
L = [0, 1, 2, 0, 0, 2, 3, 1, 0, 10, 0, 2, 4, 5, 0, 0, 7, 8, 9, 0]
consecutive_sums = {} # Create an empty dictionary
I would also recommend directly iterating through the list, rather than using range(len(L). e.g.
for number in L:
print(number)
Then, you can just create a counter variable to check how long the current sequence is, and reset the counter when you get to a zero.
L = [0, 1, 2, 0, 0, 2, 3, 1, 0, 10, 0, 2, 4, 5, 0, 0, 7, 8, 9, 0]
consecutive_sums = {} # Create an empty dictionary
counter = 0
sum = 0
for number in L:
if number == 0: # Save the current sum and reset counter
# Check if we've already had a sequence of this length.
# If so, we have already added a list, so can append to it.
if counter in consecutive_sums:
consecutive_sums[counter].append(sum)
else: # If this is the first time we've had this length sequence
# we need to create a new key value pair in the dictionary.
# Note that the value is a list containing `sum`.
# Make sure to use a list so that you can append to it later.
consecutive_sums[counter] = [sum]
# Reset counter and sum
counter = 0
sum = 0
else: # Increment counter
counter += 1
sum += number
print(consecutive_sums)
Note that this code will not sort the dictionary keys, so the sums of sequences of length 1 may not appear at the beginning. But you can access it using consecutive_sums[1].
Also note that this version of the code also counts sequences of length 0. I suspect this is not what you want, but I'll let you figure out how to fix it!
Output:
{0: [0, 0, 0], 2: [3], 3: [6, 11, 24], 1: [10]}
EDIT:
I've intentionally tried to solve this using just builtin functions and datatypes. But if you really want to be fancy, you can use collections.defaultdict.
Below is an alternative version which uses defaultdict:
from collections import defaultdict
L = [0, 1, 2, 0, 0, 2, 3, 1, 0, 10, 0, 2, 4, 5, 0, 0, 7, 8, 9, 0]
consecutive_sums = defaultdict(list) # Create an empty defaultdict of type list
counter = 0
sum = 0
for number in L:
if number == 0: # Save the current sum and reset counter
# The magic of defaultdict:
# We don't need to check if the key exists.
# If it doesn't exist yet, defaultdict will automatically make
# an empty list for us to append to!
consecutive_sums[counter].append(sum)
# Reset counter and sum
counter = 0
sum = 0
else: # Increment counter
counter += 1
sum += number
print(consecutive_sums)

The first thing I would do is to organize a bit better our output lists, so that each can be accessed in the same way using the number of consecutive numbers. As #daviewales suggested, you could do this with a dictionary with lists as values, something like sums = {}, so that sums[1] would be the same as singleData, sums[2] the same as sumofTwo, and so on. That way, you will avoid a lot of if to know in what list you should put your data, you'll just need to use stuff like sums[nbOfValuesInSum].
Second thing, you could write a function that detects your sequences of non-zero values. A possibility would be a function that takes a list and a start index, and returns the start and end indexes of the next "interesting" sequence. It would look like this :
def findNextSequence(l, start):
while l[start] == 0:
if start == len(l)-1:
return None # there is no non-zero value left
start+=1
# when we exit the loop, start is the index of the first non-zero value
end = start + 1
while l[end] != 0:
if end == len(l)-1:
break
end+=1
# and now end is the index of the first zero value after the sequence
return (start, end)
Then you can call it in a loop, like this:
i = 0
while True:
bounds = findNextSequence(l, i)
if bounds is None:
break # there is no non-zero value left
seq = l[bounds[0]:bounds[1]] # get the start and end index of the sequence
if len(seq) not in sums:
sums[len(seq)] = []
sums[len(seq)].append(sum(seq)) # see? No need to explicitly check len(seq) to know what list I want
i = bounds[1] # get ready for the next iteration
if i == len(l):
break
NB : no need to pass l as a parameter of findNextSequence if it's a global variable

Related

Trying to loop through a list in python and see i can match a 1 or 0 and return the value of that index location

sample list [1, 0, 0, 1, 0, 0, 1, 0, 0, 0]
1 means occupied and 0 means unoccupied
expected output:
occupied
[0,3,6]
unoccupied:
[1,2,3,4,5,6,7,9]
def row_seats(seats):
occupied_seats =[]
vacant_seats =[]
for x in seats:
if x == 0:
vacant_seat.index(x)
elif x == 1 :
occupied_seats.index(x)
this is what i have done so far but i do not think i am approaching it properly any help would be appreciated!
You need to use enumerate to access the correct index,
def row_seats(seats):
occupied_seats =[]
vacant_seats =[]
for ind, x in enumerate(seats):
if x == 0:
vacant_seats.append(ind)
elif x == 1 :
occupied_seats.append(ind)
return vacant_seats, occupied_seats
seat_list = [1, 0, 0, 1, 0, 0, 1, 0, 0, 0]
vacant, occupied = row_seats(seat_list)
print(vacant)
print(occupied)
The original vacant_seat.index(x) was wrong because it was trying to search for x in an empty list. The correct approach appends the index to the respective lists.
Using index was also incorrect. Even if used properly (on the original - input list, with appending), you were obtaining an index of either 0 or 1. This would persistently return the index of only the first occurrence of those values.

tribonacci sequence python code skips for loop within while loop

I wanted to use a for loop within a while loop to add up the last 3 numbers of the list and generate a new number to append into the existing list. However, the code would not enter the for loop within the while loop and I have no clue why.
What the function is supposed to do:
Take in a list of numbers (as signature)
Total up the LAST 3 numbers in the list and produce the next number to be appended
Continue step 2 until length of list == n
#my code
def tribonacci(signature, n):
total = 0
for i in range(len(signature)):
num = signature[i]
total += num
signature.append(total)
while len(signature) < n:
for j in range(-1,-4):
num = signature[j]
total += num
signature.append(num)
return signature
#Some sample test code:
print(tribonacci([1, 1, 1], 10))
print("Correct output: " + "[1, 1, 1, 3, 5, 9, 17, 31, 57, 105]")
print(tribonacci([0, 0, 1], 10))
print("Correct output: " + "[0, 0, 1, 1, 2, 4, 7, 13, 24, 44]")
print(tribonacci([300, 200, 100], 0))
print("Correct output: " + "[]")
UPDATE!
As suggested, I resetted the total count in the while loop by creating a total_2 = 0. I've also added the -1 to the range and changed .append(num) in the while loop block to .append(total_2).
def tribonacci(signature, n):
total = 0
for i in range(len(signature)):
num = signature[i]
total += num
signature.append(total)
while len(signature) < n:
total_2 = 0
for j in range(-1,-4, -1):
num = signature[j]
total_2 += num
signature.append(total_2)
return signature
However, this code doesnt work the 3rd print test code where n = 0. One of the users shared a much shorter code which works for ALL of the test code.
Try range(-1,-4,-1). You need to tell python to go backwards.
Just for the reference, I've implemented your function with a few improvements:
def tribonacci(signature, n):
signature = signature[:n]
while len(signature) < n:
signature.append(sum(signature[-3:]))
return signature

How many times should I loop to cover all cases of possible sums?

I am trying to solve this competitive programming problem using python3. The problem asks, given an array of size n, split the array into three consecutive, contiguous parts such that the first part has maximum sum and equals the sum of the third part. The elements in the array are positive integers.
My approach:
inputN = int(input())
inputString = input()
usableString = stringToList(inputString)
counting = 0
sum1 = usableString[0]
sum3 = usableString[-1]
maxSum1 = 0
countFromLeft = 1
countFromRight = 2
while counting < inputN-1:
if sum1 > sum3:
sum3 += usableString[-countFromRight]
countFromRight += 1
elif sum3 > sum1:
sum1 += usableString[countFromLeft]
countFromLeft += 1
elif sum1 == sum3:
maxSum1 = sum1
sum1 += usableString[countFromLeft]
countFromLeft += 1
counting += 1
print(maxSum1)
We read in the array elements and store them in a list usableString.
We set two variables sum1 and sum3 to the first and last elements of the list respectively.
We set a variable to keep track of the maximum sum of the first part of the list.
Finally, we set a variable counting to 0 which will represent the number of elements we have added from the list into sum1 or sum3.
The rest of the logic is in the while loop, which just checks if sum1 is larger than sum3 or the other way around and otherwise if they equal. After each iteration we add 1 to counting as an extra element has been included in a part. The while loop should stop when the number of elements used (i.e counting) is equal to the number of elements in the array - 1, since we added the first and last elements before entering the loop, which makes (array - 2), however, we still need to loop one additional time to check if sum1 and sum3 are equal.
I checked your submitted algorithm, and the problem is your stringToList function:
def stringToList(s):
list=[]
for elem in s:
if elem != " ":
list.append(int(elem))
return list
As far as I can tell, your main algorithm is completely fine, but stringToList does one crucial thing incorrectly:
>>> stringToList('2 4 6 8 10')
[2, 4, 6, 8, 1, 0]
# should be
[2, 4, 6, 8, 10]
As it treats each character individually, the two digits of 10 are turned into 1, 0. A simpler method which performs correctly would be to do the following:
# explanation
>>> input()
'2 4 6 8 10'
>>> input().split(' ')
['2', '4', '6', '8', '10']
>>> map(int, input().split(' ')) # applies the int function to all elements
<map object at 0x...>
>>> list(map(int, input().split(' '))) # converts map object into list
[2, 4, 6, 8, 10]
Sorry it took so long, I ended up making my own algorithm to compare to yours, ran my own tests, and then ran your code with the input to list method I just explained, and figured the only difference was your stringToList function. Took a while, but I hope it helps!
Just for the fun, here's my algorithm and turns out it was pretty similar to yours!
array = [1, 3, 2, 1, 4]
n = len(array)
slice = [0, n]
sum = [array[0], 0]
bestSum = 0
while slice[0] < slice[1]-1:
i = 0 if (sum[0] < sum[1]) else 1
slice[i] += 1-(2*i)
sum[i] += array[slice[i]]
if sum[0] == sum[1]: bestSum = sum[0]
# print(array[ : slice[0]+1], array[slice[0]+1 : slice[1]], array[slice[1] : ])
print(bestSum)

how to separate and sort numbers in python?

Given a variable containing integer numbers with the provisions of the number 0 (zero) in the variable is a separator between one number with another number. The numbers will be separated and ordered by the numbers in the numbers themselves. After that, the numbers from the sort will be rejoined without separating with the output in the form of integer numbers. Make a method / function that accepts parameters only a series of numbers and produces output like the description above.
def sort(nums):
for i in range (15):
minpos = 1
for j in range (i,6):
if nums[j] < nums[minpos]:
minpos = j
temp = nums[i]
nums[i] = nums[minpos]
nums[minpos] = temp
nums = [5,9,5,6,5,6,0,1,5,9,4,6,6,0,5,6]
sort(nums)
print(nums)
The output should be 55566914566956 , but actual output is [5, 5, 5, 5, 6, 6, 9, 0, 1, 5, 9, 4, 6, 6, 0, 6]
You can try the following function:
def sort(nums):
curr = []
ret = ''
for i in nums:
if i == 0:
ret += ''.join(map(str, sorted(curr)))
curr = []
else:
curr.append(i)
ret += ''.join(map(str, sorted(curr)))
return ret
print(sort([5,9,5,6,5,6,0,1,5,9,4,6,6,0,5,6]))
Prints:
55566914566956
def sort(nums):
result = list()
sub_array = list()
for i in nums:
if i == 0:
# sort sub array and append it to result
sub_array.sort()
result.extend(sub_array)
sub_array = list()
else:
sub_array.append(i)
# ensure that the last sub_array is also treated
sub_array.sort()
result.extend(sub_array)
return result
nums = [5,9,5,6,5,6,0,1,5,9,4,6,6,0,5,6]
result = sort(nums)
print(result)
The easiest way is to loop through your list and create a sub array of numbers up until you find a '0'. At this point, simply order your sub array, append it to your result list and continue until the end of your input string.

How do I replace an element in a list?

I'm not really sure how to properly word my question but I will try my best.
I'm working on a class project and one part is using lists.
So first off I set random values in a list. a = [1,1,1,0,0,0,0]
To make this clearer let's say I'm making a program to manage the seating arrangement in a cinema. 1 is for taken and 0 is for vacant. So this means that there are only 4 vacant seats remaining. A customer uses the program and the program chooses the seat for the customer, so the first zero is chosen and now there are only three vacant seats left. Another customer uses the program and the same thing happens until all the seats are taken which will then print a statement that says "All seats are taken"
How do I pull this off?
I hope I was able to word this question properly
Thanks in advance!
Here's what I have tried. (and absolutely failed lol):
a = [1, 1, 1, 0, 0, 0, 0]
for i in a:
if i ==0:
a[i] = 1
Edit:
while True:
seats = [1,1,1,0,0,0]
for i, seat in enumerate(seats):
if seat == 0:
seats[i] = 1
print("Your seat is at", i)
break
else:
print("No more seats!")
user = int(input("Press 1 to reserve another seat, 2 to quit the program"))
if user == 2:
break
Guessing from the type of question, some of this might be new to you. However, I believe it's always nice to see different ways to do things, and you can always look stuff up and learn something new!
You'll need to check the list for zeros, and if one is found, switch it with a 1. Importantly, you should then stop (break) looking, as you don't want to fill all seats at once.
Something like this is a possible way to write that up (again, many ways to do this, this is just one). You would still need to repeat this process however many times you want to seat someone.
for i, seat in enumerate(seats):
if seat == 0:
seats[i] = 1
print("Your seat is at", i)
break # Stop looking for seats.
else: # If no break happened.
print("No more seats!")
a[a.index(0)] = 1 will replace the first zero found with a one, and throw an exception if there are no more zeros. You can catch the exception:
try:
a[a.index(0)] = 1
except ValueError:
print('no more seats')
Your code snippet will set the first element to 1 if it is 0. You need a way to find the index of the first element that is 0 and just set that to 1.
Try the index function to get the index for one element and use that...or just save the total and currently occupied seat counts as numbers.
Why are you comparing i with 0, when you should be checking the array content:
a = [1, 1, 1, 0, 0, 0, 0]
for i, val in enumerate(a):
if val == 0:
a[i] = 1
a =[1,1,1,0,0,0]
for x in sorted(a):
if len(a) == sum(a):
print('seats are full ')
else:
a[x].pop
a.append(1)
print ('your seat is at a[x]')
Okay, so I have given two fixes for your problem, using both for and while loops. Personally, I believe the while loop is much more preferable in this scenario, to avoid using the break command.
def original_main():
a = [1, 1, 1, 0, 0, 0, 0]
for i in a:
# i here will be equal to the values in the list, what happens here is that the value is being copied to a new paramater.
if i == 0:
a[i] = 1
print('original_main:', a)
def fixed_main():
a = [1, 1, 1, 0, 0, 0, 0]
for index, value in enumerate(a):
# What happens here is that enumerate takes both the index and value of the element and uses them.
# You check whether the value of the seat is 0, AKA free.
if value == 0:
# You update the location of the same element/ seat to being occupied.
a[index] = 1
# To stop the loop, often preferable not to need to use breaks.
break
print('fixed_main:', a)
def alternative_main():
a = [1, 1, 1, 0, 0, 0, 0]
index = 0
# Cycle through the list until reaching the end of the list or the seat is free.
while index < len(a) and a[index] != 0:
index += 1
# Check whether there were any empty seats as if there weren't index will be higher or equal to the amount of seats.
# Thechnically it can only be equal due to the while condition but it is a better practice to cover all scenarios.
if index >= len(a):
print('No empty seats.')
else:
a[index] = 1
print('alternative_main:', a)
if __name__ == '__main__':
original_main()
print()
fixed_main()
alternative_main()
Printed result:
original_main: [1, 1, 1, 0, 0, 0, 0]
fixed_main: [1, 1, 1, 1, 0, 0, 0]
alternative_main: [1, 1, 1, 1, 0, 0, 0]

Resources