내일배움캠프 - 알고리즘 1주차 개발일지

Dongwoo Kim·2022년 7월 26일
0

스파르타 코딩클럽

내일배움캠프 AI 웹개발자양성과정 2회차

알고리즘 강의 1주차 개발일지

1. 최대값 찾기

input = [3, 5, 6, 1, 2, 4]


def find_max_num(array):
    max_num = array[0]
    for num in array:
        if max_num < num:
            max_num = num
    return max_num


result = find_max_num(input)
print(result)

2. 최대 빈도수 알파벳 찾기

input = "hello my name is sparta"


def find_max_occurred_alphabet(string):
    alphabet_occurrence_array = [0] * 26

    for char in string:
        if not char.isalpha():
            continue
        index = ord(char) - ord('a')
        alphabet_occurrence_array[index] += 1

    max_index = 0
    max_num = alphabet_occurrence_array[max_index]

    for i, num in enumerate(alphabet_occurrence_array):
        if max_num < num:
            max_num = num
            max_index = i

    max_alphabet = chr(ord('a') + max_index)
    return max_alphabet


result = find_max_occurred_alphabet(input)
print(result)

3. 더하기 or 곱하기

: 입력받은 리스트에 원소들 사이에 더하기 또는 곱하기를 통해 최대값을 반환

input = [0, 3, 5, 6, 1, 2, 4]


def find_max_plus_or_multiply(array):
    current_num = array[0]
    for i, num in enumerate(array):
        if i == 0:
            continue
        if current_num + num > current_num * num:
            current_num += num
        else:
            current_num *= num
    return current_num


result = find_max_plus_or_multiply(input)
print(result)

4. 반복되지 않는 문자 찾기

: 입력받은 문자열에 반복되지않는 첫 문자를 반환

input = "abadabaccandipadfk"


def find_not_repeating_character(string):
    alpha_list = []
    alpha_repeating = []

    for char in string:
        alpha_first = True
        if len(alpha_list) == 0:
            alpha_list.append(char)
            alpha_repeating.append(1)
            continue
        for i, alpha in enumerate(alpha_list):
            if alpha == char:
                alpha_repeating[i] += 1
                alpha_first = False
                break
        if alpha_first:
            alpha_list.append(char)
            alpha_repeating.append(1)

    print(alpha_list)
    print(alpha_repeating)

    for i, num in enumerate(alpha_repeating):
        if num == 1:
            return alpha_list[i]
    return "_"


result = find_not_repeating_character(input)
print(result)

5. 시간복잡도

: 알고리즘을 실행하는 데 걸리는 시간을 표현하는 방법

In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each elementary operation takes a fixed amount of time to perform.

https://en.wikipedia.org/wiki/Time_complexity

1) Big O 표기법

: 알고리즘의 성능을 수학적으로 표기하는 방법 중 하나

In computer science, big O notation is used to classify algorithms according to how their run time or space requirements grow as the input size grows.

https://en.wikipedia.org/wiki/Big_O_notation#Use_in_computer_science

알고리즘의 실행시간에 가장 많은 영향을 주는 반복문에 대해서 시간복잡도를 간단하게 표현할 수 있다.

O(N)O(N), O(N2)O(N^2), O(NlogN)O(N*logN)

Github

https://github.com/kimphysicsman/nbcamp-algorithm-220726

profile
kimphysicsman

0개의 댓글