I
I
Ivan Lee2020-07-23 03:04:37
Python
Ivan Lee, 2020-07-23 03:04:37

Task from stepik. How do I fix the listing issue?

5f18d2e5c4f1b930010115.png
5f18d2da31efb489747523.png
I suffered, put on crutches, and still it doesn’t work. The mistake is that the results of one team (Lokomotiv) change with the results of the second team (Spartak).

n = int(input())
teams = dict()

for i in range(n):
    team1, r1, team2, r2= input().split(';')

    if team1 not in teams.keys():
        teams[team1] = [0, 0, 0, 0, 0]

    if team2 not in teams.keys():
        teams[team2] = [0, 0, 0, 0, 0]       
 
    teams[team1][0] += 1
    teams[team2][0] += 1

    if r1> r2:
        teams[team1][1] += 1
        teams[team1][4] += 3
        teams[team2][3] += 1
    elif r1< r2: 
        teams[team2][1] += 1
        teams[team2][4] += 3
        teams[team1][3] += 1
    else:
        teams[team2][2] += 1
        teams[team2][4] += 1
        teams[team1][2] += 1
        teams[team1][4] += 1      
    
for key, value in teams.items():
    print(key, end=':')

    for v in value:
        print(v, end=' ')

    print()

At first I thought the problem was in the output (I couldn’t do it myself, I looked in the comments), then I made a crutch, and it also output. I will be grateful for any help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gorodnev, 2020-07-23
@appliks

The most basic problem in your code is incorrect type comparison.
Here in this line you get the name of the team and its points, but all these variables are of type string.

team1, r1, team2, r2=input().split(';')

And they are compared, respectively, in lexicographic order. For example,
try comparing 119 and 12 both as numbers and as strings ('119' and '12'). The results may surprise :)
My solution:
spoiler
import collections

results = collections.defaultdict(list)
n = int(input())
for _ in range(n):
  team1, score1, team2, score2 = input().strip().split(';')
  score1 = int(score1)
  score2 = int(score2)
  if score1 == score2:
    results[team1].append(1)
    results[team2].append(1)
    continue

  if score1 > score2:
    results[team1].append(3)
    results[team2].append(0)
  else:
    results[team2].append(3)
    results[team1].append(0)

for team, scores in results.items():
  games = str(len(scores))
  wins = str(scores.count(3))
  draws = str(scores.count(1))
  loses = str(scores.count(0))
  total = str(sum(scores))
  print('%s:%s' % (team, ' '.join((games, wins, draws, loses, total))))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question