본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 37

반응형

Q>

10명으로 구성된 학급에 반장 선거를 한다.

10명의 투표내용을 기호순으로 입력 받고 후보별 득표 수를 인쇄하여 최다득표자의 기호를 인쇄하시오.


A>

def str2int(slist):
nlist = []
for s in slist:
nlist.append(int(s))
return nlist

def countvotes(votes, numbers):
earns = [0] * numbers
no = 0
for vote in votes:
index = vote - 1
if 0 <= index < numbers:
earns[index] += 1
else:
no += 1
return [earns, no]

def findmax_index(earns):
max = 0
for index in range(0, len(earns)):
if max < earns[index]:
max = earns[index]
max_index = index
return max_index

numbers = int(input("총 후보자가 몇 명입니까? "))
votes = input("투표내용: ").split()
votes = str2int(votes)

earns_no = countvotes(votes, numbers)
earns = earns_no[0]
no = earns_no[1]

for index in range(0, len(earns)):
number = index + 1
print("기호: {} 득표 수: {}".format(number, earns[index]))
print("무효 표: {}".format(no))
index = findmax_index(earns)
print("최다 득표자: {}".format(index + 1))


O>

총 후보자가 몇 명입니까? 10

투표내용: 7 2 4 5 6 8 10 2 3 4

기호: 1 득표 수: 0

기호: 2 득표 수: 2

기호: 3 득표 수: 1

기호: 4 득표 수: 2

기호: 5 득표 수: 1

기호: 6 득표 수: 1

기호: 7 득표 수: 1

기호: 8 득표 수: 1

기호: 9 득표 수: 0

기호: 10 득표 수: 1

무효 표: 0

최다 득표자: 2


Process finished with exit code 0

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 39  (0) 2019.06.08
Python Learn the basics Quiz 38  (0) 2019.06.08
Python Learn the basics Quiz 36  (0) 2019.06.07
Python Learn the basics Quiz 35  (0) 2019.05.25
Python Learn the basics Quiz 34  (0) 2019.05.12