2์ฐจ ํ๋ก์ ํธ ๋ฐํ๊น์ง ๋ !!
์๊ณ ๋ฆฌ์ฆ ์ฃผ๊ฐ ์์ ~
๋ด์ผ๋ถํฐ 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)
๋ฝ์ธํธ
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"))
๋ฝ์ธํธ
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)
์๋ฆฌํ ์คํ ๋ค์ค์ ์ฒด..!
# ์
๋ ฅ๋ฐ์ ์ ์๋ณด๋ค ์์ ๋ชจ๋ ์์๋ฅผ ๊ตฌํ์ฌ๋ผ
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))
# 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)
์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๋ฅผ ์์ฃผ ์ค๋๋ง์ ํ๊ธฐ๋ ํ๊ณ ์ด๋ฐ์ด๋ผ ์ฌ์ด ๋ฌธ์ ์์ฃผ๋ก ํ๋๋ ๋งค์ฐ ์ฌ๋ฏธ์์๋..ใ ๋ชจ๋ ๋ฌธ์ ๊ฐ ์ด๋ฌ์ผ๋ฉด ํฉ๋๋ค!!..!!