개발👩‍💻/기타

geeksforgeeks contribute 하기

gigibean 2022. 9. 17. 23:49
728x90

🔒 문제

geeksforgeeks 사이트에서 그리디 알고리즘 관련 아티클을 읽다가 뭔가 이상한 부분을 발견했다.

 

https://www.geeksforgeeks.org/minimum-product-subset-array/

 

Minimum product subset of an array - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

위 링크의 JS Implementation 부분 코드이다.

<script>
 
// Javascript program to find maximum
// product of a subset.
function minProductSubset(a, n)
{
    if (n == 1)
        return a[0];

    let negmax = Number.MIN_VALUE;
    let posmin = Number.MIN_VALUE;
    let count_neg = 0, count_zero = 0;
    let product = 1;
 
    for(let i = 0; i < n; i++)
    {
         
        // If number is zero, count it
        // but dont multiply
        if (a[i] == 0)
        {
            count_zero++;
            continue;
        }
 
        // Count the negative numbers
        // and find the max negative number
        if (a[i] < 0)
        {
            count_neg++;
            negmax = Math.max(negmax, a[i]);
        }
 
        // Find the minimum positive number
        if (a[i] > 0 && a[i] < posmin)
        {
            posmin = a[i];
        }
 
        product *= a[i];
    }
 
    // If there are all zeroes
    // or zero is present but no
    // negative number is present
    if (count_zero == n || (count_neg == 0 &&
        count_zero > 0))
        return 0;
 
    // If there are all positive
    if (count_neg == 0)
        return posmin;
 
    // If there are even number except
    // zero of negative numbers
    if (count_neg % 2 == 0 && count_neg != 0)
    {
 
        // Otherwise result is product of
        // all non-zeros divided by maximum
        // valued negative.
        product = parseInt(product / negmax, 10);
    }
    return product;
}
 
// Driver code
let a = [ -1, -1, -2, 4, 3 ];
let n = 5;
 
document.write(minProductSubset(a, n));
 
</script>

여기에서 이상한 부분이 있었다.

 

let negmax = Number.MIN_VALUE;
let posmin = Number.MIN_VALUE;

 

위와 같이 가장 큰 음수와 가장 작은 양수를 찾기위한 변수를 초기화 하는 부분이었는데,

 

...
if (a[i] < 0)
{
    count_neg++;
    negmax = Math.max(negmax, a[i]); // 여기
}

// Find the minimum positive number
if (a[i] > 0 && a[i] < posmin)
{
    posmin = a[i]; // 여기
} 

...

 

표시된 부분에서 사용하는 것이기 때문에 예제에서는 사용되지 않는 값이지만,

(예제에서는 postmin과 nexmax 값을 사용하지 않는 [-1, -1, -2, 4, 3] 이라는 값을 사용하기 때문에)

다른 예제를 넣어보면 원치않는 값이 나올 것이다.

 

왜냐하면, 

 

가장 큰 음수를 찾기 위해서는 음수끼리 비교해야 원하는 값을 도출할 수 있고,

가장 작은 양수를 찾기 위해서는 앞에 수보다 작은 수를 찾아야 하기 때문이다.

 

그러나 Number.MIN_VALUE로 두 개의 변수를 초기화 한다면 둘다 어떤 값이 들어와도 초기값을 리턴하게 된다.

 

Number.MIN_VALUE는 자바스크립트에서 양수 중 가장 작은 값을 나타낸다.

그 말은 즉슨,

negmax에서 Number.MIN_VALUE와 어떠한 음수를 비교해도 Number.MIN_VALUE가 가장 크며,

posmin에서 Number.MIN_VALUE와 어떠한 양수를 비교해도 Number.MIN_VALUE가 가장 작는 것을 의미한다.

 

이를 제대로 동작하게 하기 위해서는 아래와 같이 수정해주어야 한다.

negmax = Number.NEGATIVE_INFINITY

posmin = Number.MAX_VALUE

 

수정된 코드 🧩

<script>
 
// Javascript program to find maximum
// product of a subset.
function minProductSubset(a, n)
{
    if (n == 1)
        return a[0];
 
    // Find count of negative numbers,
    // count of zeros, maximum valued
    // negative number, minimum valued
    // positive number and product of
    // non-zero numbers
    let negmax = Number.MAX_VALUE;
    let posmin = Number.NEGATIVE_INFINITY;
    let count_neg = 0, count_zero = 0;
    let product = 1;
 
    for(let i = 0; i < n; i++)
    {
         
        // If number is zero, count it
        // but dont multiply
        if (a[i] == 0)
        {
            count_zero++;
            continue;
        }
 
        // Count the negative numbers
        // and find the max negative number
        if (a[i] < 0)
        {
            count_neg++;
            negmax = Math.max(negmax, a[i]);
        }
 
        // Find the minimum positive number
        if (a[i] > 0 && a[i] < posmin)
        {
            posmin = a[i];
        }
 
        product *= a[i];
    }
 
    // If there are all zeroes
    // or zero is present but no
    // negative number is present
    if (count_zero == n || (count_neg == 0 &&
        count_zero > 0))
        return 0;
 
    // If there are all positive
    if (count_neg == 0)
        return posmin;
 
    // If there are even number except
    // zero of negative numbers
    if (count_neg % 2 == 0 && count_neg != 0)
    {
 
        // Otherwise result is product of
        // all non-zeros divided by maximum
        // valued negative.
        product = parseInt(product / negmax, 10);
    }
    return product;
}
 
// Driver code
let a = [ -1, -1, -2, 4, 3 ];
let n = 5;
 
document.write(minProductSubset(a, n));
 
</script>

수정 & 컨트리뷰트하기💪

문서 맨 아래 보면 아래와 같이 Improve Article 이라고 하는 버튼과 문서 상단에 펜모양 버튼 둘 중 하나를 클릭하면 수정 페이지로 이동한다.

그러면 아래와 같은 write 페이지로 이동하게 되고 

수정 후에는 Submit for Review 를 클릭한다

 

그러면 Reason to Improve 라는 창이 뜨게 된다.

 

예를 들어 

이런 창에서 무엇에 기여하는지 어떤 부분을 생성 혹은 수정했는지에 대해 적어주고 type을 맞는 부분을 찾아 변경해주고 Submit for Review 를 누르면 상태가 pending으로 변한다.

 

담당자가 확인 후 변경 사항을 받아준다면 컨트리뷰트에 성공이다.

 

Submit에 대한 자세한 사항은 아래 링크를 참고하면 된다.

 

https://write.geeksforgeeks.org/how-to-write/

 

Write

 

write.geeksforgeeks.org

 

결과 🕶

이렇게 해당 부분을 pr한 후 심사를 거쳐 아래와 같이 수정된 부분이 채택되어 퍼블리시된다.

원래 해당 아티클에 JS 코드에 // This code is contributed by gigibean.  이 있어야 하는데 내가 그 주석을 추가하지 않고 pr을 올려서 해당 부분이 없는게 좀 아쉽다🥲

느낀 점💡

이와 같이 개발 오픈 커뮤니티에 기여해보았다.

깃헙 오픈 소스 라이브러리 중 내가 해결한 부분을 관련 이슈 댓글에 적어본 적은 있었지만, 

이렇게 내가 직접 수정해본 다는 게 의미 있던 것 같다.

오픈 소스, 커뮤니티에 기여한다는 것이 뭔가 어렵고 크게 느껴졌는데 이렇게 사소한 부분이라도 기여함으로써 다른 개발자들이 참고할 때 조금이나마 불편함을 덜어줄 수도 있을 것이라는 생각에 뿌듯했다.

 

반응형