본문 바로가기

Python_Intermediate/Algorithmus

(9)
[알고리즘]선택 정렬(Select Sort) 선택 정렬> 다음과 같은 순서를 반복하며 정렬하는 알고리즘 1. 주어진 데이터 중, 최소값을 찾음 2. 해당 최소값을 데이터 맨 앞에 위치한 값과 교체함 3. 맨 앞의 위치를 뺀 나머지 데이터를 동일한 방법으로 반복함 참조 사이트> https://visualgo.net/bn/sorting VisuAlgo - Sorting (Bubble, Selection, Insertion, Merge, Quick, Counting, Radix) VisuAlgo is free of charge for Computer Science community on earth. If you like VisuAlgo, the only payment that we ask of you is for you to tell the existe..
[알고리즘]버블 정렬(Buble Sort) 버블 정렬> 두 인접한 데이터를 비교해서, 앞에 있는 데이터가 뒤에 잇는 데이터보다 크면, 자리를 바꾸는 정렬 알고리즘 참조 사이트> https://visualgo.net/bn/sorting VisuAlgo - Sorting (Bubble, Selection, Insertion, Merge, Quick, Counting, Radix) VisuAlgo is free of charge for Computer Science community on earth. If you like VisuAlgo, the only payment that we ask of you is for you to tell the existence of VisuAlgo to other Computer Science students/inst..
Python - 피보나치 수열(for문 / 재귀함수) 1. for문 def fib(n): list = [] for i in range(0, n): if i < 2: list.append(1) else: list.append(list[i-1] + list[i-2]) return list[n-1] n = int(input()) print(fib(n)) - 값을 저장 하기 위한 빈 리스트 생성 - for문을 통해 배열의 인덱스는 0 부터 시작이므로 0 ~ n 까지 실행 - n의 값이 3보다 작으면 값이 1이므로 if < 2는 1를 추가 - 바로 앞의 두 피보나치의 수의 합을 구하여 되므로 else 구문 - 0부터 시작했으므로 list[n-1] 2. 재귀함수 def fib2(n): if n < 3: return 1 else: return fib2(n-1) + fi..
Python - 최대 k-구간 합 알고리즘 Q>주어진 목록에서 최대 k-구간합을 구하는 알고리즘을 작성하시오. A>def deQuote(list): for i in range(0, len(list)): list[i] = int(list[i]) def findMaxSpan(list, k): size = len(list) # 목록의 길이 max = list[0] # 임시 최대 구간합 for start in range(0, size - k + 1): # strat = 구간 출발 위치 sum = list[start] # 현재 구간 합 초기화 for i in range(1, k): # 현재 구간 내 원소들 sum += list[start +i] # 원소 합산 if sum > max: # 최대 구간합 갱신 max = sum return max list = ..
Palindrome Discrimination(회문 판별) 1. 회문?순서를 거꾸로 읽어도 똑같은 단어나 문장.예를 들어 level, bab, 기러기, 토마토 등 과 같은 것.판별 방법은 첫번째 글자와 마지막 글자를 비교 하여 같은지 다른지 보면 된다. 2. 회문 판별 방법2-1. for문word = input('단어를 입력하세요: ') palindrome = True for i in range(len(word) // 2): if word[i] != word[-1 - i]: palindrome = False break print(palindrome)2-2. 문자열 슬라이스word = input('단어를 입력하세요: ') print(word == word[::-1])2-3. reversedword = input('단어를 입력하세요: ') print(list(wor..
Python - 사용자에게 입력 받아 * 산 모양 알고리즘 Q>사용자에게 입력 받아 * 산 모양 만들기 A>star = int(input('*의 높이를 입력하세요 : ')) for i in range(star): for j in reversed(range(star)): if j > i: print(' ', end='') else: print('*', end='') for j in range(star): if j *의 높이를 입력하세요 : 5 * *** ***** **************** Process finished with exit code 0
Python - 계단식 * 출력 알고리즘 Q>계단식 * 출력 알고리즘 A>for i in range(5): for j in range(5): if j *************** Process finished with exit code 0
Python - 5 x 5 사각형 * 출력 알고리즘 Q>5 x 5 사각형 * 출력 알고리즘 A>for i in range(5): for j in range(5): print('*', end='') print() O>************************* Process finished with exit code 0