Skip to content

Instantly share code, notes, and snippets.

@p3nj
Created August 10, 2018 08:11
Show Gist options
  • Save p3nj/3258684a3370be52e1b24357e161f521 to your computer and use it in GitHub Desktop.
Save p3nj/3258684a3370be52e1b24357e161f521 to your computer and use it in GitHub Desktop.
[加分題] 動物星球
'''
猴子 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