Skip to content

Commit

Permalink
🚀 Two Sum Brute Force solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jobearrr committed Jul 4, 2024
1 parent b1d58a8 commit 712dafe
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 25 deletions.
25 changes: 25 additions & 0 deletions Sources/Solutions/001 - Two Sum/TwoSumBruteForceSolution.swift
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 []
}
}
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 { }
26 changes: 1 addition & 25 deletions Tests/SolutionsTests/001 - Two Sum/TwoSumSolutionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,4 @@ final class TwoSumSolutionTests: XCTestCase {
}
}

extension TwoSumSolutionTests: TestCaseProviding {

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]
)
] }
}
extension TwoSumSolutionTests: TwoSumTestCaseProvider { }
37 changes: 37 additions & 0 deletions Tests/SolutionsTests/001 - Two Sum/TwoSumTestCaseProvider.swift
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]
)
] }
}

0 comments on commit 712dafe

Please sign in to comment.