-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
25 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Sources/Solutions/001 - Two Sum/TwoSumBruteForceSolution.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// TwoSumBruteForceSolution.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 04/07/2024. | ||
// | ||
|
||
import Foundation | ||
import Problems | ||
|
||
final class TwoSumBruteForceSolution: TwoSumDefinition { | ||
|
||
func twoSum(_ nums: [Int], _ target: Int) -> [Int] { | ||
var dictionary: [Int: Int] = [:] | ||
|
||
for (i, num) in nums.enumerated() { | ||
if let item = dictionary[target - num] { | ||
return [item, i] | ||
} | ||
dictionary[num] = i | ||
} | ||
|
||
return [] | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Tests/SolutionsTests/001 - Two Sum/TwoSumBruteForceSolutionTests.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// TwoSumBruteForceSolutionTests.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 04/07/2024. | ||
// | ||
|
||
import XCTest | ||
@testable import Problems | ||
@testable import Solutions | ||
@testable import TestSupport | ||
|
||
final class TwoSumBruteForceSolutionTests: XCTestCase { | ||
|
||
let solution: TwoSumDefinition = TwoSumBruteForceSolution() | ||
|
||
func testSolution() { | ||
for testData in data { | ||
let input = testData.input | ||
|
||
let output = solution.twoSum(input.nums, input.target) | ||
|
||
XCTAssertEqual(output, testData.expectedOutput) | ||
} | ||
} | ||
} | ||
|
||
extension TwoSumBruteForceSolutionTests: TwoSumTestCaseProvider { } |
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
37 changes: 37 additions & 0 deletions
37
Tests/SolutionsTests/001 - Two Sum/TwoSumTestCaseProvider.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// TwoSumTestCaseProvider.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 04/07/2024. | ||
// | ||
|
||
@testable import Core | ||
@testable import TestSupport | ||
|
||
protocol TwoSumTestCaseProvider: TestCaseProviding { } | ||
|
||
extension TwoSumTestCaseProvider { | ||
|
||
func validateInput(_ input: (nums: [Int], target: Int)) -> Bool { | ||
// TODO: Complete validation | ||
input.nums.count >= 2 && | ||
input.nums.count <= 10_000 && | ||
input.target >= -1_000_000_000 && | ||
input.target <= 1_000_000_000 | ||
} | ||
|
||
var data: [TestData<(nums: [Int], target: Int), [Int]>] { [ | ||
TestData( | ||
input: ([2, 7, 11, 15], 9), | ||
expectedOutput: [0, 1] | ||
), | ||
TestData( | ||
input: ([3, 2, 4], 6), | ||
expectedOutput: [1, 2] | ||
), | ||
TestData( | ||
input: ([3, 3], 6), | ||
expectedOutput: [0, 1] | ||
) | ||
] } | ||
} |