-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minimum 함수를 더 작은 단위의 함수로 분리하고 테스트 추가
- minimum 함수를 calculateMinimum, calculateRow, calculateRowMin, minimumIfPositiveInfinity 함수로 분리 - 각 함수는 더 작고 명확한 기능을 수행 - minimum 함수에 대한 테스트 추가 - 가독성 및 유지보수성 향상
- Loading branch information
Showing
1 changed file
with
32 additions
and
10 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,14 +1,36 @@ | ||
const minimum = (arr: number[][]) => { | ||
let result = Number.POSITIVE_INFINITY; | ||
for (let x = 0; x < arr.length; x++) { | ||
for (let y = 0; y < arr[x].length; y++) { | ||
if (result > arr[x][y]) { | ||
result = arr[x][y]; | ||
} | ||
} | ||
function minimumIfPositiveInfinity(result: number, arr: number[][], x: number, y: number) { | ||
if (result > arr[x][y]) { | ||
result = arr[x][y]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function calculateRowMin(arr: number[][], x: number, result: number) { | ||
for (let y = 0; y < arr[x].length; y++) { | ||
result = minimumIfPositiveInfinity(result, arr, x, y); | ||
} | ||
|
||
return result; | ||
return result; | ||
} | ||
|
||
function calculateRow(arr: number[][], result: number) { | ||
for (let x = 0; x < arr.length; x++) { | ||
result = calculateRowMin(arr, x, result); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
const calculateMinimum = (arr: number[][]) => { | ||
const result = Number.POSITIVE_INFINITY; | ||
|
||
return calculateRow(arr, result); | ||
}; | ||
|
||
console.log(minimum([[1, 2, 4], [1, 2, 3]])); // 결과값 : 1 | ||
describe('minimum', () => { | ||
it('두 배열의 값이 주어지면 최소값을 리턴한다.', () => { | ||
expect(calculateMinimum([[1, 2, 4], [1, 2, 3]])) | ||
.toBe(1); | ||
}); | ||
}); |