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

33-Dolchae #124

Merged
merged 1 commit into from
Mar 8, 2024
Merged

33-Dolchae #124

merged 1 commit into from
Mar 8, 2024

Conversation

Dolchae
Copy link
Collaborator

@Dolchae Dolchae commented Mar 4, 2024

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

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

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

1์‹œ๊ฐ„

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

๋ฌธ์ œ

์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ๋ฅผ ์ •๋ ฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ •๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ฃจ์–ด์ง„๋‹ค.

  1. ๊ธธ์ด๋Š” ์งง์€ ๊ฒƒ์ด ๋จผ์ €(์•ž์—) ์˜จ๋‹ค.
  2. ๊ธธ์ด๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๊ฐ ๋ชจ๋“  ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์ด ์ž‘์€ ๊ฒƒ์ด ๋จผ์ € ์˜จ๋‹ค.(์ˆซ์ž์ธ ๊ฒƒ๋งŒ ๋”ํ•จ)
  3. 1,2๋กœ๋„ ๋น„๊ตํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ์‚ฌ์ „์ˆœ์œผ๋กœ ๋น„๊ตํ•œ๋‹ค. ์ด๋•Œ ์ˆซ์ž๊ฐ€ ์•ŒํŒŒ๋ฒณ๋ณด๋‹ค ์‚ฌ์ „์ˆœ์œผ๋กœ ๋” ์•ž์ด๋‹ค.

์ฝ”๋“œ

import sys
input = sys.stdin.readline

n = int(input().strip())
serial = []

for _ in range(n):
    serial.append(input().strip())

n์— ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ์˜ ๊ฐœ์ˆ˜, serial ๋ฆฌ์ŠคํŠธ์— ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ๋ฅผ ๋ชจ๋‘ ์ €์žฅํ•œ๋‹ค.

def get_number_sum(s):
    number_sum = 0
    for char in s:
        if char.isdigit():
            number_sum += int(char)
    return number_sum

ํ›„์˜ ์ •๋ ฌ์—์„œ ๊ฐ ๋ฒˆํ˜ธ์˜ ์ˆซ์žํ•ฉ์„ ๊ตฌํ•  ๋•Œ ์“ธ ํ•จ์ˆ˜์ด๋‹ค. ๋ฌธ์ž์—ด s๋ฅผ ๋ฐ›์•„ ๋ฌธ์ž char๊ฐ€ ์ •์ˆ˜๋ผ๋ฉด ๊ทธ ๊ฐ’์„ number_sum์— ์ €์žฅํ•œ ํ›„ ์ด ๊ฐ’์„ ๋„˜๊ธด๋‹ค.

for i in range(n-1):
    for j in range(i+1, n):
        if len(serial[i]) > len(serial[j]): #1
            serial[j], serial[i] = serial[i], serial[j]
        elif len(serial[i]) == len(serial[j]): #2
            i_total = get_number_sum(serial[i])
            j_total = get_number_sum(serial[j])
            if i_total > j_total: #3
                serial[j], serial[i] = serial[i], serial[j]
            elif i_total == j_total: #4
                if serial[i] > serial[j]:
                    serial[j], serial[i] = serial[i], serial[j]

for i in serial:
    print(i)

for๋ฌธ i๋Š” (0 ~ n-2), j๋Š” (i+1 ~ n-1)๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ •๋ ฌ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
if๋ฌธ #1์—์„œ ๋งŒ์•ฝ i๊ฐ€ j๋ณด๋‹ค ๊ธธ๋‹ค๋ฉด ๋‘˜์˜ ์œ„์น˜๋ฅผ ๋ฐ”๊พธ์–ด์ค€๋‹ค.
elif๋ฌธ #2์—์„œ i์™€ j์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์ˆซ์ž ํ•ฉ์„ ๋”ํ•ด์ค˜์•ผํ•˜๋ฏ€๋กœ get_number_sum์„ ํ˜ธ์ถœํ•ด ์ˆซ์žํ•ฉ์„ i_total, j_total์— ๊ฐ๊ฐ ์ €์žฅํ•œ๋‹ค.
if๋ฌธ #3์—์„œ i_total๋ณด๋‹ค j_total์ด ๋” ์ž‘๋‹ค๋ฉด ๋‘˜์˜ ์œ„์น˜๋ฅผ ๋ฐ”๊พธ์–ด์ค€๋‹ค.
elif๋ฌธ #4์—์„œ i_total, j_total์ด ๊ฐ™๋‹ค๋ฉด ์•ŒํŒŒ๋ฒณ ์ˆœ๊นŒ์ง€ ์‹ ๊ฒฝ์จ์•ผํ•˜๋ฏ€๋กœ j๊ฐ€ ๋” ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์•ž์ด๋ผ๋ฉด ๋‘˜์˜ ์œ„์น˜๋ฅผ ๋ฐ”๊พธ์–ด์ค€๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ serial ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

