Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5-honggukang0623 #23

Merged
merged 2 commits into from
Jun 27, 2024
Merged

5-honggukang0623 #23

merged 2 commits into from
Jun 27, 2024

Conversation

Ghdrn1399
Copy link
Contributor

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://school.programmers.co.kr/learn/courses/30/lessons/120863?language=javascript

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ „์ฒด์ฝ”๋“œ

function solution(polynomial) {
    const values = polynomial.split(' + ');
    console.log("values : ", values);
    let x = 0;
    let c = 0;
    for(let i=0;i<values.length;i++) {
        const item = values[i];
        if(item[item.length-1] !== 'x') {
            c += Number(item);
        }
        else {
            // x์ผ๋•Œ
            const num = item.split('x')[0];
            if(num === '') {
                x += 1;
            } else {
                x += Number(num)
            }
        }
    }
    let answer = '';
    if(x === 1) {
        answer += 'x';
    }
    if(x > 1) {
        answer += `${x}x`;
    }
    
    if(x === 0 && c > 0) {
        answer += c;
    }
    else if(c > 0) {
        answer += ` + ${c}`;
    }
    return answer;
}

๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ๊บผ๋‚ด์„œ x์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

 for(let i=0;i<values.length;i++) {
        const item = values[i];
        if(item[item.length-1] !== 'x') {
            c += Number(item);
        }
        else {
            // x์ผ๋•Œ
            const num = item.split('x')[0];

x๊ฐ€ 1์ผ๋•Œ, 1๋ณดํƒ€ ํด ๋•Œ, x๊ฐ€ 0์ด๊ณ  ์ƒ์ˆ˜๊ฐ€ 0๋ณด๋‹ค ํด ๋–„, ์ƒ์ˆ˜๊ฐ€ 0๋ณด๋‹ค ํด๋•Œ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‚˜๋ˆ ์„œ
์ถœ๋ ฅํ˜•์‹์— ๋งž๊ฒŒ return ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

let answer = '';
    if(x === 1) {
        answer += 'x';
    }
    if(x > 1) {
        answer += `${x}x`;
    }
    
    if(x === 0 && c > 0) {
        answer += c;
    }
    else if(c > 0) {
        answer += ` + ${c}`;
    }
    return answer;

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์‚ฌ์ดํŠธ์— ์žˆ๋Š” ์‰ฌ์šด ๋‚œ์ด๋„ ๋ฌธ์ œ์˜€์ง€๋งŒ ์ค‘๊ฐ„๊ณ ์‚ฌ, ์ค‘๊ฐ„๋ฐœํ‘œ ๋“ฑ ์˜ค๋žœ๋งŒ์— ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€๋ ค๊ณ  ํ•˜๋‹ˆ ์‹œ๊ฐ„์ด ์ข€ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”. ์•ž์œผ๋กœ๋„ ๊พธ์ค€ํžˆ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๊ฒ ์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@pu2rile pu2rile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ง„์งœ ๊ฐ์ด ์ค‘์š”ํ•œ ๊ฒƒ ๊ฐ™์•„์š”... ๊ทธ๋ž˜์„œ ์•Œ๊ณ ๋ฆฌ๋“œ๋ฏธ๋ฅผ ํ•˜๊ณ  ์žˆ๋Š” ๊ฑด๋ฐ... ์ž์•„์„ฑ์ฐฐ ํ•˜๊ฒŒ ๋˜๋„ค์š” ใ…‹ใ…‹ ์ด์ œ ์‹œํ—˜๋„ ๋๋‚ฌ์œผ๋‹ˆ ๊ฐ™์ด ๊พธ์ค€ํžˆ ๋˜ ํ’€์–ด ๋ด์š”! pr ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค~

Copy link
Collaborator

@suhyun113 suhyun113 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋‹คํ•ญ์‹์„ ์ •๋ฆฌํ•ด ๋™๋ฅ˜ํ•ญ๋ผ๋ฆฌ ๋”ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ๋„ค์š”!
์ €๋Š” ํŒŒ์ด์ฌ์œผ๋กœ ์ด ๋ฌธ์ œ๋ฅผ ํ•œ ๋ฒˆ ํ’€์–ด๋ดค์–ด์š”!!

def solution(polynomial):
    # ๊ณ„์ˆ˜ ์ดˆ๊ธฐํ™”
    x_coef = 0
    constant = 0

    # ๋ง์…ˆ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹คํ•ญ์‹์„ + ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌ
    terms = polynomial.split(' + ')

 
    for term in terms:
        if 'x' in term:
            if term == 'x':
                    x_coef += 1 # x๊ฐ€ ์žˆ๋Š”๋ฐ x๋ผ๋ฉด 1๋กœ ๋ฐ”๊พธ๊ธฐ
            else:  
                    x_coef += int(term.replace('x', '')) # x๊ฐ€ ์žˆ๋Š”๋ฐ ๊ณ„์ˆ˜๋ผ๋ฉด ๊ณ„์ˆ˜๋งŒ ๋‚จ๊ธฐ๊ธฐ
        else : 
            constant += int(term) # ์ƒ์ˆ˜๋ผ๋ฉด ๊ทธ ์ˆซ์ž 

    result = []

    # xํ•ญ ์ •๋ฆฌ
    if x_coef > 0:
        if x_coef == 1:
            result.append('x')
        else:
            result.append(f'{x_coef}x') # 1์ด๋ฉด ๊ณ„์ˆ˜ ์ƒ๋žต

    # ์ƒ์ˆ˜ํ•ญ ์ •๋ฆฌ
    if constant > 0:
        result.append(str(constant))

    answer = ' + '.join(result) 
    return answer

@tgyuuAn tgyuuAn merged commit f85a3f1 into main Jun 27, 2024
@tgyuuAn tgyuuAn deleted the 5-honggukang0623 branch June 27, 2024 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants