Move Zeroes to the end of the string - python

I was just doing some leetcode
but this blew my mind.
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
nums = [0, 1, 0, 3 ,12]
k = 0
i=0
while i < len(nums):
print(i)
if nums[i] == 0:
print('inside if=',i)
print('before',nums)
k += 1
print('removed',nums[i])
nums.remove(i)
print('after',nums)
else:
i += 1
print('no change',nums)
print('final',nums)
for j in range (k):
nums.append(0)
print(nums)
and the output is:
0
inside if= 0
before [0, 1, 0, 3, 12]
element to be removed 0
after [1, 0, 3, 12]
0
no change [1, 0, 3, 12]
->1
->inside if= 1
->before [1, 0, 3, 12]
->element to be removed 0
->after [0, 3, 12]
1
no change [0, 3, 12]
2
no change [0, 3, 12]
final [0, 3, 12]
[0, 3, 12, 0, 0]
How can it remove element 1 if it can read that it has to remove 0?

So I got to know my dumb mistake.
Actually remove() is used to remove the element that is mentioned.
Like if I type remove(6)from a list a = [1,2,3,4,5,6]. it removes element 6, not the element that is at 6th position.
so use pop() to do so.

Related

recursive calling to print list of numbers

I'm trying to sort an array and print the output recursively until the array is = [0,0,0,0,0] but it only prints [3,2,1,0,0] ,,,,, this is what i wrote can you help to fix this isuee ,, still learning
the answer should be
[4 3 2 1 0 0]
[3 2 1 0 0 0]
[2 1 0 0 0 0]
[1 0 0 0 0 0]
[0 0 0 0 0 0]
numbers=[5,4,3,2,1,1]
numbers.sort()
numbers.sort(reverse=True)
print('List sorted: ', numbers)
def list_arrays(numb):
level=numbers[0]
if len(numbers)-1 < level:
return 0
else:
numbers.pop(0);
print(numbers)
for i in range(len(numbers)):
numbers[i] -= 1
print(numbers)
#list_arrays(numbers)
if __name__=='__main__':
list_arrays(numbers)
You have a number of problems here. First, you defined a function, but you never called the function. That's why nothing printed.
Second, you don't need "sort" immediately followed by "sort(reverse)".
Third, I don't know what you were attempting by checking the level. That doesn't seem to be related to your problem.
Fourth, you're subtracting 1 from every number, but you should only be subtracting 1 if the number is greater than 0.
Finally, you're only subtracting once. You need to repeat it until all the numbers are zero.
numbers=[5,4,3,2,1,1]
numbers.sort(reverse=True)
print('List sorted: ', numbers)
def list_arrays(numb):
while any(numb):
for i in range(len(numb)):
if numb[i]:
numb[i] -= 1
print(numb)
list_arrays(numbers)
In most cases, rather than modify the list in place, I would suggest creating a new list during each loop, but for this simple assignment, this will work.
To remove the zeros, you really do want to create a new list, not modify in place.
numbers=[5,4,3,2,1,1]
numbers.sort(reverse=True)
print('List sorted: ', numbers)
def list_arrays(numb):
while numb:
new = []
for v in numb:
if v > 1:
new.append(v-1)
numb = new
print(numb)
list_arrays(numbers)
Or even:
numbers=[5,4,3,2,1,1]
numbers.sort(reverse=True)
print('List sorted: ', numbers)
def list_arrays(numb):
while numb:
numb = [v-1 for v in numb if v>1]
print(numb)
list_arrays(numbers)
In another comment you asked to remove all zeroes. Perhaps this is the solution you are looking for -
def run(t):
while(t):
yield t
t = [x - 1 for x in t if x > 1]
mylist = [5,4,3,2,1,1]
for t in run(mylist):
print(t)
[5, 4, 3, 2, 1, 1]
[4, 3, 2, 1]
[3, 2, 1]
[2, 1]
[1]
Or gather all lines in a single array using list -
mylist = [5,4,3,2,1,1]
print(list(run(mylist)))
[
[5, 4, 3, 2, 1, 1],
[4, 3, 2, 1],
[3, 2, 1],
[2, 1],
[1]
]
Maybe this is what you want?
def make_all_zero_and_print(numbers):
while numbers[0] > 0:
print(numbers)
for i in range(len(numbers)):
if numbers[i] > 0:
numbers[i] -= 1
print(numbers)
make_all_zero_and_print([5, 4, 3, 2, 1, 1])
Output:
[5, 4, 3, 2, 1, 1]
[4, 3, 2, 1, 0, 0]
[3, 2, 1, 0, 0, 0]
[2, 1, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]

