๐Ÿ”ฅ TIL - Day 32

Kim Dae Hyunยท2021๋…„ 10์›” 19์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
37/93

2์ฐจ ํ”„๋กœ์ ํŠธ ๋ฐœํ‘œ๊นŒ์ง€ ๋ !!
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฃผ๊ฐ„ ์‹œ์ž‘ ~

๋‚ด์ผ๋ถ€ํ„ฐ 1์ฃผ์ผ ๊ฐ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‹œ๊ฐ„์„ ๊ฐ–๋Š”๋‹ค. ์ œ๋Œ€๋กœ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์˜ค๋Š˜ ์‚ด~์ง ๋ง›๋ดค๋‹ค.

๋น„๊ต์  ์‰ฌ์šด ๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ ๊ตณ์–ด์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋‡Œ๋ฅผ ํ’€์—ˆ๋‹ค...


๐Ÿ“Œ ๋ฌธ์ œ1 - ์ตœ๋Œ€๊ฐ’ ์ฐพ๊ธฐ

2์ค‘ for๋ฌธ? Nope
O(N)์œผ๋กœ ๋๋‚ด์•ผ ํ•œ๋‹ค.

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


def find_max_num(array):
    n = array[0]
    for i in range(1, len(array)):
        if array[i] > n:
            n = array[i]
    return n


result = find_max_num(input)
print(result)

๐Ÿ“Œ ๋ฌธ์ œ2 - ์ตœ๋นˆ ์•ŒํŒŒ๋ฒณ ์ฐพ๊ธฐ

๋ฝ€์ธํŠธ
ord: ๋ฌธ์ž๋ฅผ ascii ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
chr: ascii ๊ฐ’์„ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

26๊ฐœ์งœ๋ฆฌ ์•ŒํŒŒ๋ฒณ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž˜ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์ด POINT!!

# ์ตœ๋Œ€๊ฐ’ ์ฐพ๊ธฐ ํ•จ์ˆ˜
def find_max_item(array):
    num = array[0]
    max_idx = 0

    for i in range(1, len(array)):
        if array[i] > num:
            num = array[i]
            max_idx = i

    return max_idx


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

    for s in string:
        if s.isalpha():
            alphabet_occurrence_array[ord(s) - ord('a')] += 1

    # ์ตœ๋นˆ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ (0=a)
    max_idx = find_max_item(alphabet_occurrence_array)

    return chr(max_idx + ord('a'))


print(find_alphabet_occurrence_array("hello my name is sparta"))

๐Ÿ“Œ ๋ฌธ์ œ3 - ๊ณฑํ•˜๊ธฐ, ๋”ํ•˜๊ธฐ ์ตœ๋Œ€๊ฐ’

๋ฝ€์ธํŠธ
0๊ณผ 1์€ ๊ณฑํ–ˆ์„ ๋•Œ๊ฐ€ ์•„๋‹Œ ๋”ํ–ˆ์„ ๋•Œ๊ฐ€ ์ตœ๋Œ€๊ฐ’์ด๋‹ค.


# ์™ผ์ชฝ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์—ฐ์‚ฐ๋  ๋•Œ +, *๋กœ๋งŒ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ผ.

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

def find_max_plus_or_multiply(array):
    result = 0

    for a in array:
        if a <= 1 or result <= 1:
            result += a
        else:
            result *= a

    return result


result = find_max_plus_or_multiply(input)
print(result)

๐Ÿ“Œ ๋ฌธ์ œ4 - ์†Œ์ˆ˜์ฐพ๊ธฐ

์•„๋ฆฌํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด..!

# ์ž…๋ ฅ๋ฐ›์€ ์ •์ˆ˜๋ณด๋‹ค ์ž‘์€ ๋ชจ๋“  ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜์—ฌ๋ผ

def get_all_prime(num):
    result = []
    arr = [0] * (num+1)
    for i in range(2, num):
        if arr[i] == 0:
            j = i*2
            while j <= num:
                arr[j] = 1
                j*=2

    for i in range(2, num+1):
        if arr[i] == 0:
            result.append(i)

    return result


num = int(input())
print(get_all_prime(num))

๐Ÿ“Œ ๋ฌธ์ œ5 - ๋ฌธ์ž์—ด ๋’ค์ง‘๊ธฐ

# 0, 1๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์„ ๋’ค์ง‘์–ด์„œ ๋ชจ๋‘ ๊ฐ™์€ ๋ฌธ์ž๋กœ ๋งž์ถ”๊ณ  ์‹ถ์„ ๋•Œ ์ตœ์†Œ ๋’ค์ง‘๋Š” ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜์—ฌ๋ผ

input = "011110"

def find_count_to_turn_out_to_all_zero_or_all_one(string):

    to_one = 0
    to_zero = 0

    if string[0] == '0':
        to_one += 1
    else:
        to_zero += 1
    for i in range(len(string)-1):
        if string[i] is not string[i+1]: # ์•ž ๋’ค๊ฐ€ ๋‹ฌ๋ผ์ง€๋Š” ์ˆœ๊ฐ„์ด ๋’ค์ง‘๋Š” ์ˆœ๊ฐ„
            if string[i+1] == '0':
                to_one += 1
            else:
                to_zero += 1

    return min(to_one, to_zero)


result = find_count_to_turn_out_to_all_zero_or_all_one(input)
print(result)

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ์•„์ฃผ ์˜ค๋žœ๋งŒ์— ํ’€๊ธฐ๋„ ํ–ˆ๊ณ  ์ดˆ๋ฐ˜์ด๋ผ ์‰ฌ์šด ๋ฌธ์ œ ์œ„์ฃผ๋กœ ํ–ˆ๋”๋‹ˆ ๋งค์šฐ ์žฌ๋ฏธ์žˆ์—ˆ๋˜..ใ…Ž ๋ชจ๋“  ๋ฌธ์ œ๊ฐ€ ์ด๋žฌ์œผ๋ฉด ํ•ฉ๋‹ˆ๋‹ค!!..!!

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€