Skip to content

Commit

Permalink
✨ Problem 004 Merge solution and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jobearrr committed Jul 19, 2024
1 parent b77d254 commit f45c0a7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// MedianOfTwoSortedArraysMergeSolution.swift
// LeetSwift
//
// Created by Jobert Sá on 19/07/2024.
//

import Core
import Foundation
import Problems

final class MedianOfTwoSortedArraysMergeSolution: MedianOfTwoSortedArraysDefinition {

func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
let mergedArray = (nums1 + nums2).sorted()

let mid = mergedArray.count / 2

if mergedArray.count % 2 == 1 {
return Double(mergedArray[mid])
}
return Double(mergedArray[mid - 1] + mergedArray[mid]) / 2.0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// MedianOfTwoSortedArraysMergeSolutionTests.swift
// LeetSwift
//
// Created by Jobert Sá on 19/07/2024.
//

import XCTest
@testable import Problems
@testable import Solutions
@testable import TestSupport

final class MedianOfTwoSortedArraysMergeSolutionTests: XCTestCase {

let solution: MedianOfTwoSortedArraysDefinition = MedianOfTwoSortedArraysMergeSolution()

func testSolution() {
for testData in data {
let input = testData.input

let output = solution.findMedianSortedArrays(input.nums1, input.nums2)

XCTAssertEqual(output, testData.expectedOutput)
}
}
}

extension MedianOfTwoSortedArraysMergeSolutionTests: MedianOfTwoSortedArraysTestCaseProvider { }

0 comments on commit f45c0a7

Please sign in to comment.