forked from yagom-academy/ios-calculator-app
-
Notifications
You must be signed in to change notification settings - Fork 0
๐๐ป ๊ธฐ์ ์ ๋์
Wonbi edited this page Oct 7, 2022
·
1 revision
struct LinkedList<T: CalculateItem> {
class Node<T: CalculateItem> {
fileprivate var value: T
fileprivate var next: Node?
fileprivate init(_ value: T, next: Node? = nil) {
self.value = value
self.next = next
}
}
private(set) var head: Node<T>?
private(set) var nodeCount: Int = 0
mutating func removeFirst() -> T? {
let value = head?.value
head = head?.next
nodeCount -= 1
return value
}
mutating func removeAll() {
head = nil
nodeCount = 0
}
}
CalculatorItemQueue
์์ dequeue
๋ฅผ ์คํํ ๊ฒฝ์ฐ
- ์ด๋ฅผ ๋ฐฐ์ด๋ก ๊ตฌํํ ๊ฒฝ์ฐ
Array
์ ๊ธฐ๋ณธ ๋ฉ์๋์ธremoveFirst()
๋ฅผ ์ฌ์ฉํด์ผ๋ง ํฉ๋๋ค. ํ์ง๋ง ์ด ๋ฉ์๋๋ 0๋ฒ index ๊ฐ์ ์ง์ด ํ, ์ดํ index์ ๊ฐ์ ๋ชจ๋ ํ์นธ ์์ index๋ก ์ฎ๊ธฐ๋ ํ๋ก์ธ์ค๊ฐ ํ์ํ์ฌ O(n)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๊ฒ ๋ฉ๋๋ค. - ํ์ง๋ง, ์ด๋ฅผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ก ๊ตฌํํ๋ ๊ฒฝ์ฐ,
head
์head?.next
๊ฐ์ ๋ถ์ฌํด์ฃผ๋ฉด ํ๋ก์ธ์ค๊ฐ ์๋ฃ๋๊ธฐ ๋๋ฌธ์ O(1)์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ง๊ฒ ๋ฉ๋๋ค.
CalculatorItemQueue
์ ๋ชจ๋ ๊ฐ์ ์ง์ฐ๋ ๊ฒฝ์ฐ
- ์ด๋ฅผ ๋ฐฐ์ด๋ก ๊ตฌํํ ๊ฒฝ์ฐ ์กด์ฌํ๋ ๋ชจ๋ ๊ฐ์ ๋ํด ์์ index ๊ฐ์ ์ง์ด ํ, ์ดํ index์ ๊ฐ์ ๋ชจ๋ ํ์นธ ์์ index๋ก ์ฎ๊ฒจ์ค ํ์ ์ญ์ ํ๋ ํ๋ก์ธ์ค๊ฐ ํ์ํ์ฌ O(n)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๊ฒ ๋ฉ๋๋ค.
- ํ์ง๋ง, ์ด๋ฅผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ก ๊ตฌํํ๋ ๊ฒฝ์ฐ์๋ ๋ฆฌ์คํธ ๋ด์
head
์nil
์ ๋ถ์ฌํด์ฃผ๋ฉด ํ๋ก์ธ์ค๊ฐ ์๋ฃ๋๊ธฐ ๋๋ฌธ์ O(1)์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ง๊ฒ ๋ฉ๋๋ค.
๊ฒฐ๋ก
- ์ด๋ฐ ์ฑ๋ฅ์ ์ธ ์ด์ ์ ์ฑ๊ธธ ์ ์๋ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ก ๊ตฌํํ๊ฒ ๋์์ต๋๋ค.
protocol CalculateItem { }
struct CalculatorItemQueue<T: CalculateItem> {
private var list: LinkedList<T> = LinkedList<T>()
...
}
- ์ด๋ฒ ํ๋ก์ ํธ๋
Queue
์ ์ฌ์ฉ์๋ก ๋ถํฐ ์ ๋ ฅ๋ฐ์ ์ฐ์ฐ์์ ํผ์ฐ์ฐ์๋ฅผ ๋ด์์ ๊ณ์ฐ ์์์ ์ ์ฅํ๋ ๋ฐฉ์์ ๊ตฌํํด์ผ๋ง ํ์ต๋๋ค. - ๊ทธ ๊ณผ์ ์์
Queue
์ ๋ด๊ธฐ๋ ์์๋CalculateItem
ํ๋กํ ์ฝ์ ์ฑํํ๋ ํ์ ๋ง ๋ด์์ง๋๋ก ์ ํํ๋ ์กฐ๊ฑด์ด ์์์ต๋๋ค. - ์ด๋ฅผ ๊ตฌํํ๊ธฐ์ํด
Queue
๊ฐ ์ ๋ค๋ฆญ ํ์ ์ ๊ฐ์ง๋๋กํ๊ณ , ๊ทธ ์ ๋ค๋ฆญ ํ์ ์ดCalculateItem
ํ๋กํ ์ฝ์ ์ฑํํ์ฌQueue
์ ๋ด๊ธธ ์ ์๋ ํ์ ์ด ์ ํ๋๋๋ก ๊ตฌํํ์์ต๋๋ค.
import XCTest
@testable import Calculator
final class FormulaTests: XCTestCase {
var sut: Formula!
override func setUpWithError() throws {
try super.setUpWithError()
let operands = CalculatorItemQueue<Double>()
let operators = CalculatorItemQueue<Operator>()
sut = Formula(operands: operands, operators: operators)
}
override func tearDownWithError() throws {
try super.tearDownWithError()
sut = nil
}
func test_0๋๋๊ธฐ0์์งํํ๋ฉด_NaN์๋ฐํํ๋์ง() {
sut.operands.enqueue(0.0)
sut.operators.enqueue(Operator.divide)
sut.operands.enqueue(0.0)
let result = sut.result()
XCTAssertTrue(result.isNaN)
}
}
- TDD๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ฝ๋๋ฅผ ์ง๋ณด๋ผ๋ ์๊ตฌ์ฌํญ์ ์งํํ๊ณ ์ ํ์ผ๋, ์ต์ํ์ง ์์ ์ ๋ป ์์ด ๊ฐ์ง ์๋ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค.
- ์ด๋ฒ ํ๋ก์ ํธ์ ๊ฒฝ์ฐ, ์๋์ ๊ฐ์ ์์๋ก ์งํํ๊ณ ์ ํ์์ต๋๋ค.
- ์๊ตฌ์ฌํญ์ ๋ง์กฑํ๊ธฐ ์ํด ์ด๋ค ํ ์คํธ๊ฐ ํ์ํ์ง๋ฅผ ์๊ฐํฉ๋๋ค.
- ๊ทธ ํ ์คํธ๋ฅผ ๋จผ์ ๊ตฌํํ ๋ค์, ๊ทธ ํ ์คํธ๋ฅผ ํต๊ณผํ๊ธฐ ์ํด ์ฝ๋๋ฅผ ๊ตฌํํฉ๋๋ค.
- ๋ฌผ๋ก ํด๋น ๊ณผ์ ์์ ํ ์คํธ๋ฅผ ๋ค์ ๊ณ ์น๋ ์ํฉ์ด ๋ฐ์ํ๊ธฐ๋ ํ๊ณ , ์ฝ๋๋ฅผ ๋จผ์ ๊ตฌํํ๊ณ ๊ทธ์ ๋ง๋ ํ ์คํธ๋ฅผ ๋ค์ ๊ตฌํํ๋ ๋ฑ, ์์๊ฐ ์ ์ง์ผ์ง์ง ์์์ง๋ง, ์ด๋ ์๋ก์ด ๊ธฐ์ ์ ๋์ ์์ ๊ฒช๋ ์ํ์ฐฉ์ค๋ผ ์๊ฐํ๊ณ ์ด๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๋ง์ ๊ฒฝํ์ ํตํด ์ต์ํด์ง๋ ๊ฒ์ด๋ผ ์๊ฐํ์์ต๋๋ค.