0

Inputs are

5 6 7
3 6 10

I'm trying to compare these two vectors and add a tally system to whoever has more. When returning the loop, it gives more than I anticipate.

My code is showing that it iterated three times more than it should have. I am uncertain about the return of the global function. Another thread mentioned a tuple is the result, I don't have that problem.

Alice_wins=0
Bob_wins=0

def compareTriplets(a, b):
    global Alice_wins
    global Bob_wins
    # Write your code here
    for ele in a:
        for i in b:
            if ele>i:
                Alice_wins+=1
            elif ele<i:
                Bob_wins+=1
            else:
                pass
    return Alice_wins, Bob_wins       
                
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    a = list(map(int, input().rstrip().split()))

    b = list(map(int, input().rstrip().split()))

    result = compareTriplets(a, b)

    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')

    fptr.close()
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Jon370
  • 3
  • 2
  • "My code is showing that it iterated three times more than it should have." How many times is that? How many times do you think it should iterate? What do you think should happen each time? What should be the final result, and what is the final result? (Also, we don't have "threads" here - this is **not a discussion forum**.) – Karl Knechtel Jan 31 '23 at 18:18
  • Not sure what output you are expecting, but maybe you want to [`zip`](https://docs.python.org/3.3/library/functions.html#zip) the 2 lists together and compare the pairs? – 001 Jan 31 '23 at 18:19
  • Nested loops iterate `M x N` times. You probably want parallel loops using `zip()`. – Barmar Jan 31 '23 at 18:19
  • @KarlKnechtel my result was 4 4 I wanted to get 1 1. – Jon370 Jan 31 '23 at 18:22
  • @JohnnyMopp ill look into the zip function – Jon370 Jan 31 '23 at 18:23
  • An alternative to `zip()` is to use a for loop: `for index in len(a): if a[index] > b[index] ...`. Your problem right now is you're comparing EVERY value in a to EVERY value in b. That's 9 comparisons, of which 8 will result in a 'winner' (since there is only one tie). – Nathaniel Ford Jan 31 '23 at 18:24
  • @NathanielFord oooohhhh that makes more sense I have to specify the index of both for loops. Also thank you for reformatting my question. I tried to understand the "duplicate" discussion but i struggle with comprehensions as of now – Jon370 Jan 31 '23 at 19:52

0 Answers0