제로베이스 데이터 스쿨/일일 스터디 노트
18일차 스터디노트 / 파이썬 알고리즘(병합정렬, 재귀, 평균) 연습문제, 미니콘다, vscode, 구글 colab
김뎀뎀
2023. 1. 25. 19:53
※제로베이스 데이터 취업스쿨 11기 수강 중
📗 18일차 공부 내용 요약
1. 파이썬 알고리즘과 관련된 연습문제를 풀었다.
- 재귀, 병합정렬, 평균
2. EDA/웹크롤링/파이썬프로그래밍 학습의 오리엔테이션을 듣고, 관련 프로그램을 설치/실행해보았다.
- 미니콘다, vscode, 구글 colab
📖 18일차 공부 내용 자세히
※기억에 남는 연습문제 위주로 기재
Q.다음은 A 상사의 2021년 월별 매출을 나태내는 리스트이다. 재귀 알고리즘을 이용해서 1월부터 12월까지 전월대비 매출 증감액을 나타내는 프로그램을 만들어보자.
sales = [12000, 13000, 12500, 11000, 10500, 98000, 91000, 91500, 10500, 11500, 12000, 12500]
sales = [12000, 13000, 12500, 11000, 10500, 98000, 91000, 91500, 10500, 11500, 12000, 12500]
def salesUpAndDown(ss):
if len(ss) == 1:
return ss
print(f'sales: {ss}')
currentSales = ss.pop(0)
nextSales = ss[0]
increase = nextSales - currentSales
if increase > 0:
increase = '+' + str(increase)
print(f'매출 증감액: {increase}')
return salesUpAndDown(ss)
if __name__ == '__main__':
salesUpAndDown(sales)
Q. 사용자가 정수 두 개를 입력하면 작은 정수와 큰 정수 사이의 모든 정수의 합을 구하는 프로그램을 재귀 알고리즘을 이용해서 만들어보자.
sales = [12000, 13000, 12500, 11000, 10500, 98000, 91000, 91500, 10500, 11500, 12000, 12500]
def salesUpAndDown(ss):
if len(ss) == 1:
return ss
print(f'sales: {ss}')
currentSales = ss.pop(0)
nextSales = ss[0]
increase = nextSales - currentSales
if increase > 0:
increase = '+' + str(increase)
print(f'매출 증감액: {increase}')
return salesUpAndDown(ss)
if __name__ == '__main__':
salesUpAndDown(sales)
Q.다음은 어떤 체조선수의 경기 점수이다. 최댓값과 최솟값을 제외한 나머지 점수에 대한 평균을 구하고 순위를 정하는 알고리즘을 만들어보자.
체조선수의 경기 점수 [ 6.7, 5.9, 8.1, 7.9, 6.7, 7.3, 7.2, 8.2, 6.2, 5.8 ]
현재 상위권 순위 점수 [ 9.12, 8.95, 8.12, 6.90, 6.18]
#최댓값 모듈 (maxAlgorithm)
class MaxAlgorithm:
def __init__(self, ss):
self.scores = ss
self.maxScore = 0
self.maxIdx = 0
def removeMaxScore(self):
self.maxScore = self.scores[0]
for i, s in enumerate(self.scores):
if self.maxScore < s:
self.maxScore = s
self.maxIdx = i
print(f'self.maxScore: {self.maxScore}')
print(f'self.maxIdx: {self.maxIdx}')
self.scores.pop(self.maxIdx)
print(f'scores: {self.scores}')
#최솟값모듈 (minAlgorithm)
class MinAlgorithm:
def __init__(self, ss):
self.scores = ss
self.minScore = 0
self.minIdx = 0
def removeMinScore(self):
self.minScore = self.scores[0]
for i, s in enumerate(self.scores):
if self.minScore > s:
self.minScore = s
self.minIdx = i
print(f'self.minScore: {self.minScore}')
print(f'self.minIdx: {self.minIdx}')
self.scores.pop(self.minIdx)
print(f'scores: {self.scores}')
#근삿값모듈 (nearAlgorithm)
class Top5Players:
def __init__(self, cts, ns):
self.currentScores = cts
self.newScore = ns
def setAlignScore(self):
nearIdx = 0
minNum = 10.0
for i, s in enumerate(self.currentScores):
absNum = abs(self.newScore - s)
if absNum < minNum:
minNum = absNum
nearIdx = i
if self.newScore >= self.currentScores[nearIdx]:
for i in range(len(self.currentScores)-1, nearIdx, -1):
self.currentScores[i] = self.currentScores[i-1]
self.currentScores[nearIdx] = self.newScore
else:
for i in range(len(self.currentScores)-1, nearIdx+1, -1):
self.currentScores[i] = self.currentScores[i-1]
self.currentScores[nearIdx+1] = self.newScore
def getFinalTop5Scroes(self):
return self.currentScores
#싫행
import maxAlgorithm, minAlgorithm
import nearAlgorithm
top5Scores = [9.12, 8.95, 8.12, 6.90, 6.18]
scores = [6.7, 5.9, 8.1, 7.9, 6.7, 7.3, 7.2, 8.2, 6.2, 5.8]
print(f'scores: {scores}')
maxA = maxAlgorithm.MaxAlgorithm(scores)
maxA.removeMaxScore() #최댓값 삭제
minA = minAlgorithm.MinAlgorithm(scores)
minA.removeMinScore() #최솟값 삭제
total = 0
average = 0
for n in scores:
total += n
average = round(total / len(scores), 2) #평균 도출
print(f'total: {round(total, 2)}')
print(f'average: {average}')
tp = nearAlgorithm.Top5Players(top5Scores, average)
tp.setAlignScore()
top5Scores = tp.getFinalTop5Scroes() #새로운순위
print(f'top5Scores: {top5Scores}')
➰ 18일차 후기
어제에 이어 알고리즘 관련 문제를 풀었다.
어려워서 기억해둬야지 했던 코드들이, 다음 날만 되면 또 잊어버리고 만다.
계속 보는 것 밖에 답이 없지 싶다..(하지만 시간이 없어,,)
이제 파이썬에 대한 기본적인 내용들은 마무리하고, EDA, 웹크롤링 등 다음 파트를 시작하게 되었다.
첫날이니 오리엔테이션으로 이런저런 프로그램들을 설치했는데, 아직까지 뭐가 뭔지 모르겠지만 다루다보면 익숙해지지 않을까 싶다.
※본 내용은 제로베이스 데이터 취업 스쿨에서 제공하는 학습 내용에 기반합니다.