Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the winner of a game where scores are given as a binary string in Python
When analyzing volleyball match scores represented as binary strings, we need to determine the winner based on specific scoring rules. In this scenario, 1 represents our team scoring a point, while 0 represents the opponent scoring a point.
Game Rules
The volleyball match follows these conditions:
The first team to reach 15 points wins the match
Exception: If both teams reach 14 points, the winner must maintain a 2-point lead
Algorithm Steps
To solve this problem, we follow these steps:
Track scores for both teams using a counter array
Process each character in the binary string to update scores
Check for early wins (15 points with opponent having < 14 points)
Handle the deuce situation (both teams at 14 points)
In deuce, continue until one team has a 2-point advantage
Implementation
def predictWinner(score, n):
score_cnt = [0, 0] # [opponent_score, our_team_score]
for i in range(len(score)):
pos = int(score[i]) # Convert '0' or '1' to integer
score_cnt[pos] += 1
# Check if opponent wins early (reaches n points first)
if score_cnt[0] == n and score_cnt[1] < n - 1:
return "Team lost"
# Check if our team wins early (reaches n points first)
if score_cnt[1] == n and score_cnt[0] < n - 1:
return "Team won"
# Check for deuce situation (both teams at n-1 points)
if score_cnt[0] == n - 1 and score_cnt[1] == n - 1:
score_cnt[0] = 0
score_cnt[1] = 0
break
# Continue from deuce - need 2-point advantage
i += 1
for i in range(i, len(score)):
pos = int(score[i])
score_cnt[pos] += 1
# Check for 2-point advantage
if abs(score_cnt[0] - score_cnt[1]) == 2:
if score_cnt[0] > score_cnt[1]:
return "Team lost"
else:
return "Team won"
# Test the function
score = "1001010101111011101111"
n = 15
print(predictWinner(score, n))
Team won
Example Breakdown
Let's trace through the example score "1001010101111011101111":
score = "1001100110111001110011011"
n = 15
def trace_game(score, n):
score_cnt = [0, 0]
print(f"Processing score: {score}")
print("Position | Our Team | Opponent | Status")
print("-" * 40)
for i in range(len(score)):
pos = int(score[i])
score_cnt[pos] += 1
status = f"Playing ({score_cnt[1]} - {score_cnt[0]})"
if score_cnt[0] == n and score_cnt[1] < n - 1:
status = "Team lost"
break
elif score_cnt[1] == n and score_cnt[0] < n - 1:
status = "Team won"
break
elif score_cnt[0] == n - 1 and score_cnt[1] == n - 1:
status = "Deuce! Resetting..."
print(f"{i+1:8d} | {score_cnt[1]:8d} | {score_cnt[0]:8d} | {status}")
score_cnt[0] = 0
score_cnt[1] = 0
continue
print(f"{i+1:8d} | {score_cnt[1]:8d} | {score_cnt[0]:8d} | {status}")
return status
result = trace_game(score, n)
print(f"\nFinal Result: {result}")
Processing score: 1001100110111001110011011
Position | Our Team | Opponent | Status
----------------------------------------
1 | 1 | 0 | Playing (1 - 0)
2 | 1 | 1 | Playing (1 - 1)
3 | 1 | 2 | Playing (1 - 2)
4 | 2 | 2 | Playing (2 - 2)
5 | 3 | 2 | Playing (3 - 2)
6 | 3 | 3 | Playing (3 - 3)
7 | 3 | 4 | Playing (3 - 4)
8 | 4 | 4 | Playing (4 - 4)
9 | 5 | 4 | Playing (5 - 4)
10 | 5 | 5 | Playing (5 - 5)
11 | 6 | 5 | Playing (6 - 5)
12 | 7 | 5 | Playing (7 - 5)
13 | 8 | 5 | Playing (8 - 5)
14 | 8 | 6 | Playing (8 - 6)
15 | 8 | 7 | Playing (8 - 7)
16 | 9 | 7 | Playing (9 - 7)
17 | 10 | 7 | Playing (10 - 7)
18 | 11 | 7 | Playing (11 - 7)
19 | 11 | 8 | Playing (11 - 8)
20 | 11 | 9 | Playing (11 - 9)
21 | 12 | 9 | Playing (12 - 9)
22 | 13 | 9 | Playing (13 - 9)
23 | 13 | 10 | Playing (13 - 10)
24 | 14 | 10 | Playing (14 - 10)
25 | 15 | 10 | Team won
Final Result: Team won
Key Points
Early Win: First team to reach 15 points wins if opponent has < 14 points
Deuce Handling: When both teams reach 14 points, reset counters and require 2-point advantage
Binary Interpretation: '1' = our team scores, '0' = opponent scores
Efficient Processing: Check win conditions after each point to avoid unnecessary computation
Conclusion
This algorithm efficiently determines the volleyball match winner by tracking scores and applying the standard 15-point rule with deuce handling. The solution processes each point sequentially and checks win conditions immediately to optimize performance.
