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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dolchae/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
| 29์ฐจ์‹œ| 2024.02.19 | ๋ธŒ๋ฃจํŠธํฌ์Šค | [2961 ๋„์˜์ด๊ฐ€ ๋งŒ๋“  ๋ง›์žˆ๋Š” ์Œ์‹](https://www.acmicpc.net/problem/2961) | [#109](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/109) |
| 30์ฐจ์‹œ| 2024.02.22 | DFS | [2644 ์ดŒ์ˆ˜๊ณ„์‚ฐ](https://www.acmicpc.net/problem/2644) | [#112](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/112) |
| 31์ฐจ์‹œ| 2024.02.27 | ๊ตฌํ˜„ | [13414 ์ˆ˜๊ฐ•์‹ ์ฒญ](https://www.acmicpc.net/problem/13414) | [#117](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/117) |
| 32์ฐจ์‹œ| 2024.02.29 | ๊ทธ๋ฆฌ๋”” | [2012 ๋“ฑ์ˆ˜ ๋งค๊ธฐ๊ธฐ](https://www.acmicpc.net/problem/13414) | [#119](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/119) |
| 32์ฐจ์‹œ| 2024.02.29 | ๊ทธ๋ฆฌ๋”” | [2012 ๋“ฑ์ˆ˜ ๋งค๊ธฐ๊ธฐ](https://www.acmicpc.net/problem/2012) | [#119](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/119) |
| 33์ฐจ์‹œ| 2024.03.04 | ์ •๋ ฌ | [1431 ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ](https://www.acmicpc.net/problem/1431) | [#124](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/124) |
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys
input = sys.stdin.readline

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

for _ in range(n):
serial.append(input().strip())
Comment on lines +5 to +8
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))

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


def get_number_sum(s):
number_sum = 0
for char in s:
if char.isdigit():
number_sum += int(char)
return number_sum
Comment on lines +10 to +15
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())

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


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]
Comment on lines +17 to +28
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. ๋ฌธ์ž์—ด ์‚ฌ์ „ ์ˆœ์œผ๋กœ

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


for i in serial:
print(i)

Loading