Question

""" ''' Write a python code to push all zeors to the end of an array...

"""
'''
Write a python code to
push all zeors to the end of an array

'''

import numpy as np


def Move_a(i):
  
num = len(a)
  
for k in range (i, num-1):
a[k] = a[k+1]

a[num-1] = 0
  

return a

a = np.array([0,1,4,7,0,9,12,0,0,15,0,21])

#length of array (len)
num = len(a)
print (num)

for i in range(0,num):

if (a[i] == 0):
#Functioon call to Move_a()
a = Move_a(i)
  
  

print ("the array looks like")
print (a)

My code is able to push all zeros to the end of the array, but does not work if two zeros are next to each other. How do I fix this?

Homework Answers

Answer #1

change in your code...

Another O(n) time approach...Simplest..

import numpy as np

a = np.array([0,1,4,7,0,9,12,0,0,15,0,21])
num = len(a)
j = 0
for i in range(0,num):
if (a[i] != 0):
a[i],a[j] = a[j],a[i] # swap...
j+=1
  
print ("the array looks like")
print (a)

plz like it...if u have still any query u can ask through comment..

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Language: Python Write a program to simulate an experiment of tossing a fair coin 16 times...
Language: Python Write a program to simulate an experiment of tossing a fair coin 16 times and counting the number of heads. Repeat this experiment 10**5 times to obtain the number of heads for every 16 tosses; save the number of heads in a vector of size 10**5 (call it headCounts). You should be able to do this in 1-3 lines of numpy code. (Use np.random.uniform1 to generate a 2d array of 10**5 x 16 random numbers between 0 and...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Plotting trajectories Classical Mechanics (Python) Question: In the plot, set v = 1 m/s, ω =...
Plotting trajectories Classical Mechanics (Python) Question: In the plot, set v = 1 m/s, ω = 1 1/s, and plot the trajectory for t ∈ [−6π, 6π]. Code: import numpy as np import matplotlib.pyplot as plt tx=[] for i in range(180): tx.append(i) radianConverter = np.pi/180 x_2 = [1*t*np.cos(2*np.pi*t*radianConverter) for t in tx] y_2 = [1*t*np.sin(2*np.pi*t*radianConverter) for t in tx] plt.figure(figsize=[10,10]) plt.grid(True) plt.axhline(y=0, color='k') plt.axvline(x=0, color='k') plt.plot(x_2,y_2) plt.show() What changes do I have to do in order to fit the question...
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
Code in Python Hello I need an os module to restart and re-run the whole code...
Code in Python Hello I need an os module to restart and re-run the whole code when the multiplier limit is reached. So when the code stops the outputs I need it to initialize again. import os, sys initial=int(input("Initial value : "))       #taking inputs multiplier=float(input("Multiplier : ")) compound=int(input("No of compounds : ")) print("Your values are:") mult=initial                         #initalizing to find answer for i in range(0,compound):         #multiplying by multiplier     print(round(mult,1))     mult=mult*multiplier
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following...
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following questions using the list below. You can record your answers in the essay textbox. data = [5,3,7] A. Write code to replace the value in the 0 position with the number 8. B. Add the value 10 to the end of the list. C. Insert the value 22 after position 1 in the list. D. Remove the value at position 2. E. Sort the...
PYTHON The following code implements this algorithm to sort a list of numbers in ascending order....
PYTHON The following code implements this algorithm to sort a list of numbers in ascending order. But some code is missing as indicated by '?'. def sort_in_place(list_num): for i in range(?): for j in range(?): if ?: temp = list_num[j] list_num[j] = list_num[i] list_num[i] = temp my_list = [23,1,45,20,13,-34] sort_in_place(my_list) print(my_list) Modify the three lines of code in the program below so that the output is [-34, 1, 13, 20, 23, 45]
I am a student taking python programming. Can this problem be modified using the define main...
I am a student taking python programming. Can this problem be modified using the define main method, def main()? import random #function definition #check for even and return 0 if even def isEven(number): if(number%2==0): return 0 #return 1 if odd else: return 1 #count variables even =0 odd = 0 c = 0 #loop iterates for 100 times for i in range(100): #generate random number n = random.randint(0,1000) #function call val = isEven(n) #check value in val and increment if(val==0):...
Develop a Traceroute application in python using ICMP. Your application will use ICMP but, in order...
Develop a Traceroute application in python using ICMP. Your application will use ICMP but, in order to keep it simple, will not exactly follow the official specification in RFC 1739.. Below you will find the skeleton code for the client. You are to complete the skeleton code. The places where you need to fill in code are marked with #Fill in start and #Fill in end. Code from socket import * import os import sys import struct import time import...