Question

In: Computer Science

Write a function that receives a StaticArray with integers and returns a new StaticArray object with...

Write a function that receives a StaticArray with integers and returns a new StaticArray object with the content from the original array, modified as follows:

1) If the number in the original array is divisible by 3, the corresponding element in the new array should be a string ‘fizz’.

2) If the number in the original array is divisible by 5, the corresponding element in the new array should be a string ‘buzz’.

3) If the number in the original array is both a multiple of 3 and a multiple of 5, the corresponding element in the new array should be a string ‘fizzbuzz’. 4) If all other cases, the element in the new array should have the same value as in the original array.

Content of the input array should not be changed. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions.

Below is the starter “skeleton” code for this problem, on which the implementation must be built. Methods defined in the skeleton code must retain their names and input / output parameters. Variables defined in skeleton code must also retain their names.

def fizz_buzz(arr: StaticArray) -> StaticArray:

"""

TODO: Write this implementation

"""

return StaticArray()

# BASIC TESTING

if __name__ == "__main__":

# example 1 source = [_ for _ in range(-5, 20, 4)]

arr = StaticArray(len(source))

for i, value in enumerate(source):

arr[i] = value

print(fizz_buzz(arr))

print(arr)

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

Note: Since you did not provide StaticArray class code, which is crucial for this function, I have written this based on assumptions only. So in case if this implementation does not work, please share the code for StaticArray class, and I'll fix the below code. Also, when you post a question in future, please try to include all existing code/resources.

#code


def fizz_buzz(arr: StaticArray) -> StaticArray:
    """
    Note: Since you did not provide StaticArray class implementation, I'm writing this based on assumptions only.
    """
    # creating a StaticArray object with capacity = length of arr. assuming StaticArray class implements __len__()
    # method
    result = StaticArray(len(arr))
    # looping through each index between 0 and len(arr)-1
    for i in range(len(arr)):
        num = arr[i]  # fetching value at index i
        if num % 3 == 0 and num % 5 == 0:  # checking if num is divisible by both 3 and 5
            result[i] = "fizzbuzz"  # storing fizzbuzz in current position on result array
        elif num % 3 == 0:  # divsible by 3, not 5
            result[i] = "fizz"
        elif num % 5 == 0:  # divisible by 5, not 3
            result[i] = "buzz"
        else:  # not divisible by 3 or 5
            result[i] = arr[i]
    return result


# BASIC TESTING

if __name__ == "__main__":
    # example 1
    source = [_ for _ in range(-5, 20, 4)]
    arr = StaticArray(len(source))
    for i, value in enumerate(source):
        arr[i] = value
    print(fizz_buzz(arr))
    print(arr)

Related Solutions

Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Write a Scheme function that takes a list of integers and returns all odd integers on...
Write a Scheme function that takes a list of integers and returns all odd integers on the list in the original order: (odd-numbers `(2 4 9 16 25 7)) (9 25 7) Hint: 2 (remainder 13 5) 3 Please explain every step
In PYTHON: Write a function that receives a sentence and returns the last word of that...
In PYTHON: Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence. To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
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.
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
Write a Python function that receives a stack object s, where the items in the stack...
Write a Python function that receives a stack object s, where the items in the stack are only numbers in the set {0,1} and returns the number of 1's in the stack s. The function must satisfy the following restrictions: the state of the stack must be preserved; ie., after calling this function the state of the stack s must be the same it was before the function was called. The function cannot use any additional variables of any of...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT