
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find winner of stone game in Python
Suppose Amal and Bimal are playing a game and Amal's turn is first. The game is like below −
There are n stones in a pile. Each player can take a stone from the pile and receive points based on the position of that stone. Amal and Bimal may value the stones in different manner.
We have two arrays of same length, A_Values and B_Values. Each A_Values[i] and B_Values[i] represents how Amal and Bimal, respectively, value the ith stone. Here whose score is maximum, he will be winner after all the stones are taken out. If there is a tie, the game results in a draw. Both players will play optimally. Both of them know the other's values. So if Amal wins, return 1. If Bimal wins, return -1. And for a draw match, return 0.
So, if the input is like A_Values = [2,4] B_Values = [3,5], then the output will be 1 because Amal will select second stone with point 4, so Bimal has only one chance to take first stone with point 3, but as Amal has greater score, so he wins.
To solve this, we will follow these steps −
- n := size of A_Values
- combinedValues := a new list
- for i in range 0 to n, do
- tmpV := A_Values[i] + B_Values[i]
- insert pair (temV, i) into combinedValues at the end
- sort the list combinedValues in reverse order
- score_a := 0, score_b := 0
- for i in range 0 to n - 1, do
- curV := combinedValues[i]
- if i mod 2 is same as 0, then
- score_a := score_a + A_Values[curV[1]]
- otherwise,
- score_b := score_b + B_Values[curV[1]]
- if score_a > score_b, then
- return 1
- otherwise when score_a is same as score_b, then
- return 0
- otherwise,
- return -1
Example
Let us see the following implementation to get better understanding −
def solve(A_Values, B_Values): n = len(A_Values) combinedValues = [] for i in range(n): tmpV = A_Values[i] + B_Values[i] combinedValues.append([tmpV, i]) combinedValues.sort(reverse=True) score_a, score_b = 0, 0 for i in range(n): curV = combinedValues[i] if (i % 2 == 0): score_a += A_Values[curV[1]] else: score_b += B_Values[curV[1]] if (score_a > score_b): return 1 elif (score_a == score_b): return 0 else: return -1 A_Values = [2,4] B_Values = [3,5] print(solve(A_Values, B_Values))
Input
[2,4], [3,5]
Output
1
- Related Articles
- Program to find winner of array removal game in Python
- Program to find winner of number reducing game in Python
- Program to find winner of a rower reducing game in Python
- Program to find winner of a rower breaking game in Python
- Program to find maximum score in stone game in Python
- C++ program to find winner of card game
- Program to find the winner of an array game using Python
- Program to find winner of a set element removal game in Python
- Python program to find score and name of winner of minion game
- C++ program to find winner of cell coloring game
- C++ program to find winner of ball removal game
- C++ Program to find winner of unique bidding game
- C++ Program to find winner name of stick crossing game
- C++ program to find winner of typing game after delay timing
- Program to check whether Amal can win stone game or not in Python
