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 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.
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.
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.
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
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...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
write a function mean_value that accepts up to four integers and returns their possibly floating point...
write a function mean_value that accepts up to four integers and returns their possibly floating point mean_value. the function should be able to work with any number of inputs from one to four. ( it just returns the input if there is only one)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT