기본적인 파이썬 문법과 내장 함수들을 다양한 문제에 적용해보며 실력을 다질 수 있었다.
for/while, sort/sorted, map, join 등 함수와 반복문의 차이를 실제로 써보며 명확히 이해했다. 실습을 통해 작은 코드의 차이가 결과에 어떻게 영향을 주는지 몸소 체감할 수 있었다.
✅ sort() : 리스트를 그대로 정렬하고 싶을 때
✅ sorted() : 원래 리스트를 유지하면서, 정렬된 복사본 생성
✅ for 반복문 : 횟수가 정해져 있을 때 사용
✅ while 반복문 : "~할때까지"라는 느낌이 있을 때 사용
✅ map() : 리스트 안의 모든 값에 어떤 작업을 한번에 똑같이 해줌
✅ swapcase() : 대문자는 소문자로, 소문자는 대문자로
✅ index() : 리스트나 문자열에서 특정 값이 처음 나오는 위치를 알려주는 함수
✅ replace() : 문자열에서 특정 글자를 다른 글자로 바꿔주는 함수
✅ join() : join() : 여러개의 문자열을 하나로 붙여주는 함수
def solution(sides):
sides.sort(reverse = True)
total = sides[1] + sides[2]
if sides[0] < total:
answer = 1
else:
answer = 2
return answer

def solution(numbers, num1, num2):
answer = numbers[num1:num2+1:1]
return answer
def solution(slice, n):
pizzas = 0
while pizzas * slice < n:
pizzas += 1
return pizzas
def solution(dot):
if dot[0] > 0 and dot[1] > 0: # 1사분면
answer = 1
elif dot[0] < 0 and dot[1] > 0: # 2사분면
answer = 2
elif dot[0] < 0 and dot[1] < 0: # 3사분면
answer = 3
else:
answer = 4
return answer
def solution(s1, s2):
count = 0
for i in s1:
if i in s2:
count += 1
return count
def solution(n):
num_list = []
for i in range(1,n+1):
if n % i == 0:
num_list.append(i)
return len(num_list)
def solution(n, numlist):
newlist = []
for i in range(len(numlist)):
if numlist[i] % n == 0:
newlist.append(numlist[i])
return newlist
def solution(strlist):
lenlist = []
for i in strlist:
lenlist.append(len(i))
return lenlist
def solution(money):
newlist = []
countcoffee = money // 5500
newlist.append(countcoffee)
change = money - (5500 * countcoffee)
newlist.append(change)
return newlist
def solution(array, n):
count = 0
for num in array:
if num == n:
count += 1
return count
def solution(numbers):
doublelist = []
for n in numbers:
n = n * 2
doublelist.append(n)
return doublelist
def solution(array):
array.sort()
return array[len(array) // 2]
def solution(n):
oddlist = []
for num in range(1,n+1):
if num <= n and num % 2 != 0:
oddlist.append(num)
return oddlist
def solution(price):
if price >= 500000:
pay = price * 0.8
elif price >= 300000:
pay = price * 0.9
elif price >= 100000:
pay = price * 0.95
else:
pay = price
return pay
def solution(hp):
j_ant = hp // 5
hp = hp % 5
b_ant = hp // 3
hp = hp % 3
i_ant = hp
return j_ant + b_ant + i_ant
def solution(my_string):
digit_list = []
for i in my_string:
if i.isdigit() == True:
digit_list.append(i)
intdigit_list = list(map(int,digit_list))
# digit_list의 값을 int로 전부 바꿔서 list로 변환한다.
total = sum(intdigit_list)
return total
def solution(numbers):
numbers.sort(reverse = True)
max_num1 = numbers[0] * numbers[1]
max_num2 = numbers[-1] * numbers[-2]
if max_num1 > max_num2:
answer = max_num1
else:
answer = max_num2
return answer
def solution(cipher, code):
result = ''
for i in range(1, len(cipher) + 1):
if i % code == 0:
result += cipher[i - 1]
return result
def solution(my_string):
swap_string = my_string.swapcase()
return swap_string

def solution(my_string, num1, num2):
new_list = list(my_string)
new_list[num1], new_list[num2] = new_list[num2], new_list[num1]
return ''.join(new_list)
def solution(n):
measure_list = []
for i in range(1,n+1):
if n % i == 0:
measure_list.append(i)
return measure_list
def solution(array):
max_num = max(array)
max_idx = array.index(max_num)
return [max_num, max_idx]

def solution(my_string):
lower_string = my_string.lower()
abc_string = sorted(lower_string)
string = ''.join(abc_string)
return string
def solution(num, k):
num_str = str(num)
for i in range(len(num_str)):
if num_str[i] == str(k):
return i + 1
return -1
def solution(my_string, n):
result_list = []
for i in range(len(my_string)):
result_list.append(my_string[i] * n)
result = ''.join(result_list)
return result
join() : 여러개의 문자열을 하나로 붙여주는 함수
-- 기본사용법 : "구분자".join(문자열리스트)
words = ['apple', 'banana', 'cherry']
result = ','.join(words)
print(result) # 👉 'apple,banana,cherry'
def solution(number, n, m):
if number % n == 0 and number % m == 0:
return 1
else:
return 0
def solution(a, b):
plus1 = str(a) + str(b)
plus2 = str(b) + str(a)
if plus1 > plus2:
return int(plus1)
elif plus1 == plus2:
return int(plus1)
else:
return int(plus2)
str1, str2 = input().strip().split(' ')
string = str1 + str2
new = string.replace(" ", "")
print(new)
def solution(arr):
arr_list = ''.join(arr)
return arr_list
def solution(n):
measure = []
for i in range(1, n+1):
if n % i == 0:
measure.append(i)
return sum(measure)
def solution(N):
str_N = str(N)
list_N = list(map(int, str_N))
total = sum(list_N)
return total
def solution(x, n):
result = []
for i in range(1, n+1):
xx = x * i
result.append(xx)
return result
def solution(n):
for x in range(1, n+1):
if n % x == 1:
return x