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

[python]boj1431 #66

Merged
merged 1 commit into from
Sep 19, 2023
Merged

[python]boj1431 #66

merged 1 commit into from
Sep 19, 2023

Conversation

chjcode
Copy link
Contributor

@chjcode chjcode commented Sep 7, 2023

๐Ÿ“ฃ Related Issue

close #

๐ŸŒ Background

ํŒŒ์ด์ฌ ์ •๋ ฌ๋ฌธ์ œ์—์„œ cmp_to_key()๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๊ณต์œ 
cmp_to_key()๋Š” 1,0,-1 ์„ธ ๊ฐ€์ง€ ๊ฐ’์„ ๋ฐ›๋Š”๋ฐ, 1์„ ๋ฐ›์„ ๊ฒฝ์šฐ ๋‘ ์ˆ˜์˜ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ๊ณ , 0๊ณผ -1์€ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ์ง€ ์•Š๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ํ›จ์”ฌ ๊ฐ„๋‹จํ•ด์ง„๋‹ค.

๐Ÿ‘ฉโ€๐Ÿ’ป Contents

# 1431 ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ

n = int(input())
serial = [input() for _ in range(n)]

from functools import cmp_to_key

def compare(a,b):
    len_a = len(a)
    len_b = len(b)
    num = ['0','1','2','3','4','5','6','7','8','9']
    # ์ฒซ๋ฒˆ์งธ ์กฐ๊ฑด
    if len_a > len_b:   # a๊ฐ€ ํฐ๋ฐ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ๋ฉด ํฐ ๊ฐ’์ด ๋’ค๋กœ ์˜ค๊ฒŒ ์ •๋ ฌ๋จ
        return 1
    elif len_a < len_b:
        return -1

    # ๋‘๋ฒˆ์งธ ์กฐ๊ฑด
    cnt_a = 0
    cnt_b = 0
    for i in range(len_a):
        if a[i] in num:
            cnt_a += int(a[i])
        if b[i] in num:
            cnt_b += int(b[i])
    if cnt_a > cnt_b:
        return 1
    elif cnt_a < cnt_b:
        return -1
    # ์„ธ๋ฒˆ์งธ ์กฐ๊ฑด
    if a > b:
        return 1
    else:
        return -1


serial = sorted(serial, key = cmp_to_key(compare))

for temp in serial:
    print(temp)

๐Ÿ“ฑ Screenshot

๐Ÿ“ Review Note

๐Ÿ“ฌ Reference

@chjcode chjcode requested a review from a team as a code owner September 7, 2023 01:54
@chjcode chjcode requested review from cJinu and CrimsonTheLegoBuilder and removed request for a team September 7, 2023 01:54
Copy link

@cJinu cJinu left a comment

Choose a reason for hiding this comment

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

์ž˜ ๋ณด์•˜์Šต๋‹ˆ๋‹ค!

seoul_x_hwijoonchoi/BOJ/boj1431/README.md Show resolved Hide resolved
seoul_x_hwijoonchoi/BOJ/boj1431/README.md Show resolved Hide resolved
seoul_x_hwijoonchoi/BOJ/boj1431/README.md Show resolved Hide resolved
@minjae9610 minjae9610 requested review from a team and gabalja and removed request for CrimsonTheLegoBuilder and a team September 13, 2023 02:17
seoul_x_hwijoonchoi/BOJ/boj1431/README.md Show resolved Hide resolved
seoul_x_hwijoonchoi/BOJ/boj1431/README.md Show resolved Hide resolved
seoul_x_hwijoonchoi/BOJ/boj1431/README.md Show resolved Hide resolved
seoul_x_hwijoonchoi/BOJ/boj1431/README.md Show resolved Hide resolved
@chjcode chjcode merged commit 6c58810 into main Sep 19, 2023
1 check passed
@chjcode chjcode deleted the algo/chjcode/boj1431 branch September 19, 2023 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants