Question

In: Computer Science

Task 1: Remove Number Complete the function remove number such that given a list of integers...

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

Solutions

Expert Solution

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


Related Solutions

Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
This function will be given a list of strings and a character. You must remove all...
This function will be given a list of strings and a character. You must remove all occurrences of the character from each string in the list. The function should return the list of strings with the character removed. Signature: public static ArrayList<String> removeChar(String pattern, ArrayList<String> list)
Develop a C++ function to find the number of even and odd integers in a given...
Develop a C++ function to find the number of even and odd integers in a given array of integers Pass in an array of integers Pass in the size of the array of integers Pass in a reference parameter for the number of even values Pass in a reference parameter for the number of odd values The function doesn't return anything Develop a C++ program to test your function
In Coral. Given a sorted list of integers, output the middle integer .Assume the number of...
In Coral. Given a sorted list of integers, output the middle integer .Assume the number of integers ia odd. Ex: if the input 2 3 4 8 11 -1(a negative indicates end), the output is 4. the maximum number of inputs for any test case should not exceed 9 positive values. If exceeded , output Too many inputs". Hint: Use an array of size 9. First read the data into array.Then,based in the number of items, find the middle item.
Java, no imports: Given a list of integers, round each number to the nearest multiple of...
Java, no imports: Given a list of integers, round each number to the nearest multiple of 5.        2.5-7.49 round to 5. 7.5-12.49 round to 10. 12.5-17.49 round to 15. etc.        return the sum of all the rounded elements.        Implement this way for credit:        Fill in the method directly below this one called helperRound() to round each number. Call that method and use the sum of the returned values.        helperSum({4.3, 16.7})...
This function will receive a list of elements with duplicate elements, this function should remove the...
This function will receive a list of elements with duplicate elements, this function should remove the duplicate elements in the list and return a list without duplicate elements. The elements in the returned list must be in the same order that they were found in the list the function received. A duplicate element is an element found more than one time in the specified list. JAVA
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates...
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd. Ex: If the input is: 2 3 4 8 11 -1 the output is: Middle item: 4 The maximum number of inputs for any test case should not exceed 9. If exceeded, output "Too many numbers". Hint: First read the...
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT