Last active
February 19, 2018 06:46
-
-
Save SwiftArchitect/d884172e10a5cbc9e3ee51a9e2ef8ca4 to your computer and use it in GitHub Desktop.
Xcode Swift Functional Programming alternative and Unit Test for InterviewCake stock price question. ⚠️ For the full experience, join https://interviewcake.com and visit https://www.interviewcake.com/question/swift/stock-price
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
// | |
// InterviewCake-StockPrice.swift | |
// | |
// Copyright © 2018 Xavier Schott | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
// | |
import Foundation | |
class StockPrice { | |
func getMaxProfit(from stockPrices:[Int]) -> Int { | |
var minPrice = Int.max | |
var maxProfit = Int.min | |
stockPrices.forEach { (currentPrice) in | |
maxProfit = max(maxProfit, currentPrice - minPrice) | |
minPrice = min(minPrice, currentPrice) | |
} | |
return maxProfit | |
} | |
} |
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
// | |
// StockPriceTests.swift | |
// | |
// Copyright © 2018 Xavier Schott | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
// | |
import XCTest | |
class appleStockPriceTests: XCTestCase { | |
func testGetMaxProfit() { | |
let result = StockPrice().getMaxProfit(from: [12, 7, 5, 8, 11, 9, 1, 5]) | |
XCTAssertTrue(6 == result) | |
} | |
func testGetMaxProfitGoingDownAllDay() { | |
let result = StockPrice().getMaxProfit(from: [100, 90, 79, 67, 54, 40, 25, 9]) | |
XCTAssertTrue(-10 == result) | |
} | |
func testGetMaxProfitSingleRise() { | |
let result = StockPrice().getMaxProfit(from: [100, 90, 80, 70, 80, 50, 40, 30, 20, 10]) | |
XCTAssertTrue(10 == result) | |
} | |
func testGetMaxProfitZeroEntry() { | |
// Not designed to return meaningful value with less than 2 entries | |
let result = StockPrice().getMaxProfit(from: []) | |
XCTAssertTrue(Int.min == result) | |
} | |
func testGetMaxProfitOneEntry() { | |
// Not designed to return meaningful value with less than 2 entries | |
let result = StockPrice().getMaxProfit(from: [20]) | |
XCTAssertTrue(Int.min + 20 + 1 == result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment