Fuxy
Fuxy

Reputation: 1

My list comparator doesn't want to run the way I want it to

I'm a beginer in python and I have a task where the winner_round function compares two lists and count how many rounds there were in the game in which Adam's team scored more points than the opponent. If the two list don't match then return with -1 This is my code:

def winner_round(list1,list2):
    list1 = [30, 50, 10, 80, 100, 40]
    list2 = [60, 20, 10, 20, 30, 20]
    point = 0
    for i in winner_round(list1,list2):
        if list1>list2:
            return -1
            print(-1)
    for pointA in list1:
        for pointE in list2:
            if pointA > pointE:
                point+=1
        break
    return(point)
    print(point)

Sorry for my english

Upvotes: 0

Views: 50

Answers (4)

chepner
chepner

Reputation: 531275

The only reason to return -1 is if the lists have different sizes; you can check that with len, an O(1) operation, before you bother iterating.

After that, it's just a matter of point-wise comparisons of list items. Assuming list1 is Adam and list2 his opponent,

def winner_round(list1, list2):
    if len(list1) != len(list2):
        return -1

    return sum(x > y for x, y in zip(list1, list2))

zip(list1, list2) produces the pairs (30, 60), (50, 20), etc. Because True == 1 and False == 0 (bool being a subclass of int), you can simply sum the results where the value from list1 is the greater value of a pair.

(You can also use map, since a 2-argument function as the first argument allows you to pass the two lists as the 2nd and 3rd arguments, replacing the need for more explicit iteration over the zip instance. operator.gt provides the function you need:

return sum(map(operator.lt, list1, list2))

Which one is "better" is a matter of personal preference.)

Upvotes: 2

lightstack
lightstack

Reputation: 321

This would be my solution to your problem:

    def get_points(teamA,teamB):
    if len(teamA)!=len(teamB):
        print("Both lists have to be of same length.")
        return -1
    points=0
    for i in range(len(teamA)):
        if teamA[i]>teamB[i]:
            points+=1

    print("points:",points)
    return points

I first checked, whether both of the lists have the same length and then looped through both of the lists and increased a counter if one is bigger than the other one.(Btw you tried to print something after the return statement, which exists the function) Hope it helps, Lars

Upvotes: 0

Ironkey
Ironkey

Reputation: 2607

sum of points:

list1 = [30, 50, 10, 80, 100, 40]
list2 = [60, 20, 10, 20, 30, 20]

def winner_round(list1,list2):
    if sum(list1) > sum(list2):
        return 1
    else:
        return 2

comparing each round:

list1 = [30, 50, 10, 80, 100, 40]
list2 = [60, 20, 10, 20, 30, 20]

def winner_round(list1,list2):
    if len(list1) != len(list2): return -1
    score1, score2 = 0
    for i in range(list1):
        if list1[i] > list2[i]:
            score1 +=1
        else:
            score2 +=1
    if score1 > score2:
        return 1
    else:
        return 2

Upvotes: 0

Equinox
Equinox

Reputation: 6748

Compare each item one by one if it's greater than the other then add 1 to point

list1 = [30, 50, 10, 80, 100, 40]
list2 = [60, 20, 10, 20, 30, 20]

def winner_round(list1,list2):
  point = 0

  if len(list1)!=len(list2):
    return -1

  for i in range(len(list1)):
      if list1[i] > list2[i]:
          point+=1
  return point

Upvotes: 0

Related Questions