Write a program to move zeros in Python
Problem : Given an array of random numbers, move all zeros in that array to the end of the array.
Example : Given array {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it becomes {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0} after sorting. The order of all other elements is the same.
Input : arr[] = {1, 2, 0, 4, 3, 0, 5, 0}; Output : arr[] = {1, 2, 4, 3, 5, 0, 0, 0}; Input : arr[] = {1, 2, 0, 0, 0, 3, 6}; Output : arr[] = {1, 2, 3, 6, 0, 0, 0};
In this article, TipsMake.com will work with you to write a program to move zeros in Python.
Method 1: Use a loop
Move the array "arr" from left to right. While moving, keep checking for non-zero element in array. Let's set the position of non-zero elements as "count". For each element other than 0 arr[i], set the element to "arr[count]" and increment the value "count". Once the move was completed all non-zero elements were moved to the front and "count" was set as the index of the first zero. Now, what we need to do is run a loop for all zero elements from "count" to the end of the array.
Here is sample code:
# Python3 code to move all zeroes # at the end of array # Function which pushes all # zeros to end of an array. def pushZerosToEnd(arr, n): count = 0 # Count of non-zero elements # Traverse the array. If element # encountered is non-zero, then # replace the element at index # 'count' with this element for i in range(n): if arr[i] != 0: # here count is incremented arr[count] = arr[i] count+=1 # Now all non-zero elements have been # shifted to front and 'count' is set # as index of first 0. Make all # elements 0 from count to end. while count < n: arr[count] = 0 count += 1 # Driver code arr = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9] n = len(arr) pushZerosToEnd(arr, n) print("Mảng sau khi di chuyển toàn bộ số 0 về cuối :") print(arr) # This code is contributed by "Abhishek Sharma 44"
Method 2: Partition the array
The approach of this method is quite simple. We will use 0 as the pivot element and whenever we see a non-zero element we will swap it with the pivot element. As a result, non-zero elements will appear at the beginning.
Sample code:
# Python Program to move all zeros to the end A = [5, 6, 0, 4, 6, 0, 9, 0, 8] n = len(A) j = 0 for i in range(n): if A[i] != 0: A[j], A[i] = A[i], A[j] # Partitioning the array j += 1 print(A) # Print the array # This code is contributed by Tapesh(tapeshdua420)
Method 3: Use the method to count the number 0
In this approach we will traverse the entire array and will count the number of zeros present in the array. While counting, we will remove zero from the array.
After completing the above process, we will push back the count of zeros into the array.
Here is sample code:
# Python program to shift all zeros # to right most side of array # without affecting order of non-zero # elements # Given list arr = [5, 6, 0, 4, 6, 0, 9, 0, 8] # Storing all non zero values nonZeroValues = [x for x in arr if x != 0] # Storing all zeroes zeroes = [j for j in arr if j == 0] # Updating the answer arr = nonZeroValues + zeroes # Printing the answer print( "Mảng sau khi di chuyển toàn bộ số 0 về cuối là: " + arr)
TipsMake.com hope that this article will be useful to you!
You should read it
May be interested
- Write a program to reverse a string in Pythonwhat is string reversal in python? how to use the string reverse function in python? let's find out with tipsmake.com.com!
- Write a program to turn multiple integers into a single integer using Pythonin this article, tipsmake.com will learn how to write a program to turn multiple integers into a single integer using the python programming language.
- Python online editorthis python online compiler has two parts: the above is called script.py, where you enter python code. click run code when writing the code, the output will display below, in the ipython shell section.
- Write a program to calculate the number of ways to climb stairs in Pythonin this article, tipsmake.com will learn with you how to write a program to count the number of ways to climb stairs in python.
- The leading zero format in Excel number ranges is easyyou probably find it very frustrating to write a phone number or write a tax code, ... in excel and write the number '0' already but it disappears. why is it
- How to Write a Basic Python Programpython is a high-level programming language. the language utilizes a straightforward syntax which can make it easy for new users to get started. ==== install the dependencies ====
- Write a program to find Excel column labels by a given number of columns in Pythonin this article, tipsmake.com will learn with you how to write a program to find excel column labels by a given number of columns in python.
- How to set up Python to program on WSLget started with cross-platform python programming by setting up python on the windows subsystem for linux. here's how to set up python for wsl programming.
- How to create a command line program in Python with Clickclick is a python package to write command line interfaces with as little code as possible. this article will show you how to use click to create the command line program.
- Learn the first Python programin this article we will learn a simple python program to get a little more insight into python, before starting to learn about the main components of this programming language.