Last time It's the 4th day.
#4 Problem Hooray. B problem
** Thoughts ** Sort the entered [City Name, Population] by population to determine if there is a majority of the largest towns. In my case, it's a two-dimensional array like [town name, population], so I can't sort normally, so
s.sort(key=lambda x: int(x[1]),reverse=True)
Sorted in. Reference page I like it because it's fashionable and cool. You can sort by specifying the index of the element with x: int (x [n]). This time, I want to sort by population, so x [1], and I want to sort by population, so reverse = True.
Population was calculated with the for statement.
population = 0
for i in range(n):
population += int(s[i][1])
I wrote the variable name properly in an easy-to-understand manner. It's a little long though. All you have to do is put it in the if statement.
n = int(input())
s = [list(input().split()) for _ in range(n)]
population = 0
for i in range(n):
population += int(s[i][1])
s.sort(key=lambda x: int(x[1]),reverse=True)
if int(s[0][1]) > population//2:
print(s[0][0])
else:
print('atcoder')
Finally, be careful of misspellings in print ('atcoder').
I think that even the B problem is easy. I'm wondering whether to post tomorrow because there is a contest. ~~ Post every day ~~ see you
Recommended Posts