Skip to content

Instantly share code, notes, and snippets.

@bhakes
Created November 7, 2018 16:44
Show Gist options
  • Save bhakes/9effcfc34bc15d93dec227516851e258 to your computer and use it in GitHub Desktop.
Save bhakes/9effcfc34bc15d93dec227516851e258 to your computer and use it in GitHub Desktop.
import Cocoa
// trying to implement test cases
var intPairArray = [[],["",""],["",2],[2,""],[1,1],[1,1],[1,2],[1,3],[4,1]]
var intPairArray2 = [12,15]
// main function here
public func sumAndProduct(_ pIntPairArray: [Array<Int>])->[Int]{
for intPair in intPairArray{
// trying to implement guard here
guard (intPair.count < 2) else {continue }
return intPair
}
return []
}
// Trying to use Greatest common divisor formula to find a solution
var val1 = intPairArray2[0]
var val2 = intPairArray2[1]
var remainder = 1
var result = 1
while (remainder != 0){
remainder = val2 % val1
result = val2 / val1
print(remainder)
val2 = val1
val1 = remainder
}
print (val2)
print (result)
print ([val2+val2, val2*val2])
@dillon-mce
Copy link

You're on the right track Ben! I can see that you've spent some time thinking through the problem and it seems like you have a pretty good idea of what your function should do (that is a good first step, and not everyone got there on today's challenge). I also like that you're trying a wider variety of test cases, though I think you've gone a little too far here. Since you've defined your function as taking an array of Ints, the compiler won't let you run it with anything but an Int in your function call. So several of your test cases won't even run. (Which is what you want. The function only accepts Ints, so you only have to deal with them inside the function.)

I realized my format with the testCases yesterday was probably a little confusing, and unnecessarily so. Don't feel like you have to put them in an array, that will probably lead to more confusion than it is worth at the moment. Take a look at how I've defined my test cases in today's solution. https://gist.github.com/dillon-mce/5a72855079d1ed80320d585a5cb95a48

On the actual logic, one thing that can help is to break down the problem into smaller parts. And it seems like you've started down that track with trying to find the GCD. Personally I would start with the sum though, because that is simpler. How would you find any two numbers that add up to the sum? Or a series of them? Could you then loop through that series and check to see if they multiply to the product?

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