Algorithm/Programmers

[Programmers] 코딩 기초 트레이닝 #4

j4ko 2023. 11. 25. 16:45
728x90
반응형

목차

     

    n보다 커질 때까지 더하기

    https://school.programmers.co.kr/learn/courses/30/lessons/181884

    def solution(numbers, n):
        
        latest = 0
        for number in numbers:
            latest += number
            
            if latest > n:
                return latest
    

     

    문자열 앞의 n 글자

    https://school.programmers.co.kr/learn/courses/30/lessons/181907

    def solution(my_string, n):    
        return my_string[0:n]
    

     

    문자열 뒤의 n 글자

    https://school.programmers.co.kr/learn/courses/30/lessons/181910

    def solution(my_string, n):
        return my_string[len(my_string) - n:]
    

     

    0떼기

    https://school.programmers.co.kr/learn/courses/30/lessons/181847

    def solution(n_str):
        return n_str.lstrip("0")
    

     

    문자열로 변환

    https://school.programmers.co.kr/learn/courses/30/lessons/181845

    def solution(n):
        return str(n)
    

     

    n 번째 원소까지

    https://school.programmers.co.kr/learn/courses/30/lessons/181889

    def solution(num_list, n):    
        return num_list[0:n]
    

     

    접미사인지 확인하기

    https://school.programmers.co.kr/learn/courses/30/lessons/181908

    def solution(my_string, is_suffix):
        length = len(my_string)
        length2 = len(is_suffix)
    
        answer = 0
        if length2 > length:
            answer = 0
        for x in range(length + 1):
            if is_suffix == my_string[x:length]:
                answer = 1
        return answer
    
    def solution(my_string, is_suffix):
        return int(my_string.endswith(is_suffix))

     

    문자열 바꿔서 찾기

    https://school.programmers.co.kr/learn/courses/30/lessons/181864

    def solution(myString, pat):
        cc = ""
        for x in myString:
            if x == 'A':
                cc += "B"
            if x == 'B':
                cc += "A"
        return int(pat in cc)

     

    길이에 따른 연산

    https://school.programmers.co.kr/learn/courses/30/lessons/181879

    def solution(num_list):
        length = len(num_list)
        
        if 10 < length:
            return sum(num_list)
        else:
            mul = 1
            for n in num_list:
                mul *= n
            return mul

     

    첫 번째로 나오는 음수

    https://school.programmers.co.kr/learn/courses/30/lessons/181896

    def solution(num_list):
        for num in num_list:
            if num < 0:
                return num_list.index(num)
        return -1
    728x90
    반응형