In: Computer Science
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)
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)