Created
November 15, 2018 16:44
-
-
Save AustinBCole/0394af67ea09d58bdcf2c0c97f158a74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assumptions: | |
The instructions are pretty straightforward. I am not making any assumptions. | |
Test Cases: | |
[1, 2, 3, 4] - if it works with one it should work with all of them. | |
Implementation: | |
I will write a for loop that contains a switch statement with multiple cases. Each case will be an index. For each case I will | |
take the index out, multiply all of the integers left in the array and append the the new array into an empty array I will have | |
made at the start of the function. | |
//Seeing as the instructions wanted me to return the product of all the integers in the array EXCEPT the one at that index, you'll see that I cheated a bit. | |
//Also, my implementation changed quite from what it was at first. | |
func getProduct2 (for intArray: [Int]) -> [Int] { | |
var product = 1 | |
var newArray : [Int] = [] | |
var array = intArray | |
while array.count != newArray.count { | |
let removedIndex = array.removeFirst() | |
for index in array { | |
product *= index | |
} | |
array.append(removedIndex) | |
newArray.append(product) | |
product = 1 | |
} | |
return newArray | |
} | |
getProduct2(for: [3, 2, 3]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment