0425 자료구조 4일차(~연습10)

박영선·2023년 4월 27일
0

튜플 연습문제01

자주 접속하는 웹사이트 비번 튜플 저장
(튜플은 수정이 안되므로 수정되면 안되는 중요한 정보를 튜플에 저장)

passwds = 'password1234','abc123','qwerty','letmein','welcome00' #괄호없어도 튜플 가능
print(passwds)
print(type(passwds))

졸업학점 4.0을 위해 받아야하는 4학년 1,2학기 최소학점

scores = ((3.7, 4.2), (2.9, 4.3), (4.1,4.2))
total = 0

for s1 in scores:
    for s2 in s1:
        total += s2

total = round(total,1)
avg = round(total/6,1)
print(f'3학년 총학점 : {total}')
print(f'3학년 평균학점 : {avg}')

grade4TargetScore = round(4.0 * 8 -total,1)  #전체 평균 4학점
print(f'4학년 목표 총학점 : {grade4TargetScore}')
minScore = round(grade4TargetScore/2,1)
print(f'4학년 학기당 목표 학점 :{minScore}')

scores=list(scores)   #4학년 학점 넣기위해 리스트로 변환
scores.append([minScore,minScore]) #학기 두개니까 두번
scores = tuple(scores)

print(scores)
print(type(scores))

튜플 연습문제02

튜플의 합집합과 교집합 출력

tuple1 = (1,3,2,6,12,5,7,8)
tuple2 = (0,5,2,9,8,6,17,3)

tempHap = list(tuple1)
tempGyo = list()

for n in tuple2:
    if n not in tempHap:  #tuple1에 없으면 추가해서 합집합 만들기
        tempHap.append(n)
    else:                  #tuple1에 있는 애들만 골라서 교집합 만들기
        tempGyo.append(n)

tempHap = tuple(sorted(tempHap))
tempGyo = tuple(sorted(tempGyo))

print(tempHap)
print(tempGyo)

while문 쓰기

empHap = tuple1 + tuple2
tempGyo = list()

tempHap = list(tempHap)

idx = 0
while True:

    if idx >=len(tempHap):
        break

    if tempHap.count(tempHap[idx]) >= 2:
        tempGyo.append(tempHap[idx])
        tempHap.remove(tempHap[idx])
        continue

    idx +=1

tempHap = tuple(sorted(tempHap))
tempGyo = tuple(sorted(tempGyo))

print(tempHap)
print(tempGyo)

튜플 연습문제03

아이템 슬라이스

시험점수 입력후 튜플에 저장하고 과목별 학점 출력

튜플 연습문제04

오름차순으로 바꾸기

fruits = ({'수박':8}, {'포도':13},{'참외':12},{'사과':17},{'자두':19},{'자몽':15})

fruits = list(fruits)

cIdx = 0; nIdx = 1; eIdx=len(fruits)-1

flag = True
while flag:
    curDic = fruits[cIdx]
    nextDic = fruits[nIdx]
    curDicCnt = list(curDic.values())[0]  #해당 딕셔너리의 과일 개수
    nextDicCnt = list(nextDic.values())[0]

    if nextDicCnt < curDicCnt:
        fruits.insert(cIdx, fruits.pop(nIdx))
        nIdx = cIdx +1
        continue

    nIdx +=1
    if nIdx > eIdx:
        cIdx +=1
        nIdx = cIdx +1

        if cIdx ==5:
            flag = False

print(tuple(fruits))

튜플 연습문제05

studentCnt = ({'cls01':18},{'cls02':21},{'cls03':20},
              {'cls04': 19},{'cls05':22},{'cls06':20},{'cls07':23},{'cls08':17})

totalCnt = 0
minStudentCnt = 0; minCls = ''#학급은 문자열이므로
maxStudentCnt = 0; maxCls = ''
deviation = [] #편차

for idx, dic in enumerate(studentCnt):
    for k,v in dic.items():
        totalCnt = totalCnt + v

        if maxStudentCnt < v:
            maxStudentCnt =v
            maxCls = k

        if minStudentCnt ==0 or minStudentCnt> v:
            minStudentCnt = v
            minCls = k

print(f'전체학생수 : {totalCnt}')
print(f'평균학생수 : {int(totalCnt/len(studentCnt))}')

print(f'최소인원 학급 {minCls}반, {minStudentCnt}명')
print(f'최대인원 학급 {maxCls}반, {maxStudentCnt}명')

for idx,dic in enumerate(studentCnt):
    for k,v in dic.items():
        deviation.append(v - int(totalCnt/len(studentCnt)))

print(f'학급별 학생 편차 :{deviation}')
profile
데이터분석 공부 시작했습니다

0개의 댓글