I have problem with removing last item from list [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 1 year ago.
nums = [0,1,2,2,3,0,4,2]
val = 2
for i in nums:
if i == val:
nums.remove(i)
else:
continue
print(nums)
Result is [0,1,3,0,4,2] instead of [0,1,3,0,4]
Well maybe I am a little bit confused because in the title you ask for removing just the last item but in the question you ask for an expected result. I just took your code and make it work to get the expected output with a list comprehension. It looks like this:
nums_new = [i for i in nums if i != val]
You can ofcourse name nums_new to only nums.
Your mistake becomes clear if you print your data inside the loop:
nums = [0,1,2,2,3,0,4,2]
val = 2
for i in nums:
if i == val:
nums.remove(i)
print(i, nums)
print(nums)
Output:
0 [0, 1, 2, 2, 3, 0, 4, 2]
1 [0, 1, 2, 2, 3, 0, 4, 2]
2 [0, 1, 2, 3, 0, 4, 2]
3 [0, 1, 2, 3, 0, 4, 2]
0 [0, 1, 2, 3, 0, 4, 2]
4 [0, 1, 2, 3, 0, 4, 2]
2 [0, 1, 3, 0, 4, 2]
[0, 1, 3, 0, 4, 2]
Note:
i goes through 0, 1, 2, 3, ..., i.e., not two 2s in a row. That's because you remove the first 2, but "the loop" doesn't care, it still goes to the next position in the list, which then holds the value 3.
You do find the 2 at the end, but since remove removes the first occurrence, it removes the earlier 2.
You have a few options:
Filter
your_list = [1, 2, 3, 4, 2, 3, 4, 2]
filtered_values = filter(lambda value: value != 2, your_list)
The first argument is the filtering function, the second is your values - whatever evaluates to False is removed from the list.
List comprehension
your_list = [1, 2, 3, 4, 2, 3, 4, 2]
filtered_values = [value for value in your_list if value != 2]
There are many other ways of doing it, although those are the most common ways of doing it.
You can also do this with the NumPy module:
import numpy as np
# generate 20 random integers between 0 (inclusive) and 5 (exclusive)
myarr = np.random.randint(0, 5, 20)
print(myarr)
[0 2 0 1 2 0 3 0 0 0 3 4 1 3 1 4 1 0 4 4]
val = 2
myarr = np.delete(myarr, np.where(myarr == val))
print(myarr)
[0 0 1 0 3 0 0 0 3 4 1 3 1 4 1 0 4 4]
myarr = np.delete(myarr, np.where(myarr == val))
In that line we do the following: from the array myarr, delete the elements whenever that element equals val.
The same can be achieved with your initial list, by first converting it to a numpy array:
nums = np.array(nums)

Finding the number of substrings which sum is equal to m

I'm trying some python and I got this:
I have a string S='3,0,4,0,3,1,0,1,0,1,0,0,5,0,4,2' and m=9.
I want to know how many substrings with with sum equals m there are.
So with S and m above i whould get 7 as a result as:
'3,0,4,0,3,1,0,1,0,1,0,0,5,0,4,2'
_'0,4,0,3,1,0,1,0'_____________
_'0,4,0,3,1,0,1'_______________
___'4,0,3,1,0,1,0'_____________
____'4,0,3,1,0,1'_______________
____________________'0,0,5,0,4'_
______________________'0,5,0,4'_
_______________________'5,0,4'_
Now, the code i came up with does something like that
def es1(S,m):
c = 0
M = 0
ls = StringToInt(S)
for x in ls:
i= ls.index(x)
for y in ls[i+1:]:
M = x + y
if M == m:
c += 1
M = 0
break
if M > m:
M = 0
break
else:
continue
return c
def StringToInt(ls):
s = [int(x) for x in ls.split(',')]
return s
Where StringToInt obv gives me a list of int to work with.
The thing I don't get is where my concept is wrong since es1 returns 3
You could use zip to progressively add numbers to a list of sums and count how many 9s you have at each pass:
S = '3,0,4,0,3,1,0,1,0,1,0,0,5,0,4,2'
m = 9
numbers = list(map(int,S.split(",")))
result = 0
sums = numbers
for i in range(len(numbers)):
result += sums.count(m)
sums = [a+b for a,b in zip(sums,numbers[i+1:]) ]
print(result)
For a more "functional programming" approach, you can use accumulate from itertools:
from itertools import accumulate
numbers = list(map(int,S.split(",")))
ranges = (numbers[i:] for i in range(len(numbers)))
sums = (accumulate(r) for r in ranges)
result = sum( list(s).count(m) for s in sums )
print(result)
To explain how this works, let's first look at the content of ranges, which are substrings from each position up to the end of the list:
[3, 0, 4, 0, 3, 1, 0, 1, 0, 1, 0, 0, 5, 0, 4, 2]
[0, 4, 0, 3, 1, 0, 1, 0, 1, 0, 0, 5, 0, 4, 2]
[4, 0, 3, 1, 0, 1, 0, 1, 0, 0, 5, 0, 4, 2]
[0, 3, 1, 0, 1, 0, 1, 0, 0, 5, 0, 4, 2]
[3, 1, 0, 1, 0, 1, 0, 0, 5, 0, 4, 2]
[1, 0, 1, 0, 1, 0, 0, 5, 0, 4, 2]
[0, 1, 0, 1, 0, 0, 5, 0, 4, 2]
[1, 0, 1, 0, 0, 5, 0, 4, 2]
[0, 1, 0, 0, 5, 0, 4, 2]
[1, 0, 0, 5, 0, 4, 2]
[0, 0, 5, 0, 4, 2]
[0, 5, 0, 4, 2]
[5, 0, 4, 2]
[0, 4, 2]
[4, 2]
[2]
When we make a cumulative sum of the rows (sums), we obtain the total of values starting at the position defined by the row number and for a length defined by the column number. e.g. line 5, column 3 represents the sum of 3 values starting at the fifth position:
[3, 3, 7, 7, 10, 11, 11, 12, 12, 13, 13, 13, 18, 18, 22, 24]
[0, 4, 4, 7, 8, 8, 9, 9, 10, 10, 10, 15, 15, 19, 21]
[4, 4, 7, 8, 8, 9, 9, 10, 10, 10, 15, 15, 19, 21]
[0, 3, 4, 4, 5, 5, 6, 6, 6, 11, 11, 15, 17]
[3, 4, 4, 5, 5, 6, 6, 6, 11, 11, 15, 17]
[1, 1, 2, 2, 3, 3, 3, 8, 8, 12, 14]
[0, 1, 1, 2, 2, 2, 7, 7, 11, 13]
[1, 1, 2, 2, 2, 7, 7, 11, 13]
[0, 1, 1, 1, 6, 6, 10, 12]
[1, 1, 1, 6, 6, 10, 12]
[0, 0, 5, 5, 9, 11]
[0, 5, 5, 9, 11]
[5, 5, 9, 11]
[0, 4, 6]
[4, 6]
[2]
In this triangular matrix each position corresponds to the sum of one of the possible substrings. We simply need to count the number of 9s in there to get the result.
The above solutions will perform in O(N^2) time but, if you are concerned with performance, there is a way to obtain the result in O(N) time using a dictionary. Rather than build the whole sub arrays in the above logic, you could simply count the number of positions that add up to each sum. Then, for the sum at each position, go directly to a previous sum total that is exactly m less to get the number of substrings for that position.
from itertools import accumulate
from collections import Counter
numbers = map(int,S.split(","))
result = 0
sums = Counter([0])
for s in accumulate(numbers):
result += sums[s-m]
sums[s] += 1
print(result)
Note that all these solutions support negative numbers in the list as well as a negative or zero target.
As mentioned by others, your code only looks at sums of pairs of elements from the list. You need to look at sublists.
Here is a O(n) complexity solution (i.e. it's efficient since it only scans though the list once):
def es2(s, m):
s = string_to_int(s)
c = 0
# index of left of sub-list
left = 0
# index of right of sub-list
right = 0
# running total of sublist sum
current_sum = 0
while True:
# if the sub-list has the correct sum
if current_sum == m:
# add as many zeros on the end as works
temp_current_sum = current_sum
for temp_right in range(right, len(s) + 1):
if temp_current_sum == m:
c += 1
if temp_right<len(s):
temp_current_sum += s[temp_right]
else:
break
if current_sum >= m:
# move the left end along and update running total
current_sum -= s[left]
left += 1
else:
# move the right end along and update running total
if right == len(s):
# if the end of the list is reached, exit
return c
current_sum += s[right]
right += 1
def string_to_int(ls):
s = [int(x) for x in ls.split(',')]
return s
if __name__ == '__main__':
print(es2('3,0,4,0,3,1,0,1,0,1,0,0,5,0,4,2', 9))
This is the code you are looking for man. i felt looking by position was better for this problem so I did it and it worked.
def es1(S,m):
c = 0
M = 0
ls = StringToInt(S)
for i in range(0, len(ls)):
M = 0
for x in range(i, len(ls)):
M += ls[x]
if M == m:
c += 1
elif M >= m:
break
return c
def StringToInt(ls):
s = [int(x) for x in ls.split(',')]
return s
print(es1("3,0,4,0,3,1,0,1,0,1,0,0,5,0,4,2", 9))
OUTPUT:
7
Your code counts how many pairs of numbers there are in the String S which together give m while you actually want to test all possible substrings.
You could do something like:
numbers = [3,0,4,0,3,1,0,1,0,1,0,0,5,0,4,2]
m = 9
c = 0
for i in range(len(numbers)):
for j in range(len(numbers)-i):
sum = 0
for k in numbers[i:i+j]:
sum += k
if sum == m:
c += 1
print(c)
Output:
7
EDIT! ->This Code is actually all possible subsets, not sublists. I am going to leave this here though in case this solution is helpful to anyone who visits this question.
This code gets every solution. If you take a look in the function es1() the result variable is huge list of arrays with all the possible solutions.
import itertools
def es1(S,m):
result = [seq for i in range(len(StringToInt(S)), 0, -1) for seq in itertools.combinations(StringToInt(S), i) if sum(seq) == m]
return len(result)
def StringToInt(ls):
s = [int(x) for x in ls.split(',')]
return s
print(es1("3,0,4,0,3,1,0,1,0,1,0,0,5,0,4,2", 9))
OUTPUT:
4608
There are 4608 possible sets that add to the value 9.
s = "3,0,4,0,3,1,0,1,0,1,0,0,5,0,4,2"
m = 9
sn = s.replace(",","+") # replace "," with "+"
d = {} # create a dictionary
# create list of strings:
# s2 = ["3+0","0+4","4+0".............]
# s3 = ["3+0+4","0+4+0","4+0+3".............]
# .
# .
# .
for n in range(2,len(s.split(","))):
d["s%s"%n] = [sn[i:i+(2*n-1)] for i in range(0,len(sn),2)][:-n+1]
# evaluate whether sum of individual lists equal to m or not, then find out how many such lists are there
l = sum([eval(x)==m for y in d.values() for x in y] )
# remember we didnot add s1, i,e check whether individual string == m or not
l = l+sum([x==m for x in s.split(",")])
print(l)
7

Pushing items in a list in Python

Let's say there's a list of [0, 3, 14, 0] and i want to 'push' the items of that list to the right corner. So the list will become [0, 0, 3, 14]. So basically the items with value > 0 will ignore the 0s and replace its position. The number could be anything (except 0) so sorting won't do it. Couple of examples:
[0, 4, 0, 4] => [0, 0, 4, 4]
[1, 0, 2, 2] => [0, 1, 2, 2]
[13, 0, 0, 6] => [0, 0, 13, 6]
Is there an efficient solution to this? I have been racking my brain for hours and i've found nothing near a solution.
If you want an efficient solution that is not inplace, and faster than sorting, you can do this:
lst = [0,1,2,0]
helper = [0 for i in len(lst)]
counter = 0
for i in range(len(lst)-1,-1,-1):
if lst[i]:
helper[len(lst)-1-counter] = lst[i]
counter += 1
print(helper)
This is probably not the most efficient solution:
dst_lst = [n for n in lst if n <= 0] + [n for n in lst if n > 0]
This will traverse the original list twice and create two temporary and one final list.
Hope this example helps!
Here we are using Python pre-defined data type queue
>>> import queue
>>> t = queue.deque()
>>> for each in [0, 4, 0, 4]:
... if each <= 0: t.appendleft(each)
... else: t.append(each)
...
>>> t
deque([0, 0, 4, 4])

Compare multidimensional array in python

I have a multidimensional array called old_arr which is like this [[8,8,8,8,0,0,0,0,6,6,5,5],[...]] then I have an updated multidimensional array new_arr like this [[9,9,6,7,3,6,5,0,6,4,3,4],[...]] What I want to do is to update the new_arr so that if the value in it corresponds to a 0 in old_arr then the value should be 0 otherwise keep the new value. So in the above example the new_arr would look like this [[9,9,6,7,0,0,0,0,6,4,3,4],[...]] where the 3,6,5 where replaced by 0. Any advice?
Also I want to know if it is possible to update the cell to 0 only if 4 out of it's 8 surrounding neighbour cells have the value 0 as well? Like new_arr and old_arr are multidimensional array (lists) which represents rows and cols so they are like a big table as shown in the below image where the blue cell in the new_arr will only be updated to zero if the corresponding cell in the old_arr is 0 and 4 of its neighbour cells are 0 (white cells in the photo)
So I need to check all 8neighbour cells (sometimes 6 or 7 depending on the cell position where it's in the middle (8) or edges(7) or corners (6) ) if they are zeros or not and count them, if the count is 4 or more then set the cell value to 0.
So if old_arr is
[[8,8,8,8,0,0,0,0,6,6,5,5],
[8,8,8,8,0,x,0,0,6,6,5,5],
[8,8,8,8,0,0,0,0,6,6,5,5],
[8,8,8,8,0,0,0,0,6,6,5,5],....]
Where x is a zero
And new_arr is
[[9,9,6,7,3,6,5,0,6,4,3,4],
[9,9,6,7,3,6,5,0,6,4,3,4],
[9,9,6,7,3,6,5,0,6,4,3,4],
[9,9,6,7,3,6,5,0,6,4,3,4],....]
For the highlighted cell, the corresponding cell in the new_arr will be zero because the highlighted cell in old_arr is 0 and more than 4 of its neighbor cells are zeros as well.
Updated new_arr is
[[9,9,6,7,3,0,0,0,6,4,3,4],
[9,9,6,7,0,0,0,0,6,4,3,4],
[9,9,6,7,0,0,0,0,6,4,3,4],
[9,9,6,7,0,0,0,0,6,4,3,4],....]
Assuming old_arr and new_arr are the same length, you could do something like this:
old_arr = [[8,8,8,8,0,0,0,0,6,6,5,5], [8,8,7,0,0,0,0,0,6,6,5,5]]
new_arr = [[9,9,6,7,3,6,5,0,6,4,3,4], [9,0,6,7,4,6,5,0,6,4,3,4]]
new_arr = [[x if old[i] else 0 for i, x in enumerate(new)] for old, new in zip(old_arr, new_arr)]
print(new_arr)
which outputs:
[[9, 9, 6, 7, 0, 0, 0, 0, 6, 4, 3, 4], [9, 0, 6, 0, 0, 0, 0, 0, 6, 4, 3, 4]]
UPDATE:
Here is a brute force solution that deals with neighbouring cells:
old_arr = [[8,8,8,8,0,0,0,0,6,6,5,5],
[8,8,8,8,0,0,0,0,6,6,5,5],
[8,8,8,8,0,0,0,0,6,6,5,5],
[8,8,8,8,0,0,0,0,6,6,5,5]]
new_arr = [[9,9,6,7,3,6,5,0,6,4,3,4],
[9,9,6,7,3,6,5,0,6,4,3,4],
[9,9,6,7,3,6,5,0,6,4,3,4],
[9,9,6,7,3,6,5,0,6,4,3,4]]
def first_last(row, next_row, old, new):
for i in range(len(new[row])):
count = 0
if old[row][i] == 0:
if old[row][i-1] == 0:
count += 1
if old[row][i+1] == 0:
count += 1
if old[next_row][i] == 0:
count += 1
if old[next_row][i-1] == 0:
count += 1
if old[next_row][i+1] == 0:
count += 1
if count > 4:
new[row][i] = 0
def middle(old, new):
for i, l in enumerate(new[1:-1]):
for j in range(len(l)):
count = 0
if old[i][j] == 0:
if old[i][j-1] == 0:
count += 1
if old[i][j+1] == 0:
count += 1
if old[i-1][j] == 0:
count += 1
if old[i-1][j-1] == 0:
count += 1
if old[i-1][j+1] == 0:
count += 1
if old[i+1][j] == 0:
count += 1
if old[i+1][j-1] == 0:
count += 1
if old[i+1][j+1] == 0:
count += 1
if count > 4:
l[j] = 0
# first row
first_last(0, 1, old_arr, new_arr)
# middle rows
middle(old_arr, new_arr)
# last row
first_last(-1, -2, old_arr, new_arr)
print(new_arr)
Which Outputs:
[[9, 9, 6, 7, 3, 0, 0, 0, 6, 4, 3, 4],
[9, 9, 6, 7, 0, 0, 0, 0, 6, 4, 3, 4],
[9, 9, 6, 7, 0, 0, 0, 0, 6, 4, 3, 4],
[9, 9, 6, 7, 3, 0, 0, 0, 6, 4, 3, 4]]
Note: This could be made better, but you can optimize it to your liking.
A simple solution with a list comprehension:
>>> old_arr=[9,9,6,7,3,6,5,0,6,4,3,4]
>>> new_arr=[8,8,8,8,0,0,0,0,6,6,5,5]
>>> [new_arr[i] if old_arr[i] else 0 for i in range(len(new_arr))]
[9, 9, 6, 7, 0, 0, 0, 0, 6, 4, 3, 4]
Lists are modifiable sequences in Python so you can change them in place, which can make sense for large data sets. You can do that simply with 2 nested loops:
for i, l in enumerate(new_arr):
for j in range(len(l)):
if old_arr[i][j] == 0:
l[j] = 0
def custom_compare(list_old, list_new):
a=[]
for num, each_list_old in enumerate(list_old):
for num1,each_list_new_entry in enumerate(list_new[num]):
if each_list_old[num1] != 0:
a.append(each_list_new_entry)
else:
a.append(0)
list_new[num] = a
a=[]
print "Finally new_arr = ",list_new
Eg:
old_arr = [[8,8,8,8,0,0,0,0,6,6,5,5],[1,2,3,4,5,6,7,8]]
new_arr = [[9,9,6,7,3,6,5,0,6,4,3,4],[8,3,4,5,6,78,8,9]]
custom_compare(old_arr, new_arr)
Finally new_arr = [[9, 9, 6, 7, 0, 0, 0, 0, 6, 4, 3, 4], [8, 3, 4, 5, 6, 78, 8, 9]]
A simple approach would be something like this:
first=[[8,8,8,8,0,0,0,0,6,6,5,5],[8,8,8,8,0,0,0,0,6,6,5,5],[8,8,8,8,0,0,0,0,6,6,5,5],[8,8,8,8,0,0,0,0,6,6,5,5]]
second = [[9,9,6,7,3,6,5,0,6,4,3,4],
[9,9,6,7,3,6,5,0,6,4,3,4],
[9,9,6,7,3,6,5,0,6,4,3,4],
[9,9,6,7,3,6,5,0,6,4,3,4]]
for first_1,second_1 in zip(first,second):
for index,value in enumerate(first_1):
if value==0:
try:
if first_1[index-1]==0 and first_1[index+1]==0:
second_1[index]=0
second_1[index-1]=0
second_1[index+1]=0
except IndexError:
pass
print(second)
output:
[[9 9 6 7 0 0 0 0 6 4 3 4]
[9 9 6 7 0 0 0 0 6 4 3 4]
[9 9 6 7 0 0 0 0 6 4 3 4]
[9 9 6 7 0 0 0 0 6 4 3 4]]

Categories

Resources