๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์Šคํ„ฐ๋””

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์Šคํ„ฐ๋”” 4์ฃผ์ฐจ

 

 

1. 2016๋…„

 

def solution(a, b):
    date = ['FRI','SAT','SUN','MON','TUE','WED','THU']
    month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    cnt = 0
    
    for i in range(a-1):
        cnt += month[i]
    ans = (cnt + b) % 7
    
    return date[ans-1]

 

 

 

2. ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๊ธฐ๋ณธ

 

def solution(s):
    if len(s) == 4 and s.isdigit():
        return True
    elif len(s) == 6 and s.isdigit():
        return True
    else:
        return False

 

 

 

3. ๋ฌธ์ž์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ 

 

def solution(s):
    s = list(s)
    s.sort(reverse=True)
    return ''.join(s)

 

 

 

4. ์•ฝ์ˆ˜์˜ ํ•ฉ

 

def solution(n):
    arr = []
    
    for i in range(1,n+1):
        if n % i == 0:
            arr.append(i)
    return sum(arr)

 

 

 

5. ์„œ์šธ์—์„œ ๊น€์„œ๋ฐฉ ์ฐพ๊ธฐ 

 

def solution(seoul):
        answer = "๊น€์„œ๋ฐฉ์€ " + str(seoul.index("Kim")) + "์— ์žˆ๋‹ค"
        return answer

 

 

 

6. ์ˆ˜๋ฐ•์ˆ˜๋ฐ•์ˆ˜๋ฐ•์ˆ˜๋ฐ•์ˆ˜๋ฐ•์ˆ˜?

 

def solution(n):
    ans = ''
    for i in range(n):
        if i % 2 == 0:
            ans += '์ˆ˜'
        else:
            ans += '๋ฐ•'
    return ans

 

 

 

7. ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ฐ”๊พธ๊ธฐ 

 

def solution(s):
    if s[0] == "-":
        k = int(s[1:]) * -1
        return k
    else:
        k = int(s[0:])
        return k

 

 

 

 

8. ์†Œ์ˆ˜ ์ฐพ๊ธฐ 

 

import itertools
import math

def solution(numbers):
    arr = []
    imp = []
    ans = 0
    
    for i in range(1,len(numbers)+1):
        num = list(map(''.join, itertools.permutations(numbers,i)))
        arr.append(num)
    
    arr = sum(arr,[])
    arr = list(set(arr))
    
    for i in arr:
        imp.append(int(i))
    imp = list(set(imp))
    imp.sort()
    
    for k in imp:
        cnt = 0
        sqrt = int(math.sqrt(k))
        if k != 0 and k != 1:
            for i in range(2,sqrt+1):
                    if k % i == 0:
                        cnt += 1
            if cnt == 0:
                ans += 1
    return ans
๋ถ„์„ > ํ•ด๋‹น ์ˆ˜๋ฅผ ์ œ๊ณฑ๊ทผ ์ดํ•˜์˜ ์ˆ˜๋กœ ํ•˜๋‚˜์”ฉ ๋‚˜๋ˆ„์–ด ๋ดค์„ ๋•Œ, ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆ˜๊ฐ€ 0๊ฐœ๋ผ๋ฉด ์ด ์ˆ˜๊ฐ€ ์†Œ์ˆ˜์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ ํฌ์ธํŠธ์ธ ๋ฌธ์ œ์˜€๋‹ค. 

 

 

 

9. ์‹œ์ € ์•”ํ˜ธ 

 

def solution(s, n):
    ans = ''
    
    for i in s:
        if i.isupper():
            if ord(i) + n > ord('Z'):
                ans += chr(ord(i) + n - ord('Z') + ord('A') - 1)
            else:
                ans += chr(ord(i) + n)
        elif i.islower():
            if ord(i) + n > ord('z'):
                ans += chr(ord(i) + n - ord('z') + ord('a') - 1)
            else:
                ans += chr(ord(i) + n)
        else:
            ans += ' '
                
    return ans

 

 

 

10. ์ตœ์†Œ ์ง์‚ฌ๊ฐํ˜• 

 

def solution(sizes):
    w = []
    h = []
    for i in range(len(sizes)):
        if sizes[i][0] > sizes[i][1]:
            w.append(sizes[i][0])
            h.append(sizes[i][1])
        else:
            w.append(sizes[i][1])
            h.append(sizes[i][0])
    return max(w) * max(h)