In: Computer Science
Task 1: Remove Number
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.
Task 2: Logged List
The log2() function is one of an algorithm designer’s favourite functions. You’ll learn more about this later, but briefly – if your input size is 1048576 elements, but you only look at log2(1048576) elements, you’ll really just look at 20 elements.
Bongo, an algorithm designer, designs algorithms that take lists as inputs. Being an efficient designer, all his algorithms are designed such that they look at log2(n) elements if the number of elements in the input list is n.
To assist him, we’ve designed the logged list function. This function takes a list l as input and returns a new list such that it has all the elements in original list l which occur at indices that are powers of 2. For example: indices 1, 2, 4, 8, 16, 32, etc.
However, something is wrong. The function isn’t just returning a list with elements at all indices that are powers of 2. Fix the function so it works correctly!
You must make sure that the fixes are minor fixes. Just like major lab 2, this means that your code will not need a lot of changes.
Task 3: Move Zeros
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!
Task 4: Find Number
Tony (your team member at work) implemented a function find number which is supposed to return the index of a number in a list if the number is in the list, else return -1. However, it doesn’t work the way it’s expected to...
Fix the function so it works correctly. Just like Task 2, your fixes should be minor fixes. Don’t try adding more code, you only need to see how you can change the existing code a bit.
See the Starter code below:
# 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 new list.
>>> lst = [1, 2, 3]
>>> remove_number(lst, 3)
>>> lst
[1, 2]
"""
pass
def logged_list(lst: List[object]) -> List[object]:
"""
Return a new list such that it has all the objects
in lst which occur at indices which are powers of 2
>>> logged_list([0, 1, 2, 3, 4, 5, 6, 7, 8])
[1, 2, 4, 8]
"""
# TODO: FIX
i = 0
new_lst = []
while i < len(lst) - 1:
new_lst.append(lst[i])
i += 2
return new_lst
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
def find_number(lst: List[int], number: int) -> int:
"""
Return the first index of the number if the number is in the
lst else return -1
>>> find_number([1, 2, 3], 3)
2
>>> find_number([1, 2, 3], 4)
-1
"""
# TODO: this code needs to be fixed. Fix
found = False
i = 0
while not found:
if lst[i] == number:
found = True
else:
found = False
i += 1
if found:
return i
return -1
if __name__ == '__main__':
# uncomment the code below once you are done the lab
# import doctest
# doctest.testmod()
pass
from typing import List
def remove_number(lst: List[int], number: int) -> None:
#If list does not contain given value then it will raise ValueError
try:
#Using remove function, we are removing the value
lst.remove(number)
return lst
except ValueError:
return "This list does not contain number value"
def logged_list(lst: List[object]) -> List[object]:
#Raising power till less than length of list
power = 0
i = 2**power
new_lst = []
while i < len(lst):
new_lst.append(lst[i])
power += 1
i = 2**power
return new_lst
def move_zeros(lst: List[int]) -> None:
new_list = []
#Appending values into new_list which are not equal to zero
for i in range(len(lst)):
if lst[i]!=0:
new_list.append(lst[i])
#Appending zeros with equal to length of given list
for i in range(len(new_list),len(lst)):
new_list.append(0)
return new_list
def find_number(lst: List[int], number: int) -> int:
#Finding number using contains method
if lst.__contains__(number):
return lst.index(number)
else:
return -1
def main():
print(remove_number([1,2,3],4))
print(logged_list([1,2,3,4,5,6,7,8,9,10]))
print(move_zeros([1,0,2,0,0,3]))
print(find_number([1,2,3],3))
if __name__ == '__main__':
main()
Sample input and output;
print(remove_number([1,2,3],4)) print(logged_list([1,2,3,4,5,6,7,8,9,10])) print(move_zeros([1,0,2,0,0,3])) print(find_number([1,2,3],3))
This list does not contain number value
[2, 3, 5, 9]
[1, 2, 3, 0, 0, 0]
2
print(remove_number([1,2,3],3)) print(logged_list([0,1,2,3,4,5,6,7,8])) print(move_zeros([1,0,2,3])) print(find_number([1,2,3],3)) print(find_number([1, 2, 3],4))
[1, 2]
[1, 2, 4, 8]
[1, 2, 3, 0]
2
-1