DNPotapov / Codewars-katas-

0 stars 0 forks source link

Simple Fun #166: Best Match (5 kyu) #27

Open DNPotapov opened 1 year ago

DNPotapov commented 1 year ago
def best_match(goals1, goals2):
    min_raznica = goals1[0] - goals2[0]
    res = 0
    for i in range(len(goals1)):
        if goals1[i] - goals2[i] < min_raznica:
            res = i
            min_raznica = goals1[i] - goals2[i]
        if (goals1[i] - goals2[i] == min_raznica) and (goals2[i] > goals2[res]):
            res = i
            min_raznica = goals1[i] - goals2[i]
    return res
DNPotapov commented 1 year ago

Task "AL-AHLY" and "Zamalek" are the best teams in Egypt, but "AL-AHLY" always wins the matches between them. "Zamalek" managers want to know what is the best match they've played so far.

The best match is the match they lost with the minimum goal difference. If there is more than one match with the same difference, choose the one "Zamalek" scored more goals in.

Given the information about all matches they played, return the index of the best match (0-based). If more than one valid result, return the smallest index.

Example For ALAHLYGoals = [6,4] and zamalekGoals = [1,2], the output should be 1 (2 in COBOL).

Because 4 - 2 is less than 6 - 1

For ALAHLYGoals = [1,2,3,4,5] and zamalekGoals = [0,1,2,3,4], the output should be 4.

The goal difference of all matches are 1, but at 4th match "Zamalek" scored more goals in. So the result is 4 (5 in COBOL).

Input/Output [input] integer array ALAHLYGoals The number of goals "AL-AHLY" scored in each match.

[input] integer array zamalekGoals The number of goals "Zamalek" scored in each match. It is guaranteed that zamalekGoals[i] < ALAHLYGoals[i] for each element.

[output] an integer Index of the best match.

DNPotapov commented 1 year ago

https://www.codewars.com/kata/58b38256e51f1c2af0000081/train/python