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

22-Hwangyerin #99

Merged
merged 2 commits into from
Mar 19, 2024
Merged

22-Hwangyerin #99

merged 2 commits into from
Mar 19, 2024

Conversation

Hwangyerin
Copy link
Collaborator

@Hwangyerin Hwangyerin commented Mar 13, 2024

πŸ”— 문제 링크

Baekjoon | νŒ°λ¦°λ“œλ‘¬ λ§Œλ“€κΈ°

문제
μž„ν•œμˆ˜μ™€ μž„λ¬ΈλΉˆμ€ μ„œλ‘œ μ‚¬λž‘ν•˜λŠ” 사이이닀.
μž„ν•œμˆ˜λŠ” μ„Έμƒμ—μ„œ νŒ°λ¦°λ“œλ‘¬μΈ λ¬Έμžμ—΄μ„ λ„ˆλ¬΄ μ’‹μ•„ν•˜κΈ° λ•Œλ¬Έμ—, λ‘˜μ˜ 백일을 κΈ°λ…ν•΄μ„œ μž„λ¬ΈλΉˆμ€ νŒ°λ¦°λ“œλ‘¬μ„ μ„ λ¬Όν•΄μ£Όλ €κ³  ν•œλ‹€.
μž„λ¬ΈλΉˆμ€ μž„ν•œμˆ˜μ˜ μ˜μ–΄ μ΄λ¦„μœΌλ‘œ νŒ°λ¦°λ“œλ‘¬μ„ λ§Œλ“€λ €κ³  ν•˜λŠ”λ°, μž„ν•œμˆ˜μ˜ μ˜μ–΄ μ΄λ¦„μ˜ μ•ŒνŒŒλ²³ μˆœμ„œλ₯Ό 적절히 λ°”κΏ”μ„œ νŒ°λ¦°λ“œλ‘¬μ„ λ§Œλ“€λ €κ³  ν•œλ‹€.
μž„λ¬ΈλΉˆμ„ 도와 μž„ν•œμˆ˜μ˜ μ˜μ–΄ 이름을 νŒ°λ¦°λ“œλ‘¬μœΌλ‘œ λ°”κΎΈλŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

μž…λ ₯
첫째 쀄에 μž„ν•œμˆ˜μ˜ μ˜μ–΄ 이름이 μžˆλ‹€. μ•ŒνŒŒλ²³ λŒ€λ¬Έμžλ‘œλ§Œ 된 μ΅œλŒ€ 50κΈ€μžμ΄λ‹€.

좜λ ₯
첫째 쀄에 문제의 정닡을 좜λ ₯ν•œλ‹€. λ§Œμ•½ λΆˆκ°€λŠ₯ν•  λ•ŒλŠ” "I'm Sorry Hansoo"λ₯Ό 좜λ ₯ν•œλ‹€. 정닡이 μ—¬λŸ¬ 개일 κ²½μš°μ—λŠ” μ‚¬μ „μˆœμœΌλ‘œ μ•žμ„œλŠ” 것을 좜λ ₯ν•œλ‹€.

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

40λΆ„

✨ μˆ˜λ„ μ½”λ“œ

νŒ°λ¦°λ“œλ‘¬μ΄ μ•ˆλ˜λŠ” 경우

  1. ν™€μˆ˜κ°œμΈ μ•ŒνŒŒλ²³μ΄ λ‘κ°œ 이상인 경우(AABBBCCC)

νŒ°λ¦°λ“œλ‘¬μ΄ λ˜λŠ” 경우

  1. 주어진 λ¬Έμžμ—΄μ˜ μ•ŒνŒŒλ²³μ΄ λͺ¨λ‘ 짝수개인 경우(AABBCC)
  2. 주어진 λ¬Έμžμ—΄μ˜ μ•ŒνŒŒλ²³ 쀑 ν•˜λ‚˜μ˜ μ•ŒνŒŒλ²³μ΄ ν™€μˆ˜κ³  λ‚˜λ¨Έμ§€κ°€ λͺ¨λ‘ 짝수인 경우(AAABB)

즉, νŒ°λ¦°λ“œλ‘¬μ΄ 될 수 μžˆλŠ” μ˜μ–΄ 이름인지 μ•ŒκΈ° μœ„ν•΄μ„œλŠ” 각 μ•ŒνŒŒλ²³μ˜ 개수λ₯Ό μ•Œμ•„μ•Όν•œλ‹€.
↳ Counter ν•¨μˆ˜ μ‚¬μš©

from collections import Counter
count_name = dict(sorted(Counter(name).items()))

좜λ ₯ 쑰건을 보면 λ‹€μŒκ³Ό 같이 μ£Όμ–΄μ Έ μžˆλ‹€.

정닡이 μ—¬λŸ¬ 개일 κ²½μš°μ—λŠ” μ‚¬μ „μˆœμœΌλ‘œ μ•žμ„œλŠ” 것을 좜λ ₯ν•œλ‹€.

Counter(name).items() ν•¨μˆ˜λ‘œ λ§Œλ“€μ–΄μ§„ 리슀트의 μš”μ†ŒλŠ” νŠœν”Œ ν˜•νƒœλ‘œ μ•ŒνŒŒλ²³(key)κ³Ό μ•ŒνŒŒλ²³μ˜ 개수(value)둜 이루어져 μžˆλ‹€.
dict_items([('A', 5), ('B', 4), ('C', 3)])
이후 μ½”λ“œμ—μ„œ 계속 key와 valueλ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ”•μ…”λ„ˆλ¦¬λ‘œ λ°”κΏ”μ€˜μ•Όν•œλ‹€.
κ·Έλž˜μ„œ sorted둜 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ 정렬을 ν•΄μ€€ λ’€ λ‹€μ‹œ λ”•μ…”λ„ˆλ¦¬λ‘œ νƒ€μž…μ„ λ°”κΏ”μ£Όμ—ˆλ‹€.



전에 μ–ΈκΈ‰ν–ˆλ˜ 것과 같이 λ¬Έμžμ—΄μ— ν™€μˆ˜κ°œμΈ μ•ŒνŒŒλ²³μ˜ κ°œμˆ˜κ°€ λ‘κ°œ 이상일 λ•Œμ—λŠ” νŒ°λ¦°λ“œλ‘¬μ΄ 될 수 μ—†λ‹€.

  1. μš°λ¦¬λŠ” λ¬Έμžμ—΄μ— ν™€μˆ˜ 개인 μ•ŒνŒŒλ²³μ˜ κ°œμˆ˜κ°€ 두 개 이상인지 μ•Œμ•„λ³΄μ•„μ•Ό ν•œλ‹€.
  2. ν™€μˆ˜κ°œμΈ μ•ŒνŒŒλ²³μ˜ κ°œμˆ˜κ°€ ν•˜λ‚˜μΌ κ²½μš°μ—λŠ” νŒ°λ¦°λ“œλ‘¬μ΄ 될 수 μžˆμœΌλ‹ˆ ν™€μˆ˜ 개인 μ•ŒνŒŒλ²³μ΄ 무엇인지도 μ•Œμ•„μ•Ό ν•œλ‹€.
    key: μ•ŒνŒŒλ²³, value: μ•ŒνŒŒλ²³ 개수
# ν™€μˆ˜ 개인 μ•ŒνŒŒλ²³ 수 κ΅¬ν•˜κΈ° 그리고 ν™€μˆ˜ 개인 μ•ŒνŒŒλ²³ κ΅¬ν•˜κΈ°
for key, value in count_name.items():
    if value%2==1:
        odd += 1
        odd_alphabet = key

λ§Œμ•½ ν™€μˆ˜κ°œμ˜ μ•ŒνŒŒλ²³μ΄ λ‘κ°œ 이상(odd>1)이라면
I'm Sorry Hansoo 좜λ ₯
ν™€μˆ˜κ°œμ˜ μ•ŒνŒŒλ²³μ΄ μ—†κ±°λ‚˜ 1개라면 λ‹€μŒκ³Ό 같이 좜λ ₯ν•œλ‹€.

