DEV Community

Abdulla Ansari
Abdulla Ansari

Posted on • Edited on

HackerRank Football Scores Python Solution

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))
Enter fullscreen mode Exit fullscreen mode

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.

By me a Coffee

My other Blog Website over Technology

My YouTube Vlog Channel

Top comments (4)

Collapse
 
alexedbalet profile image
alexedbalet

The Football Scores problem on HackerRank is a common coding question asked by top product-based companies like Meesho and Adobe. Given two lists, teamA and teamB, the goal is to count how many scores in teamA are less than or equal to each score in teamB. A Pythonic solution involves sorting teamA and using binary search for each score in teamB, which greatly improves efficiency. For example, using teamA = [1, 2, 3] and teamB = [2, 4], we find that for 2 goals, there are 2 matches in teamA (1 and 2), and for 4 goals, there are 3 matches (1, 2, 3), resulting in [2, 3]. This approach is similar to how apps like Yacine TV optimize their streaming servers—by matching conditions quickly and efficiently to provide smooth experiences.

Collapse
 
praveenkuruvangipara profile image
Praveen

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)

Collapse
 
stevemicheal profile image
Steve Micheal

Nice Post

Some comments may only be visible to logged-in visitors. Sign in to view all comments.