์ „์ฒด ์ฝ”๋“œ

import sys
input = sys.stdin.readline

n = int(input().strip())
serial = []

for _ in range(n):
    serial.append(input().strip())

def get_number_sum(s):
    number_sum = 0
    for char in s:
        if char.isdigit():
            number_sum += int(char)
    return number_sum

for i in range(n-1):
    for j in range(i+1, n):
        if len(serial[i]) > len(serial[j]):
            serial[j], serial[i] = serial[i], serial[j]
        elif len(serial[i]) == len(serial[j]):
            i_total = get_number_sum(serial[i])
            j_total = get_number_sum(serial[j])
            if i_total > j_total:
                serial[j], serial[i] = serial[i], serial[j]
            elif i_total == j_total:
                if serial[i] > serial[j]:
                    serial[j], serial[i] = serial[i], serial[j]

for i in serial:
    print(i)

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

์•„์ง๋„ ๋‚ด ๋ˆˆ์—๋Š” ๋ฌธ์ œ์ ์ด ๋ณด์ด์ง€ ์•Š๋Š”๋ฐ๐Ÿฅน ๋ฐ‘์˜ ์ฝ”๋“œ๊ฐ€ ์ž๊พธ j for๋ฌธ์—์„œ "TypeError: 'str' object cannot be interpreted as an integer"์—๋Ÿฌ๊ฐ€ ๋– ์„œ GPT์˜ ๋„์›€์„ ๋ฐ›์•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ๋ถ„๋“ค ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ๋‚ด์žฅํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ๋นจ๋ฆฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค..!

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

ํŒŒ์ด์ฌ ์ฝ”๋“œ๋Š” 'ํŒŒ์ด์จ๋‹‰'ํ•˜๊ฒŒ ์งœ๋Š” ๊ฒƒ ๋˜ํ•œ ์ค‘์š”ํ•œ ๋Šฅ๋ ฅ์ž…๋‹ˆ๋‹ค.

C++์ด๋‚˜ Java๊ฐ™์€ ๋‹ค๋ฅธ ์–ธ์–ด๋ฅผ ์“ฐ๋˜ ์‚ฌ๋žŒ์ด ํŒŒ์ด์ฌ์„ ์žก์œผ๋ฉด, ์ด์ „์— ์ž‘์„ฑํ•˜๋˜ ๋ฒ„๋ฆ‡์ด ๋‚จ์•„์žˆ์–ด ๊ทธ ์–ธ์–ด๋ฅผ ์“ฐ๋˜ ๋ฐฉ์‹๋Œ€๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด "ํŒŒ์ด์จ๋‹‰ํ•˜์ง€ ๋ชปํ•˜๋‹ค"๋ผ๊ณ  ์–˜๊ธฐํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ ์žฅ์ ์ธ ๊ฐ„๊ฒฐํ•˜๊ณ  ๋‚ด๋ถ€์ ์œผ๋กœ ์ตœ์ ํ™”๋œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .

ํ•œ ๋ฒˆ ์ด ๋ถ€๋ถ„๋„ ์‹œ๊ฐ„๋‚˜๋ฉด ๊ฒ€์ƒ‰ํ•ด๋ณด์‹œ๋Š” ๊ฑธ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค :)

Comment on lines +5 to +8
serial = []

for _ in range(n):
serial.append(input().strip())
Copy link
Collaborator

Choose a reason for hiding this comment

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

๋Š˜ ๋ง ํ•˜๋˜ ๊ฒƒ์ด์ง€๋งŒ,

serial = list(input().rstrip() for _ in range(n))

์œผ๋กœ ์ค„์—ฌ์“ธ ์ˆ˜ ์žˆ๊ฒ ์ฃ ?

Comment on lines +10 to +15
def get_number_sum(s):
number_sum = 0
for char in s:
if char.isdigit():
number_sum += int(char)
return number_sum
Copy link
Collaborator

Choose a reason for hiding this comment

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

def get_number_sum(s):
    return sum(int(char) for char in s if char.isdigit())

์š”๋ ‡๊ฒŒ ํ•œ ์ค„๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค :)

