Created
August 10, 2018 08:11
-
-
Save p3nj/3258684a3370be52e1b24357e161f521 to your computer and use it in GitHub Desktop.
[加分題] 動物星球
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
猴子 2 樹上 | |
蛇 2 地上 | |
烏龜 4 水裡 | |
熊 1 樹上 | |
猴子 1 地上 | |
蛇 3 樹上 | |
猴子 3 樹上 | |
OUTPUT: | |
樹上:猴子 5,蛇 3,熊 1 | |
地上:蛇 2,猴子 1 | |
水裡:烏龜 4 | |
''' | |
d = {} | |
while True: | |
#for totnum in range(0,int(input('Input the total number'))): | |
try: | |
anm, num, loc = input().split() | |
except: | |
break | |
if loc in d: | |
for i in d[loc]: | |
if i == anm: | |
total = int(d[loc][d[loc].index(i)+1]) + int(num) | |
d[loc][d[loc].index(i)+1] = str(total) | |
if anm not in d[loc]: | |
d[loc] += [anm, num] | |
else: | |
d[loc] = [anm, num] | |
for k, v in d.items(): | |
temp = [] | |
stemp = [] | |
for i in range(0, len(v)): | |
if i % 2 == 0: | |
values = v[i] + ' ' + v[i+1] | |
temp.append(values) | |
for j in sorted(temp, key=lambda x : int(x[-1:]), reverse=True): | |
stemp.append(j) | |
val = ','.join(stemp) | |
print(k + ':' + val) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment