Coding/Code in the Morning

211230_Code in the morning(μ†Œμˆ˜μ°ΎκΈ°, μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체)

κΉ€μœ λ‹ˆμ½˜ 2021. 12. 30. 14:40

Code in the morning, 맀일 μ•„μΉ¨ μ•Œκ³ λ¦¬μ¦˜ 문제λ₯Ό ν’‰λ‹ˆλ‹€. 

πŸ’¬ 문제 μ„€λͺ…: μ†Œμˆ˜ μ°ΎκΈ°

1λΆ€ν„° μž…λ ₯받은 μˆ«μž n μ‚¬μ΄μ— μžˆλŠ” μ†Œμˆ˜μ˜ κ°œμˆ˜λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜, solution을 λ§Œλ“€μ–΄ λ³΄μ„Έμš”.

μ†Œμˆ˜λŠ” 1κ³Ό μžκΈ° μžμ‹ μœΌλ‘œλ§Œ λ‚˜λˆ„μ–΄μ§€λŠ” μˆ˜λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.
(1은 μ†Œμˆ˜κ°€ μ•„λ‹™λ‹ˆλ‹€.)

 

 

πŸ“ μ œν•œ 사항

  • n은 2이상 1000000μ΄ν•˜μ˜ μžμ—°μˆ˜μž…λ‹ˆλ‹€.

 

 

πŸ’‘ λ‚˜μ˜ 풀이 

from collections import Counter 

def solution(n):
    num = []
    # μ†Œμˆ˜λ₯Ό μ°ΎλŠ” 식 
    for i in range (1,n+1):
        for j in range (1, n+1):
            if i % j == 0:
                num.append(i)
    
    cnt = Counter(num)
    a = []
    for i in range (1, n+1):
        if (cnt[i] == 2):
            a.append(i)
    return(len(a))

λ‚˜μ˜ ν’€μ΄λŠ”:

1. nκΉŒμ§€μ˜ μ†Œμˆ˜λ₯Ό 리슀트둜 λ°›μ•„μ˜€κ³ , 

2. μ†Œμˆ˜λ₯Ό κ³¨λΌλ‚΄μ„œ 

3. 갯수λ₯Ό μ„ΈκΈ° μ˜€λŠ”λ°, 

μœ„μ˜ λ°©λ²•μœΌλ‘œ ν•˜λ‹ˆ μ‹œκ°„ 초과둜 μ‹€νŒ¨ν•˜κ²Œ λ˜μ—ˆλ‹€. γ… γ… 

 

* Counter ν•¨μˆ˜: 

Counter의 ν•¨μˆ˜λŠ” μ»¨ν…Œμ΄λ„ˆλ“±μ— λ™μΌν•œ μžλ£Œκ°€ λͺ‡ κ°œμΈμ§€ ν™•μΈν•˜λŠ” 데 μ‚¬μš©ν•˜λŠ” 객체이닀. 

from collections import Counter
 
list = ['Hello', 'HI', 'How', 'When', 'Where', 'Hello']
print(Counter(list))




#κ²°κ³Όλ¬Ό 
>>>Counter({'Hello': 2, 'HI': 1, 'How': 1, 'When': 1, 'Where': 1})

πŸ’‘ λ‹€λ₯Έ ν•΄μ„€

* μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ μ²΄

μ—λΌν† μŠ€ν…Œλ„€μŠ€κ°€ κ³ μ•ˆν•œ μ†Œμˆ˜λ₯Ό μ°ΎλŠ” λ°©λ²•μœΌλ‘œ, μ΄ λ°©λ²•μœΌλ‘œ μ†Œμˆ˜λ₯Ό μ°ΎμœΌλ €λ©΄ 2λΆ€ν„° μ‹œμž‘ν•΄ μžμ—°μˆ˜λ₯Ό μ°¨λ‘€λ‘œ μ“΄ λ‹€μŒ, 2 μ΄μ™Έμ˜ 2의 λ°°μˆ˜, 3 μ΄μ™Έμ˜ 3의 λ°°μˆ˜, 5 μ΄μ™Έμ˜ 5의 λ°°μˆ˜μ˜ μˆœμ„œλ‘œ μˆ˜λ₯Ό μ§€μ›Œλ‚˜κ°€ λμ— λ‚¨λŠ” μˆ˜κ°€ μ†Œμˆ˜μ΄λ‹€.

def solution(n):
    num = set(range(2,n+1))
    
    for i in range(2,n+1):
        if i in num:
            num -= set(range(2*i, n+1, i))
 
    return len(num)

 

* set μ§‘ν•©μ—μ„œ μ›μ†Œλ₯Ό -둜 λ°”λ‘œ λΊ„ 수 μžˆλ‚˜λ³΄λ‹€. 

 

 

https://ko.wikipedia.org/wiki/%EC%97%90%EB%9D%BC%ED%86%A0%EC%8A%A4%ED%85%8C%EB%84%A4%EC%8A%A4%EC%9D%98_%EC%B2%B4

 

μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체 - μœ„ν‚€λ°±κ³Ό, 우리 λͺ¨λ‘μ˜ 백과사전

μˆ˜ν•™μ—μ„œ μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ μ²΄λŠ” μ†Œμˆ˜λ₯Ό μ°ΎλŠ” 방법이닀. κ³ λŒ€ 그리슀 μˆ˜ν•™μž μ—λΌν† μŠ€ν…Œλ„€μŠ€κ°€ λ°œκ²¬ν•˜μ˜€λ‹€. μ•Œκ³ λ¦¬μ¦˜[νŽΈμ§‘] 2λΆ€ν„° μ†Œμˆ˜λ₯Ό κ΅¬ν•˜κ³ μž ν•˜λŠ” κ΅¬κ°„μ˜ λͺ¨λ“  수λ₯Ό λ‚˜μ—΄ν•œλ‹€. κ·Έλ¦Όμ—μ„œ

ko.wikipedia.org

 

πŸ‘‰πŸ» 문제 λ°”λ‘œκ°€κΈ° 

'Coding > Code in the Morning' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

220110_Code in the morning  (0) 2022.01.10
220105_Code in the morning  (0) 2022.01.05
211229_Code in the morning  (0) 2021.12.29
211228_Code in the morning  (0) 2021.12.28
211227_Code in the morning  (0) 2021.12.27