Skip to content

Instantly share code, notes, and snippets.

@AustinBCole
Created November 8, 2018 16:45
Show Gist options
  • Save AustinBCole/b5e2c5199fa15efed954c457aebb56fb to your computer and use it in GitHub Desktop.
Save AustinBCole/b5e2c5199fa15efed954c457aebb56fb to your computer and use it in GitHub Desktop.
Daily Challenge 11/8
Assumptions:
I assume that I need to write a line of code to filter out non-postivie integers, because we only want the multiples of
positive integers. That is really the only assumption I am making, the rest of the instruction is fairly straightforward and does
not require further assumptions.
Test Cases:
-1: Should return nil, the function will not run.
0: Should return nil as well, because 0 is also not a postiive integer
1: Should return 0, because there are 0 multiples of 5 or 3 between 0 and 1
10: Should return 23
12: Should return 23 as well because we want all multiples of 3 and 5 BEFORE 12, not after
13: Should return 35 because it now includes 12
Approach:
First I will filter out negative integers. Next I will put all integers between one and the inputted number in an array.
Next I will I will create a for loop to lopp through all numbers in the array. I will create an "if" statement to add all
multiples of 3 and 5 into another empy array. To do this I will use the modulo operator. I will then use a nested for statement
to add all of the numbers in the new array together. I will save this in a new variable of type Int and will return it.
Code:
func sumOf35Multiples(_ number: Int) -> Int? {
var sum = 0
//This array will be used to add up all of the numbers below the inputted number
var multiplesOf35Array: [Int] = []
//I am weeding out all non-positive numbers here
guard number > 0 else { return nil }
//below I insert a for loop to assign all multiples of 3 and 5 between 1 and (number-1)
//to a returnedNumberArray
for num in 0...(number - 1){
if num % 3 == 0 || num % 5 == 0 {
multiplesOf35Array += [num]
}
}
//I print here to make sure that the for loop above is working how I want it to
print(multiplesOf35Array)
//Here i loop through each index of the array and add them together, storing them in the
//sum variable
for index in multiplesOf35Array {
sum += index
}
//sum variable is returned
return sum
}
sumOf35Multiples(12)
@dillon-mce
Copy link

Looks great Austin! It definitely works. I can clearly see your thought process. And I like how you're guarding against bad inputs.

I don't know how much time you had left, but my only suggestion would have been to try to condense the function a little bit. Do you need to store the multiples in their own array? In a real-world implementation, it might be worthwhile to cache an array of the multiples that you can query against so you don't have to run through the whole for loop every time (would be especially useful if you we're looking for the sum of large numbers lots of times). I think your solution could be refactored to do that relatively easily, so it might a better solution in the long run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment