Program Language/python

์„ฑ์ ์ฒ˜๋ฆฌ ํ”„๋กœ๊ทธ๋žจ - index ๊ฐ’์œผ๋กœ ํ‰๊ท , ์„์ฐจ ๊ตฌํ•˜๊ธฐ

๊ฐœ๋ฐœํ•˜๋Š” ์ฃผ๋””์”จ 2022. 12. 2. 14:50

 

ํŒŒ์ด์ฌ์„ ์ด์šฉํ•œ ์„ฑ์ ์ฒ˜๋ฆฌ ํ”„๋กœ๊ทธ๋žจ
<์ ์ˆ˜ ํ‰๊ท ์„ ์ด์šฉํ•œ ์„์ฐจ ๊ตฌํ•˜๊ธฐ- index ํ™œ์šฉ>

 

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฐฐ๊ฒฝ

3๋ช…์˜ ํ•™์ƒ๋“ค์€ ๊ฐ๊ฐ ์ค‘๊ฐ„๊ณ ์‚ฌ์™€ ๊ธฐ๋ง๊ณ ์‚ฌ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด ๋‘ ๊ฐœ์˜ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐœ์ธ์˜ ์„์ฐจ๋ฅผ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ์„์ฐจ๋Š” ๋‘ ๊ฐ€์ง€ ์‹œํ—˜ ์ ์ˆ˜(์ค‘๊ฐ„๊ณ ์‚ฌ, ๊ธฐ๋ง๊ณ ์‚ฌ)์˜ ํ‰๊ท ์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค. 

 

ํ•„์ˆ˜ ์กฐ๊ฑด

* ํ•™๋ฒˆ, ์ด๋ฆ„, ์ค‘๊ฐ„๊ณ ์‚ฌ ์ ์ˆ˜, ๊ธฐ๋ง๊ณ ์‚ฌ ์ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.

* Score ํด๋ž˜์Šคํ˜• ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

* ์ƒ์„ฑ์ž ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

๊ตฌํ˜„ ํฌ์ธํŠธ
์ตœ๋Œ€ํ•œ ์„์ฐจ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์ž์ฒด ์ธ๋ฑ์Šค ๊ฐ’์„ ์„์ฐจ๋กœ ํ™œ์šฉํ•œ ์˜ˆ์ œ์ด๋‹ค. 
ํ•ต์‹ฌ์€ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ key์™€ value๋กœ ์ด๋ฆ„๊ณผ ์ ์ˆ˜๋ฅผ mapping ํ•ด์ฃผ์–ด index๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋•Œ, ์ƒ๊ฐํ•ด๋ด์•ผ ํ•  ๋ฌธ์ œ๋กœ๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ฆฌ์ŠคํŠธํ™” ํ•˜์—ฌ์•ผ index์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ํฌ์ธํŠธ์ด๋‹ค.

 

ํŒŒ์ด์ฌ ๊ธฐ์ดˆ์— ์ถฉ์‹คํ•˜๊ฒŒ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์ด๋‹ค. ์ถ”ํ›„ develop ํ•ด์•ผ ํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ˆ ์ฐธ๊ณ ์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜์ž. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” ํ•™์ƒ 3๋ช…์˜ ์ ์ˆ˜๊ฐ€ ๋ชจ๋‘ ์ž…๋ ฅ๋˜์–ด์•ผ๋งŒ ์—ฐ์‚ฐ์œผ๋กœ ํ•˜๋„๋ก ํ•˜์˜€์œผ๋ฉฐ, ์ค‘๊ฐ„์— ๋ฐœ์ƒํ•˜๋Š” ์ด์Šˆ์— ๋Œ€ํ•ด์„œ๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์•˜๋‹ค.

 

๋˜ํ•œ ์„์ฐจ ๊ณ„์‚ฐ ์‹œ ์ค‘์š”ํ•˜๊ฒŒ ๋‹ค๋ค„์ง€๋Š” ๋™์„์ฐจ์— ๊ด€๋ จํ•ด์„œ๋„ ์ด๋ฒˆ ์ฝ”๋“œ์—์„œ๋Š” ๊ณ ๋ ค๋Œ€์ƒ์œผ๋กœ ์„ ์ •ํ•˜์ง€ ์•Š์•˜๋‹ค. ๋‹จ์ˆœํžˆ ๊ธฐ๋ณธ ์„์ฐจ ํ๋ฆ„์— ๋Œ€ํ•œ ์ดํ•ด ์œ„์ฃผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

 

Class Score:
    num = 0
    name = ""
    mid = 0
    end = 0
    avg = 0
    reselut_dic = {}
    
    def __init(self, no, name, mid, end):
    	self.no = no
        self.name = name
        self.mid = mid
        self.end = end

	def getName(self):
    	return self.name
    
    def getAvg(self):
    	Score.avg = float(float(self.mid) + float(self.end)/2.0)
        Score.reselt_dic[self.name] = Score.avg
        
    def getRank(self):
    	tmp = list(Score.result_dic.items()) #๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธํ™”
        tmp.sort(key=lambda x:x[1], reverse=True) #์›๋ณธ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ
        return tmp.index((self.getName(), self.getAvg()))+1 #์ธ๋ฑ์Šค ์‹œ์ž‘์ด 0๋ถ€ํ„ฐ ์ด๋ฏ€๋กœ +1
        
st1, st2, st3 = None, None, None

##๋ฉ”์ธ์ฝ”๋“œ ๋ถ€๋ถ„##
for i in range(3):
    num = input("ํ•™๋ฒˆ :")
    name = input("์ด๋ฆ„ :")
    mid = input("์ค‘๊ฐ„๊ณ ์‚ฌ ์ ์ˆ˜ :")
    end = input("๊ธฐ๋ง๊ณ ์‚ฌ ์ ์ˆ˜ :")
    
    if i == 0:
    	st1 = Score(num, name, mid, end)
    if i == 1:
    	st2 = Score(num, name, mid, end)
    if i == 2:
    	st3 = Score(num, name, mid, end)

print("%s์˜ ํ‰๊ท ์ ์ˆ˜๋Š” %d์ž…๋‹ˆ๋‹ค" % (st1.getName(), st1.getAvg()))
print("%s์˜ ํ‰๊ท ์ ์ˆ˜๋Š” %d์ž…๋‹ˆ๋‹ค" % (st2.getName(), st2.getAvg()))
print("%s์˜ ํ‰๊ท ์ ์ˆ˜๋Š” %d์ž…๋‹ˆ๋‹ค" % (st3.getName(), st3.getAvg()))
print("=========================================================")
print("%s์˜ ์„์ฐจ๋Š” %d๋“ฑ ์ž…๋‹ˆ๋‹ค" % (st1.getName(), st1.getRank()))
print("%s์˜ ์„์ฐจ๋Š” %d๋“ฑ ์ž…๋‹ˆ๋‹ค" % (st2.getName(), st2.getRank()))
print("%s์˜ ์„์ฐจ๋Š” %d๋“ฑ ์ž…๋‹ˆ๋‹ค" % (st3.getName(), st3.getRank()))