Comment on lines +17 to +28
for i in range(n-1):
for j in range(i+1, n):
if len(serial[i]) > len(serial[j]):
serial[j], serial[i] = serial[i], serial[j]
elif len(serial[i]) == len(serial[j]):
i_total = get_number_sum(serial[i])
j_total = get_number_sum(serial[j])
if i_total > j_total:
serial[j], serial[i] = serial[i], serial[j]
elif i_total == j_total:
if serial[i] > serial[j]:
serial[j], serial[i] = serial[i], serial[j]
Copy link
Collaborator

Choose a reason for hiding this comment

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

ํŒŒ์ด์ฌ์˜ sort๋Š” ๊ฐ•๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ •๋ ฌ ๊ธฐ์ค€์ธ key๋ฅผ ์ž˜ ์ •ํ•ด์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋‹ˆ๊นŒ์š”.

์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ 1. ๊ธธ์ด๊ฐ€ ์งง์€ ์ˆœ 2. ์ž๋ฆฟ์ˆ˜ ํ•ฉ์ด ์ ์€ ์ˆœ 3. ์‚ฌ์ „์ˆœ ์šฐ์„ ์ˆœ์œ„๋กœ ์ •๋ ฌํ•ด์•ผ ํ•˜์ž–์•„์š”? ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ

serial.sort(key=lambda x: (len(x), get_number_sum(x), x))

์ด๋ ‡๊ฒŒ ๋žŒ๋‹ค๋กœ ์ •ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. x๊ฐ€ ๊ฐ๊ฐ์˜ ์›์†Œ์ธ ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ

  1. ๊ธธ์ด๊ฐ€ ์งง์€ ์ˆœ์œผ๋กœ
  2. get_number_sum ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ(์ž๋ฆฟ์ˆ˜ ํ•ฉ)๊ฐ€ ์ž‘์€ ์ˆœ์œผ๋กœ
  3. ๋ฌธ์ž์—ด ์‚ฌ์ „ ์ˆœ์œผ๋กœ

์ •๋ ฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค :)

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

chat GPT๋ฅผ ์ž˜ ํ™œ์šฉํ•˜์‹œ๋Š” ๋ชจ์Šต์ด ๋ฉ‹์ง€์‹ญ๋‹ˆ๋‹ค!!๐Ÿ‘
์ €๋Š” ํ•ญ์ƒ ๊ตฌ๊ธ€๋ง์œผ๋กœ๋งŒ ํ•ด๊ฒฐํ•˜๋ ค ํ–ˆ๋Š”๋ฐ, ๋ณธ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!๐Ÿ˜Š
๊ทธ๋ฆฌ๊ณ  ์ €๋„ ๋‚ด์žฅํ•จ์ˆ˜ ์ž˜ ์•ˆ์“ฐ๋Š”๋ฐ ์“ฐ๋ ค๊ณ  ๋…ธ๋ ฅํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.
์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!!

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

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

๋ฌธ์ž์—ด์„ ํŠน์ • ์กฐ๊ฑด์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๊ฑด ์ž๋ฃŒ๊ตฌ์กฐ ๊ฐ•์˜ ๋•Œ์—๋„ ๋งŽ์ด ํ–ˆ์ง€๋งŒ ์—ฌ์ „ํžˆ ์ ์‘์ด ์•ˆ๋˜๋„ค์š” ๐Ÿ˜ญ
์ค‘๊ฐ„์— ์ด์ƒํ•˜๊ฒŒ ๊ณ„์† ์ค„๋ฐ”๊ฟˆ์ด ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด๋ฉฐ ๊ณ ์น˜๋‹ค๊ฐ€ ํ•œ์ฐธ ํ—ค๋งธ์–ด์š”...
๊ทธ๋ž˜๋„ ์–ด์ฐŒ์ €์ฐŒ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค..!

์ด๋ฒˆ PR๋„ ์ˆ˜๊ณ ํ•˜์…จ์–ด์š”!!

์ฝ”๋“œ
input_ = open(0).readline

from collections import defaultdict

words = [input_().strip() for _ in range(int(input_()))]

len_words = defaultdict(list)

for w in words:
    len_words[len(w)].append(w)

for same_len in sorted(len_words):
    cnt_words = defaultdict(list)
    for w in len_words[same_len]:
        w_sum = sum(int(p) for p in w if p.isdigit())
        cnt_words[w_sum].append(w)

    for i in sorted(cnt_words):
        for j in sorted(cnt_words[i]):
            print(j, end=("\n" if j else ""))

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