So Football Score problem is asked in many top level product base company like Meesho, Adobe etc.
So here we have came across with the solution of this problem in a more pythonic way and it is easily understandable if you have basic idea of programming and logical idea. So let's get started with the problem statement.
Problem Statement
The number of goals achieved by two football
teams in matches in a league is given in the form
of two lists. For each match of team B, compute
the total number of matches of team A where
team A has scored less than or equal to the
number of goals scored by team B in that match.
Example
teamA = [1, 2, 3]
teamB = [2, 4]
Team A has played three matches and has scored
teamA = [1, 2, 3] goals in each match respectively.
Team B has played two matches and has scored
teamB = [2, 4] goals in each match respectively.
For 2 goals scored by team B in its first match,
team A has 2 matches with scores 1 and 2. For 4
goals scored by team B in its second match, team
A has 3 matches with scores 1, 2 and 3. Hence,
the answer is [2, 3).
def counts(teamA, teamB):
answer = []
teamA.sort()
for score in teamB:
low, high = 0, len(teamA) - 1
while low <= high:
mid = (low + high) // 2
if teamA[mid] > score:
high = mid - 1
else:
low = mid + 1
answer.append(low)
return answer
l1 = [1,2,3]
l2 = [2,4]
print(counts(l1, l2))
So this was the simplest solution for this problem you can also try different approaches to solve this problem in a efficient way.
Thanks for your valuable time. You can like my post and
you can.
Top comments (1)
def counts(teamA,teamB):
ans = []
for i in range(len(teamB)):
val =0
for j in range(len(teamA)):
if teamA[j] <= teamB[i]:
val += 1
ans.append(val)
return ans
l1 = [1,2,3]
l2 = [2,4]
counts(l1,l2)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.