Question

In: Computer Science

(Programming Language: Python) It's trivial that the value of a number remains the same no matter...

(Programming Language: Python)

It's trivial that the value of a number remains the same no matter how many zeros precede it. However, adding the zeros at the end of the number increases the value * 10. Jasmine is tired of seeing \001/100" on her tests (well yes, no one really writes 001, but Jasmine's teacher finds it funny to do so). So, she managed to login to her teacher's computer and now wants to design a function that can move the 0's in her grade to the end before her teacher uploads her grades. Although we don't agree with Jasmine's tactics, Krish (Jasmine's mentor) told us to help her out by finishing the `move zeros' function. This function should move all the 0's in the given list to the end while preserving the order of the other elements.

Remember that this function needs to modify the list, not return a new list!

---------------------------------------------

def move_zeros(lst: List[int]) -> None:

"""

Move all the 0's in lst to the END of the lst *in-place*, i.e. *modify* the list, DONT return a new list

>>> lst = [1, 0, 2, 3]

>>> move_zeros(lst)

>>> lst [1, 2, 3, 0]

"""

pass

Solutions

Expert Solution

  • Below is the detailed implementation of the above problem in PYTHON with code and output shown.
  • For better understanding please read the comments mentioned in the code.
  • In the code below function move_zeros(lst) is implemented with testing it with sample test cases in which we modify the list which comes as a argument in the function such that the 0's present in the list are moved to the end of the list wihtout changing the order of the non-zero elements of list. We take a pointer starting from 0 and everytime we have a non-zero element we place it at the position where pointer points and move the pointer forward and at last the positions which are left to fill by the pointer are filled with 0's.
  • CODE:

#function which modifies the given list such that all zeros are pushed at the end
def move_zeros(lst):
#pointer which points to a location at which we will place a non-zero element
i=0
#iterate on list
for j in range(len(lst)):
#if current element is non-zero then,
if lst[j]!=0:
#place this at ith position and move i by 1
lst[i]=lst[j]
i+=1
#now for the positions left in the list
while i<len(lst):
#place 0 here
lst[i]=0
i+=1

#test case 1
lst = [1, 0, 2, 3]
print("Before: ",lst)
move_zeros(lst)
print("After: ",lst)

#test case 2
lst = [1, 2, 0, 4, 3, 0, 5, 0]
print("Before: ",lst)
move_zeros(lst)
print("After: ",lst)

  • OUTPUT:
  1. Before:  [1, 0, 2, 3]
    After:  [1, 2, 3, 0]
  2. Before:  [1, 2, 0, 4, 3, 0, 5, 0]
    After:  [1, 2, 4, 3, 5, 0, 0, 0]
  • Below are the screenshot attached for the code and output for better clarity and understanding.

CODE and OUTPUT

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


Related Solutions

(Programming Language: Python) Complete the function remove number such that given a list of integers and...
(Programming Language: Python) Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. # DO NOT ADD ANY OTHER IMPORTS from typing import List def remove_number(lst: List[int], number: int) -> None: """ Remove every instance of number in lst. Do this *in-place*, i.e. *modify* the list. Do NOT return a...
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
Programming language Python It have to be in Functions with a main function Samuel is a...
Programming language Python It have to be in Functions with a main function Samuel is a math teacher at Hogwarts School of Witchcraft and Wizardry. He loves to give his students multiplication exercises. However, he doesn’t care about the actual operation result but the unit sum of its digits. At Hogwarts School of Witchcraft and Wizardry, they define the unit sum (US) of N as the unit that it is left after doing the sum of all the digits of...
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
Programming language in Python Suppose, for Jane, n1 = 3, n2 = 4, and n3 =...
Programming language in Python Suppose, for Jane, n1 = 3, n2 = 4, and n3 = 5. Also suppose, Jane iterates the number from 1 to 15. At the beginning, Jane sets count to 0, and then proceeds iterating the number from 1 to 15 and for each iteration does the following: for 1, count is increased by 1 because it is not divisible by 3, 4, and 5; count is now: 1 for 2, count is increased by 2...
Programming language is in python 3 For this project, you will import the json module. Write...
Programming language is in python 3 For this project, you will import the json module. Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as parameters a...
Programming language is python 3 For this project, you will import the json module. Write a...
Programming language is python 3 For this project, you will import the json module. Write a class named NeighborhoodPets that has methods for adding a pet, deleting a pet, searching for the owner of a pet, saving data to a JSON file, loading data from a JSON file, and getting a set of all pet species. It will only be loading JSON files that it has previously created, so the internal organization of the data is up to you. The...
a summary explaining the basic understanding of the following programming concepts using a language of python:...
a summary explaining the basic understanding of the following programming concepts using a language of python: •Variables •Arithmetic and Logical operations •Sequential coding (Structured programming •Decision structure (If statements) •Repetition structure •Functions with some coding demos inside visual studio code python IDE which can be sent as screenshot. preferably a typed summary please which can be written into powerpoint pleaseeeee ???
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): A website requests an user to input his account password. Write a program to examize the validity of the password. The valid password must consists of: At least 1 letter in [a-z] At least 1 number in [0-9] At least a letter in [A-Z] (capital) At least one special letter in [$#@] At least...
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): Write a function to seek for all even numbers and odd numbers in the middle of two number A and B. Print even and odd numbers in 1 and 2020 (including both these two numbers)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT