-
Notifications
You must be signed in to change notification settings - Fork 2
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
33-Dolchae #124
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ด์ฌ ์ฝ๋๋ 'ํ์ด์จ๋'ํ๊ฒ ์ง๋ ๊ฒ ๋ํ ์ค์ํ ๋ฅ๋ ฅ์ ๋๋ค.
C++์ด๋ Java๊ฐ์ ๋ค๋ฅธ ์ธ์ด๋ฅผ ์ฐ๋ ์ฌ๋์ด ํ์ด์ฌ์ ์ก์ผ๋ฉด, ์ด์ ์ ์์ฑํ๋ ๋ฒ๋ฆ์ด ๋จ์์์ด ๊ทธ ์ธ์ด๋ฅผ ์ฐ๋ ๋ฐฉ์๋๋ก ์ฝ๋๋ฅผ ์์ฑํ๊ณค ํฉ๋๋ค. ๊ทธ๋ฌ๋ฉด "ํ์ด์จ๋ํ์ง ๋ชปํ๋ค"๋ผ๊ณ ์๊ธฐํฉ๋๋ค. ํ์ด์ฌ์ ์ฅ์ ์ธ ๊ฐ๊ฒฐํ๊ณ ๋ด๋ถ์ ์ผ๋ก ์ต์ ํ๋ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ด์ฃ .
ํ ๋ฒ ์ด ๋ถ๋ถ๋ ์๊ฐ๋๋ฉด ๊ฒ์ํด๋ณด์๋ ๊ฑธ ์ถ์ฒํฉ๋๋ค :)
serial = [] | ||
|
||
for _ in range(n): | ||
serial.append(input().strip()) |
There was a problem hiding this comment.
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))
์ผ๋ก ์ค์ฌ์ธ ์ ์๊ฒ ์ฃ ?
def get_number_sum(s): | ||
number_sum = 0 | ||
for char in s: | ||
if char.isdigit(): | ||
number_sum += int(char) | ||
return number_sum |
There was a problem hiding this comment.
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())
์๋ ๊ฒ ํ ์ค๋ก ๊ฐ๋จํ๊ฒ ์ค์ผ ์ ์๋ต๋๋ค :)
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] |
There was a problem hiding this comment.
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๊ฐ ๊ฐ๊ฐ์ ์์์ธ ๋ฌธ์์ด์ ์๋ฏธํ๋ฏ๋ก
- ๊ธธ์ด๊ฐ ์งง์ ์์ผ๋ก
- get_number_sum ํจ์๋ฅผ ํธ์ถํ ๊ฒฐ๊ณผ(์๋ฆฟ์ ํฉ)๊ฐ ์์ ์์ผ๋ก
- ๋ฌธ์์ด ์ฌ์ ์์ผ๋ก
์ ๋ ฌํ๊ฒ ๋ฉ๋๋ค :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chat GPT๋ฅผ ์ ํ์ฉํ์๋ ๋ชจ์ต์ด ๋ฉ์ง์ญ๋๋ค!!๐
์ ๋ ํญ์ ๊ตฌ๊ธ๋ง์ผ๋ก๋ง ํด๊ฒฐํ๋ ค ํ๋๋ฐ, ๋ณธ๋ฐ์ ์ ์๋๋ก ํ๊ฒ ์ต๋๋ค!๐
๊ทธ๋ฆฌ๊ณ ์ ๋ ๋ด์ฅํจ์ ์ ์์ฐ๋๋ฐ ์ฐ๋ ค๊ณ ๋
ธ๋ ฅํด์ผ๊ฒ ์ต๋๋ค.
์๊ณ ํ์
จ์ต๋๋ค!!
There was a problem hiding this 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 ""))
๐ ๋ฌธ์ ๋งํฌ
1431 ์๋ฆฌ์ผ ๋ฒํธ
โ๏ธ ์์๋ ์๊ฐ
1์๊ฐ
โจ ์๋ ์ฝ๋
๋ฌธ์
์๋ฆฌ์ผ ๋ฒํธ๋ฅผ ์ ๋ ฌํ๋ ํ๋ก๊ทธ๋จ์ ๊ตฌํํ๋ ๋ฌธ์ ์ด๋ค. ์ ๋ ฌ์ ๋ค์๊ณผ ๊ฐ์ด ์ด๋ฃจ์ด์ง๋ค.
์ฝ๋
n์ ์๋ฆฌ์ผ ๋ฒํธ์ ๊ฐ์, serial ๋ฆฌ์คํธ์ ์๋ฆฌ์ผ ๋ฒํธ๋ฅผ ๋ชจ๋ ์ ์ฅํ๋ค.
ํ์ ์ ๋ ฌ์์ ๊ฐ ๋ฒํธ์ ์ซ์ํฉ์ ๊ตฌํ ๋ ์ธ ํจ์์ด๋ค. ๋ฌธ์์ด s๋ฅผ ๋ฐ์ ๋ฌธ์ char๊ฐ ์ ์๋ผ๋ฉด ๊ทธ ๊ฐ์ number_sum์ ์ ์ฅํ ํ ์ด ๊ฐ์ ๋๊ธด๋ค.
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 ๋ฆฌ์คํธ๋ฅผ ์ถ๋ ฅํด์ค๋ค.
์ ์ฒด ์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์์ง๋ ๋ด ๋์๋ ๋ฌธ์ ์ ์ด ๋ณด์ด์ง ์๋๋ฐ๐ฅน ๋ฐ์ ์ฝ๋๊ฐ ์๊พธ j for๋ฌธ์์ "TypeError: 'str' object cannot be interpreted as an integer"์๋ฌ๊ฐ ๋ ์ GPT์ ๋์์ ๋ฐ์๋ค. ๊ทธ๋ฆฌ๊ณ ๋ค๋ฅธ ๋ถ๋ค ์ฝ๋๋ฅผ ๋ณด๋ ๋ด์ฅํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ๋นจ๋ฆฌ ์ฒ๋ฆฌํ ์ ์๋ ๊ฒ ๊ฐ๋ค..!