오늘도 문제 푸는데 시간이 오래걸리고, 난 왜 항상 쉬운길을 돌아서 돌아서 코드를 짜는건지.........................
아직 너무 코드짜는게 힘들다......................
def get_linked_list_sum(linked_list_1, linked_list_2):
sum_1 = 0
head_1 = linked_list_1.head
while head_1 is not None:
sum_1 = sum_1 * 10 + head_1.data
head_1 = head_1.next
sum_2 = 0
head_2 = linked_list_2.head
while head_2 is not None:
sum_2 = sum_2 * 10 + head_2.data
head_2 = head_2.next
return sum_1 + sum_2
def get_linked_list_sum(linked_list_1, linked_list_2):
sum_1 = ''
sum_2 = ''
cur_1 = linked_list_1.head
cur_2 = linked_list_2.head
while cur_1 is not None and cur_2 is not None:
sum_1 += str(cur_1.data)
sum_2 += str(cur_2.data)
cur_1 = cur_1.next
cur_2 = cur_2.next
return int(sum_1) + int(sum_2)
=> 겹치는 부분 함수로 만들어주기, 변수명 깔끔하게
def linked_list_sum(linked_list):
sum_list_in = ''
cur = linked_list.head
while cur is not None:
sum_list_in += str(cur.data)
cur = cur.next
return int(sum_list_in)
def get_linked_list_sum(linked_list_1, linked_list_2):
sum_list_in_1 = linked_list_sum(linked_list_1)
sum_list_in_2 = linked_list_sum(linked_list_2)
return sum_list_in_1 + sum_list_in_2
def is_existing_target_number_binary(target, array):
current_min = 0
current_max = len(array) - 1
current_quess = (current_max + current_min) // 2
find_count = 0
while current_min <= current_max:
find_count += 1
if array[current_quess] == target:
print(find_count)
return '찾았다'
elif array[current_quess] < target:
current_min = current_quess + 1
else:
current_max = current_quess - 1
current_quess = (current_max + current_min) // 2
def is_existing_target_number_binary(target, array):
result = False
count = 0
while result == False:
count += 1
all_numbers = len(finding_numbers)
finding = all_numbers // 2 - 1
if array[finding] < target:
del array[0:finding]
elif array[finding] > target:
del array[finding+1:all_numbers]
else:
result = True
print('찾았다!')
return count
finding_target = 2
finding_numbers = [0, 3, 5, 6, 1, 2, 4]
def is_exist_target_number_binary(target, numbers):
for i in range(0, len(numbers) - 1):
if numbers[i] == finding_target:
return True
result = is_exist_target_number_binary(finding_target, finding_numbers)
print(result)
=> 정렬이 되어있지 않아서 이진탐색 불가
재귀 : 어떠한 것을 정의할 때 자기 자신을 참조하는 것
=> 재귀함수 : 자기 자신을 호출하는 함수
def count_down(number):
print(number) # number를 출력하고
if number <= 0:
return
count_down(number - 1) # count_down 함수를 number - 1 인자를 주고 다시 호출한다!
def factorial(n):
if n == 0 or n == 1:
return 1
elif n < 0:
return ERROR
return n * factorial(n-1)
print(factorial(5))
def is_palindrome(string):
n = len(string)
for i in range(n):
if string[i] != string[n-i-1]:
return False
return True
input = "abcba"
def is_palindrome(string):
if string[0] != string[-1]:
return False
elif len(string) <= 1:
return True
return is_palindrome(string[1:-1])
print(is_palindrome(input))
링크드리스트 합 계산 문자열 더해줘서 자릿수 맞춰주는건 생각도 못했는데 잘 훔쳐갑니다