6_5 - syh39/ProblemSolving GitHub Wiki

K๋ฒˆ์งธ์ˆ˜

์ถœ์ฒ˜: (https://programmers.co.kr/learn/courses/30/lessons/42748)

๋‚œ์ด๋„ : 1

ํ’€์ด์—ฌ๋ถ€ : Y

์œ ํ˜• : ์ •๋ ฌ

ํ’€์ด ๋ฐฉ๋ฒ•

def solution(array, commands):
    answer = []
    for i in commands: // ์ปค๋งจ๋“œ ๋Œ๋ฉด์„œ
        answer.append(answer1(array, i)) // ๊ฐ ์ปค๋ฉ˜๋“œ์˜ ๋‹ต์„ answer ๋ฐฐ์—ด์— ์ €์žฅ
    return answer


def answer1(array, command):
    print('answer1')
    array = array[command[0]-1:command[1]] // ์ž๋ฅด๊ธฐ
    array.sort() // ์ •๋ ฌ
    return array[command[2]-1] // ํ•ด๋‹น ์ธ๋ฑ์Šค ๋ฆฌํ„ด

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด ๋ฐฉ๋ฒ•:

def solution(array, commands):     
    return list(map(lambda x:sorted(array[x[0]-1:x[1]])[x[2]-1], commands)) 
    // ๋žŒ๋‹ค ํ‘œํ˜„์‹์œผ๋กœ ํ•จ์ˆ˜์™€ ๋ฐฐ์—ด์„ ๋งต์œผ๋กœ ๋งค์นญํ•ด์ฃผ๊ณ  ๊ฐ ๋‹ต์„ ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„์„œ ๋ฆฌํ„ด

๊ฐ€์žฅ ํฐ ์ˆ˜

์ถœ์ฒ˜: (https://programmers.co.kr/learn/courses/30/lessons/42746)

๋‚œ์ด๋„ : 2

ํ’€์ด์—ฌ๋ถ€ : N

์œ ํ˜• : ์ •๋ ฌ

ํ’€์ด ๋ฐฉ๋ฒ•

# ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด ๋ฐฉ๋ฒ• ์ฐธ๊ณ 
def solution(numbers):
    numbers = list(map(str, numbers)) # ์ˆซ์ž ๋ฐฐ์—ด์„ string์œผ๋กœ ๋ณ€ํ™˜
    answer = str(int(''.join(sorted(numbers, key=lambda x : x*3, reverse=True)))) # string ์„ ์ „๋ถ€ *3 ํ•ด์ฃผ๊ณ  ์ •๋ ฌ(3์ž๋ฆฌ ๋‹ค ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด) ํ›„ ๋นˆ์นธ์—†์ด join / int ๋ณ€ํ™˜ ํ›„ ๋‹ค์‹œ string์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ์ด์œ ๋Š” ์ˆซ์ž๊ฐ€ 000์ผ ๊ฒฝ์šฐ 0์œผ๋กœ ํ‘œ์‹œํ•ด์ฃผ๊ธฐ ์œ„ํ•ด
    return answer

๊ฒฐ๋ก 

ํ’€์ด ๋กœ์ง์€ ๋งž์ท„์ง€๋งŒ ๊ตฌํ˜„์„ ๋ชฐ๋ผ์„œ ์ฝ”๋“œ๋ฅผ ๋ชป์งฐ๋˜ ๋ฌธ์ œ๋‹ค. ํŒŒ์ด์ฌ ์–ธ์–ด๋งŒ ์ž˜ ์•Œ์•„๋„ ๊ตฌํ˜„์€ ์‰ฝ๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

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

์ถœ์ฒ˜: (https://programmers.co.kr/learn/courses/30/lessons/42839)

๋‚œ์ด๋„ : 2

ํ’€์ด์—ฌ๋ถ€ : N

์œ ํ˜• : ์™„์ „ํƒ์ƒ‰

ํ’€์ด ๋ฐฉ๋ฒ•

# ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด ๋ฐฉ๋ฒ• ์ฐธ๊ณ 
from itertools import permutations
import math


def solution(numbers):
    num = [n for n in numbers]  # '17' -> ['1', '7']
    per = []

    for i in range(1, len(numbers)+1):
        per += list(permutations(num, i))  # i๊ฐœ์”ฉ ์ˆœ์—ด์กฐํ•ฉ

    new_num = [int(('').join(p)) for p in per]  # int ํ˜•์˜ ์ˆœ์—ด ์กฐํ•ฉ
    return countPrimeNumber(set(new_num))  # ์ค‘๋ณต ์ œ๊ฑฐ ํ›„ ํ•จ์ˆ˜์— ๋„˜๊น€


def countPrimeNumber(new_num):
    ans = 0
    for n in new_num:
        checkPrime = True
        if(n < 2):  # 2๋ณด๋‹ค ๋” ํฐ ์ˆ˜์— ํ•œํ•ด์„œ
            continue
        for i in range(2, int(math.sqrt(n))+1):  # ์ˆœ์—ด ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•
            if n % i == 0:
                checkPrime = False
                break
        if checkPrime:  # ์†Œ์ˆ˜์ธ ๊ฒฝ์šฐ count++
            ans += 1
    return ans

๊ฒฐ๋ก 

ํ’€์ด ๋กœ์ง์€ ๋งž์ท„์ง€๋งŒ ๊ตฌํ˜„์„ ๋ชฐ๋ผ์„œ ์ฝ”๋“œ๋ฅผ ๋ชป์งฐ๋˜ ๋ฌธ์ œ๋‹ค. ํŒŒ์ด์ฌ ์–ธ์–ด๋งŒ ์ž˜ ์•Œ์•„๋„ ๊ตฌํ˜„์€ ์‰ฝ๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.