for key, value in count_name.items():
            answer += (key*(value//2))
    print(answer + odd_alphabet + answer[::-1])

ν”Όλ³΄λ‚˜μΉ˜-μˆ˜μ—΄-005
β–² ν™€μˆ˜κ°œμ˜ μ•ŒνŒŒλ²³μ΄ ν•˜λ‚˜μΌ 경우



μ΅œμ’…μ½”λ“œ

from collections import Counter

odd = 0 # ν™€μˆ˜ 개인 μ•ŒνŒŒλ²³ 수
odd_alphabet = '' # ν™€μˆ˜ 개인 μ•ŒνŒŒλ²³
answer = ''

# μ˜μ–΄μ΄λ¦„
name = input() 

# 각 μ•ŒνŒŒλ²³ 개수 κ΅¬ν•˜κ³  μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬
count_name = dict(sorted(Counter(name).items())) 

# ν™€μˆ˜κ°œμΈ μ•ŒνŒŒλ²³ 수 κ΅¬ν•˜κΈ° 그리고 ν™€μˆ˜κ°œμΈ μ•ŒνŒŒλ²³ κ΅¬ν•˜κΈ°
for key, value in count_name.items():
    if value%2==1:
        odd += 1
        odd_alphabet = key

# λ§Œμ•½ ν™€μˆ˜κ°œμΈ μ•ŒνŒŒλ²³μ΄ 2개 이상이라면 νŒ°λ¦°λ“œλ‘¬μ„ λ§Œλ“€ 수 μ—†μŒ
if odd > 1:
    print("I'm Sorry Hansoo")
else:
    for key, value in count_name.items():
            answer += (key*(value//2))
    print(answer + odd_alphabet + answer[::-1])

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

λ§ˆμΈν¬λž˜ν”„νŠΈλŠ” λ‹€μŒ μ°¨μ‹œμ— 올릴 μ˜ˆμ •μž…λ‹ˆλ‹€πŸ₯²

@avocado-13
Copy link
Member

낯읡은 이름이 !!

@Munbin-Lee
Copy link
Member

예?

@Hwangyerin Hwangyerin marked this pull request as ready for review March 14, 2024 01:14
Comment on lines +14 to +25
for key, value in count_name.items():
if value%2==1:
odd += 1
odd_alphabet = key

# λ§Œμ•½ ν™€μˆ˜κ°œμΈ μ•ŒνŒŒλ²³μ΄ 2개 이상이라면 νŒ°λ¦°λ“œλ‘¬μ„ λ§Œλ“€ 수 μ—†μŒ
if odd > 1:
print("I'm Sorry Hansoo")
else:
for key, value in count_name.items():
answer += (key*(value//2))
print(answer + odd_alphabet + answer[::-1])
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

for-else 문을 μ‚¬μš©ν•˜μ—¬ ν’€ μˆ˜λ„ μžˆκ² λ„€μš”!

for key, value in count_name.items():
    if value%2==1:
        odd += 1
        if odd>1:
            print("I'm Sorry Hansoo")
            break
        odd_alphabet = key
    answer += (key*(value//2))
else:       
    print(answer+odd_alphabet+answer[::-1])

Copy link
Collaborator

Choose a reason for hiding this comment

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

κ·ΈλŸΌμš”~

@Hwangyerin
Copy link
Collaborator Author

예?

예쁜 μ‚¬λž‘ with ν•œμˆ˜..

Comment on lines +8 to +11
name = input()

# 각 μ•ŒνŒŒλ²³ 개수 κ΅¬ν•˜κ³  μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬
count_name = dict(sorted(Counter(name).items()))
Copy link
Collaborator

Choose a reason for hiding this comment

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

1λ…„μ „μ˜ μ €λŠ” μΉ΄μš΄ν„°λ₯Ό μ“Έ 쀄 λͺ°λžμ—ˆλ„€μš”
μ΄λ ‡κ²Œλ‚˜ κ°„λ‹¨ν•˜κ²Œ 쀄여버릴 수 μžˆλ‹€λ‹ˆ ...

# 1λ…„μ „μ˜ μ •μ€μ½”λ“œ 
#[('A', 6), ('V', 6), ('C', 1)] ν˜•νƒœλ‘œ
letters = []
for letter in name_dic:
    letters.append((letter,name_dic[letter]))
#μ •λ ¬ λ¨Όμ €
letters.sort()

Comment on lines +14 to +25
for key, value in count_name.items():
if value%2==1:
odd += 1
odd_alphabet = key

# λ§Œμ•½ ν™€μˆ˜κ°œμΈ μ•ŒνŒŒλ²³μ΄ 2개 이상이라면 νŒ°λ¦°λ“œλ‘¬μ„ λ§Œλ“€ 수 μ—†μŒ
if odd > 1:
print("I'm Sorry Hansoo")
else:
for key, value in count_name.items():
answer += (key*(value//2))
print(answer + odd_alphabet + answer[::-1])
Copy link
Collaborator

Choose a reason for hiding this comment

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

저도 반μͺ½μ„ λ¨Όμ € λ§Œλ“€μ–΄μ€˜λ²„λ¦¬κ³  좜λ ₯ν•  λ•Œ [::-1]λ₯Ό μ‚¬μš©ν•˜μ—¬ ν•©μ³μ£Όμ—ˆμ—ˆλŠ”λ°
μ•„μ˜ˆ oddλ₯Ό ν•¨κ»˜ νŒλ‹¨ν•΄μ€˜λ²„λ¦¬λŠ” 것도 μ’‹κ² λ„€μš” ~
쒋은 μ½”λ“œ 또 λ°°μ›Œκ°‘λ‹ˆλ‹€ :) κ΅Ώκ΅Ώ bbb

for letter in letters:
    # κ°œμˆ˜κ°€ ν™€μˆ˜μΈ μ•ŒνŒŒλ²³μ΄ μ—¬λŸ¬κ°œλ©΄ break
    if letter[1]%2 != 0:
        break_cnt += 1
        #ν™€μˆ˜μΈ μ•ŒνŒŒλ²³μ΄ 센터에 였게 될 것
        center += letter[0]
    # κ°™μ€κ²Œ 2개 이상이면
    # 반μͺ½λ§Œ 일단 λ§Œλ“€μ–΄μ£Όμž
    Palindrome += letter[0]*(letter[1]//2)
if break_cnt > 1:
    print("I'm Sorry Hansoo")
else:
    print(Palindrome + center + Palindrome[::-1])

Copy link
Member

@janghw0126 janghw0126 left a comment

Choose a reason for hiding this comment

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

저도 μ˜ˆλ¦°λ‹˜κ³Ό λΉ„μŠ·ν•˜κ²Œ μ½”λ“œλ₯Ό μ§°λŠ”λ°μš”! μ •μ€λ‹˜μ˜ 리뷰 덕뢄에 제 μ½”λ“œλ₯Ό 더 κ°€λ…μ„±μžˆκ²Œ μ§œλŠ” 방법도 ν„°λ“ν•˜μ˜€μŠ΅λ‹ˆλ‹€πŸ˜Š
λ‹€μŒ μ°¨μ‹œ,,,λ§ˆμΈν¬λž˜ν”„νŠΈ,,,κΈ°λŒ€λ˜λŠ”κ΅°μ—¬,,ν›„ν›ŸπŸ˜Ž

name = input()

# 각 μ•ŒνŒŒλ²³ 개수 κ΅¬ν•˜κ³  μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬
count_name = dict(sorted(Counter(name).items()))
Copy link
Member

Choose a reason for hiding this comment

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

저도 μ •μ€λ‹˜μ²˜λŸΌ μ•ŒνŒŒλ²³ 갯수λ₯Ό κ΅¬ν•˜κΈ° μœ„ν•΄ λ°˜λ³΅λ¬Έμ„ μ¨μ„œ 각각 appendν•΄μ€¬λŠ”λ° Counterλ₯Ό μ“°λŠ” 방법이 μžˆμ—ˆλ„€μš”!

Copy link
Member

@avocado-13 avocado-13 left a comment

Choose a reason for hiding this comment

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

νŒ°λ¦°λ“œλ‘¬μ΄ 될 수 μ—†λŠ” 경우인, ν™€μˆ˜ 개의 μ•ŒνŒŒλ²³μ΄ 2개 이상인 경우λ₯Ό ꡬ뢄해두고 μ½”λ“œλ₯Ό μ§  게 μΈμƒμ μž…λ‹ˆλ‹€. 첨뢀해주신 사진도 정말 큰 도움이 λ˜μ—ˆμŠ΅λ‹ˆλ‹€ πŸ€ πŸ‘
μ΄λ²ˆμ°¨μ‹œ pr도 μˆ˜κ³ λ§ŽμœΌμ…¨μŠ΅λ‹ˆλ‹€ β™‘β™₯︎

@Hwangyerin Hwangyerin merged commit d1aa3c3 into AlgoLeadMe:main Mar 19, 2024
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.

5 participants