본문 바로가기

분류 전체보기443

[프로그래머스] 연속된 수의 합 def solution(num, total): answer = [] multiple = 0 if num%2 == 0: start_num = num//2 answer = [i for i in range(-(num//2)+1, num//2+1)] else: start_num = 0 answer = [i for i in range(-(num//2),num//2+1)] while(1): if start_num == total: answer = [i+multiple for i in answer] break start_num += num multiple += 1 return answer 이번 문제는 주어진 num개의 연속된 수의 합이 total이 되도록 하는 연속된 수로 이루어진 배열을 구하는 문제이다. 처음에는 .. 2023. 7. 23.
[Python] replace() / strip(), lstrip(), rstrip() 함수 정리 .replace() replace()는 문자열을 변경하는 함수이다. 문자열 안에서 특정 문자를 새로운 문자로 변경하는 기능을 가지고 있다. 사용 방법은 '변수. replace(old, new, [count])' 형식으로 사용한다. - old : 현재 문자열에서 변경하고 싶은 문자 - new: 새로 바꿀 문자 - count: 변경할 횟수. 횟수는 입력하지 않으면 old의 문자열 전체를 변경한다. 기본값은 전체를 의미하는 count=-1로 지정되어있다. .strip(), .lstrip(), rstrip() strip()을 이용하면 문자열에서 특정 문자를 제거할 수 있다. Python의 String은 다음 함수를 제공한다. - strip([chars]) : 인자로 전달된 문자를 String의 왼쪽과 오른쪽에서.. 2023. 7. 22.
[프로그래머스] 옹알이(2) def solution(babbling): answer = 0 prefix_list = ['a','y','w','m'] global current current = '' def check(string): global current list_str = list(string) index = 0 while(index 2023. 7. 22.
[프로그래머스] 문자열 나누기 def solution(s): answer = [] string = s while(string != ''): x = string[0] list_str = list(string) count_x = 0 count_not_x = 0 index = 0 for i,value in enumerate(list_str): if value == x: count_x += 1 else: count_not_x += 1 if count_x != 0 and count_x == count_not_x: index = i break index = i answer.append(list_str[:index+1]) string = ''.join(list_str[index+1:]) return len(answer) 이번 문제는 주어진 조건대.. 2023. 7. 21.
[프로그래머스] 문자열 밀기 def solution(A, B): def move(string): list_str = list(string) str_len = len(list_str) new_list = [list_str[-1]] + list_str[:-1] print(new_list) return ''.join(new_list) available = False check_str = A answer = 0 for i in range(len(A)): if check_str == B: available = True break check_str = move(check_str) answer += 1 if available: return answer else: return -1 이번 문제는 주어진 문자열 A를 오른쪽으로 밀면서 B를 만들 수 .. 2023. 7. 21.
[프로그래머스] 숨어있는 숫자의 덧셈(2) def solution(my_string): answer = 0 num_list = ("0","1","2","3","4","5","6","7","8","9") temp_str = '' for value in my_string: if value in num_list: temp_str += value else: if temp_str != '': answer += int(temp_str) temp_str = '' if temp_str != '': answer += int(temp_str) return answer 이번 문제는 주어진 문자열에서 정수 값만 골라서 더해준 값을 구하는 문제이다. temp_str 이라는 임의의 문자열을 선언한 뒤에 주어진 문자열을 순회하면서 해당 문자가 숫자인 경우에만 붙여주고, 자.. 2023. 7. 20.