Last active
October 3, 2019 20:53
-
-
Save junebash/7f4ea865f9053ca343385640f1f594f8 to your computer and use it in GitHub Desktop.
iOSCodeChallenge-Sprint02-02 (prompt: https://gist.github.com/jkaunert/eb9998ef2ca666db35e457261b897b4f)
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
//: ## version that only accepts positive values | |
//: | |
//: This version follows the instructions of only working for positive values of sum and product | |
func sumAndProduct(sum: Int, product: Int) -> [Int] { | |
for x in 1...sum/2 { | |
let y = sum - x | |
if x * y == product { | |
return [x, y] | |
} | |
} | |
return [] | |
} | |
sumAndProduct(sum: 6, product: 9) // [3, 3] | |
sumAndProduct(sum: 21, product: 108) // [9, 12] | |
sumAndProduct(sum: 10, product: 25) // [5, 5] | |
sumAndProduct(sum: 20, product: 100) // [10, 10] | |
sumAndProduct(sum: 101, product: 100) // [1, 100] | |
sumAndProduct(sum: 67, product: 3) // [] | |
//:------------------------------------------------------- | |
//: | |
//: Everything below was added after the deadline passed | |
//: | |
//: ## version for any int values (not just positive) | |
func sumAndProductAny(sum: Int, product: Int) -> [Int] { | |
var x = sum / 2 | |
var y = sum - x | |
while x * y >= product { | |
if x * y == product { | |
if x > y { | |
return [y, x] | |
} | |
return [x, y] | |
} | |
x -= 1 | |
y += 1 | |
} | |
return [] | |
} | |
sumAndProductAny(sum: 8, product: -20) // -2, 10 | |
sumAndProductAny(sum: -6, product: 9) // -3, -3 | |
sumAndProductAny(sum: -8, product: -20) // -10, 2 | |
sumAndProductAny(sum: 5, product: 0) // 0, 5 | |
sumAndProductAny(sum: 2, product: 1) // 1, 1 | |
sumAndProductAny(sum: -2, product: 1) // -1, -1 | |
sumAndProductAny(sum: 0, product: 25) // [] | |
sumAndProductAny(sum: 0, product: -25) // -5, 5 | |
sumAndProductAny(sum: 6, product: 9) // 3, 3 | |
sumAndProductAny(sum: 21, product: 108) // 9, 12 | |
sumAndProductAny(sum: 10, product: 25) // 5, 5 | |
sumAndProductAny(sum: 20, product: 100) // 10, 10 | |
sumAndProductAny(sum: 101, product: 100) // 1, 100 | |
sumAndProductAny(sum: 23, product: 60) // 3, 20 | |
sumAndProductAny(sum: 67, product: 3) // [] | |
sumAndProductAny(sum: 1, product: 2) // [] | |
sumAndProductAny(sum: -2, product: 20) // [] | |
sumAndProductAny(sum: 2, product: -20) // [] | |
sumAndProductAny(sum: -32, product: -42) // [] | |
//: ## Messy version I made along the way | |
//: | |
//: The following version of `sumAndProductAny()` was made before I made the much cleaner version above. | |
func sumAndProductAnyMessy(sum: Int, product: Int) -> [Int] { | |
if abs(sum) == 1 && product != 0 { return [] } | |
if sum > 0 { | |
if product > 0 { // sum pos, prod pos | |
for x in 1 ... (sum / 2) { | |
let y = sum - x | |
if x * y == product { | |
return [x, y] | |
} | |
} | |
} else if product == 0 { // sum pos, prod 0 | |
return [0, sum] | |
} else if product < 0 { // sum pos, prod neg | |
for x in ((-sum / 2) ... -1) { | |
let y = sum - x | |
if x * y == product { | |
return [x, y] | |
} | |
} | |
} | |
} else if sum == 0 { | |
// // sum 0, prod pos -> [] | |
if product == 0 { // sum 0, prod 0 | |
return [0,0] | |
} else if product < 0 { // sum 0, prod neg | |
var x = -1, y = 1 | |
while x * y >= product { | |
if x * y == product { | |
return [x,y] | |
} | |
x -= 1 | |
y += 1 | |
} | |
} | |
} else if sum < 0 { | |
if product > 0 { // sum neg, prod pos | |
let absSum = -sum | |
for x in 1 ... (absSum / 2) { | |
let y = absSum - x | |
if x * y == product { | |
return [-x, -y] | |
} | |
} | |
} else if product == 0 { // sum neg, prod 0 | |
return [sum, 0] | |
} else { // sum neg, prod neg | |
let absSum = -sum | |
for x in 1 ... (absSum / 2) { | |
let y = -1 * (sum - x) | |
if -x * y == product { | |
return [-y, x] | |
} | |
} | |
} | |
} | |
return [] | |
} | |
sumAndProductAnyMessy(sum: 8, product: -20) // -2, 10 | |
sumAndProductAnyMessy(sum: -6, product: 9) // -3, -3 | |
sumAndProductAnyMessy(sum: -8, product: -20) // -10, 2 | |
sumAndProductAnyMessy(sum: 5, product: 0) // 0, 5 | |
sumAndProductAnyMessy(sum: 2, product: 1) // 1, 1 | |
sumAndProductAnyMessy(sum: -2, product: 1) // -1, -1 | |
sumAndProductAnyMessy(sum: 0, product: 25) // [] | |
sumAndProductAnyMessy(sum: 0, product: -25) // -5, 5 | |
sumAndProductAnyMessy(sum: 6, product: 9) // 3, 3 | |
sumAndProductAnyMessy(sum: 21, product: 108) // 9, 12 | |
sumAndProductAnyMessy(sum: 10, product: 25) // 5, 5 | |
sumAndProductAnyMessy(sum: 20, product: 100) // 10, 10 | |
sumAndProductAnyMessy(sum: 101, product: 100) // 1, 100 | |
sumAndProductAnyMessy(sum: 67, product: 3) // [] | |
sumAndProductAnyMessy(sum: 1, product: 2) // [] | |
sumAndProductAnyMessy(sum: -2, product: 20) // [] | |
sumAndProductAnyMessy(sum: 2, product: -20) // [] | |
sumAndProductAnyMessy(sum: -32, product: -42) // [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment