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

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

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

 

 

1. ๋‹คํŠธ๊ฒŒ์ž„

 

def solution(dart):
    ans = []
    
    for i in range(len(dart)):
        if dart[i] =='S':
            if dart[i-1] == '0' and dart[i-2] == '1':
                ans.append(10)
            else:
                ans.append(int(dart[i-1]))
        elif dart[i] == 'D':
            if dart[i-1] == '0' and dart[i-2] == '1':
                ans.append(10**2)
            else:
                ans.append(int(dart[i-1])**2)
        elif dart[i] == 'T':
            if dart[i-1] == '0' and dart[i-2] == '1':
                ans.append(10**3)
            else:
                ans.append(int(dart[i-1])**3)
        elif dart[i] == '*':
            if len(ans) > 1:
                ans[-1] *= 2
                ans[-2] *= 2
            else:
                ans[-1] *= 2
        elif dart[i] == '#':
            ans[-1] *= -1
    return sum(ans)
๋ถ„์„ > ๋‹จ์ˆœ ๊ตฌํ˜„๋ฌธ์ œ์˜€๋‹ค. ์ ์ˆ˜๊ฐ€ 10์ธ ๊ฒฝ์šฐ๋งŒ ์ž˜ ์ฒ˜๋ฆฌํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€๋ฆฌ๋Š” ๋ฌธ์ œ์˜€๋‹ค. 

 

 

 

2. ๋‚˜๋จธ์ง€๊ฐ€ 1์ด ๋˜๋Š” ์ˆ˜ ์ฐพ๊ธฐ 

 

def solution(n):
    x = 1
    while n % x != 1:
        x += 1
    return x

 

 

 

3. ๋ถ€์กฑํ•œ ๊ธˆ์•ก ๊ณ„์‚ฐํ•˜๊ธฐ 

 

def solution(price, money, count):
    cnt = 0
    for i in range(1,count+1):
        cnt += price * i
    if cnt <= money:
        return 0
    else:
        return cnt - money

 

 

 

4. ๊ฐ€์šด๋ฐ ๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ 

 

def solution(s):
    array = []
    if len(s) % 2 == 0:
        m = (len(s) // 2) - 1
        k = (len(s) // 2) 
        array.append(s[m])
        array.append(s[k])
        return ''.join(array)
        
    else:
        m = (len(s)-1) // 2
        return s[m]

 

 

 

5. ๊ฐ™์€ ์ˆซ์ž๋Š” ์‹ซ์–ด

 

def solution(arr):
    stack = []
    stack.append(arr[0])
    for i in range(1,len(arr)):
        if stack[-1] != arr[i]:
            stack.append(arr[i])
    return stack

 

 

 

6. ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž ๋ฐฐ์—ด 

 

def solution(arr, divisor):
    ans = []
    for i in arr:
        if i % divisor == 0:
            ans.append(i)
    if len(ans) == 0:
        ans.append(-1)
    ans.sort()
    return ans

 

 

 

7. ๋‘ ์ •์ˆ˜ ์‚ฌ์ด์˜ ํ•ฉ

 

def solution(a, b):
    cnt = 0
    arr = [a,b]
    arr.sort()
    if a == b:
        return a
    for i in range(arr[0],arr[1]+1):
        cnt += i 
    return cnt

 

 

 

 

8. ๋ฌธ์ž์—ด ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ 

 

def solution(strings, n):
    ans = []
    imp = []
    
    for word in strings:
        word = list(word)
        ans.append(word)
    
    ans.sort(key = lambda x: (x[n],x),reverse=False)
    
    for i in ans:
        imp.append(''.join(i))
    return imp

 

 

 

9. ๋ฌธ์ž์—ด ๋‚ด p์™€ y์˜ ๊ฐœ์ˆ˜ 

 

def solution(s):
    s = s.upper()
    
    if s.count('P') == s.count('Y'):
        return True
    else:
        return False

 

 

 

10. ๋น„๋ฐ€์ง€๋„ 

 

def solution(n, arr1, arr2):
    imp = []

    for i in range(len(arr1)):
        arr1[i] = format(arr1[i],'b')
        if len(arr1[i]) != n:
            arr1[i] = ('0' * (n - len(arr1[i])))  + arr1[i]
                
    
    for i in range(len(arr2)):
        arr2[i] = format(arr2[i],'b')
        if len(arr2[i]) != n:
            arr2[i] = ('0' * (n - len(arr2[i]))) + arr2[i]
    
    
    for k in range(len(arr1)):
        ans = ''
        for t in range(len(arr1[k])):
            if arr1[k][t] == '1' or arr2[k][t] == '1':
                ans += '#'
            if arr1[k][t] == '0' and arr2[k][t] == '0':
                ans += ' '
        imp.append(ans)
    return imp
๋ถ„์„ > ์ง„์ˆ˜ ๋ณ€ํ™˜์‹œ ์ ‘๋‘์–ด๋ฅผ ์—†์•จ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” format ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ์ ๋งŒ ๋นผ๋ฉด, ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š”๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€๋ฆฌ๋Š” ๋ฌธ์ œ์˜€๋‹ค.