Skip to content

Instantly share code, notes, and snippets.

@sengkyaut
Created September 26, 2022 14:34
Show Gist options
  • Save sengkyaut/3d0f212395de00b1ddc050a79c0000ed to your computer and use it in GitHub Desktop.
Save sengkyaut/3d0f212395de00b1ddc050a79c0000ed to your computer and use it in GitHub Desktop.
Divided by 3 or 5
# Create a function that takes in an input: 'nums', which is an array/iterable of positive integers
# e.g. nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] The function should return an array (arr) that mirrors nums as follows.
# 1. For each index in nums, there should be a corresponding string in arr
# 2. If the integer at the given index in nums is divisible by 3, the string in arr should be "Fizz"
# 3. If the integer at the given index in nums is divisible by 5, the string in arr should be "Buzz"
# 4. However, if the integer at the given index in nums is divisible by BOTH 3 and 5, the string in arr should be "FizzBuzz" instead
# 5. If the integer at the given index in nums is not divisible by 3 or 5,the string in arr should be the integer cast as a string,
# e.g. if the integer in nums is 1, the integer in arr should be "1"
# The function should also print each number in the array one by one each on a new line.
# Example: Input: nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
def fizzbuzz(nums):
new_arr = []
for i in nums:
if i % 3 == 0:
modify_str = "Fizz"
if i % 5 == 0:
modify_str += "Buzz"
elif i % 5 == 0:
modify_str = "Buzz"
else:
modify_str = str(i)
print(modify_str)
new_arr.append(modify_str)
return new_arr
fizzbuzz(nums)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment