hacker rank solution Find the Winner using Python
Find the Winner using Python
At the beginning of the game, someone will call out "Even" or "Odd". The first move depends on which is called. If "Even" is called, the player's top cards are flipped so they can see the face value. If instead "Odd" is called, the top card is removed from each deck and discarded, then each flips her next card. Andrea subtracts the value of Maria's card from her own and adds the result to her score. Likewise, Maria subtracts the value of Andrea's card from her own and adds the result to her score.
From this point forward, each alternately discards then flips a card. Each time two cards are flipped, the players' scores are computed as before. Once all the cards have been played, whoever has the most points wins.
As an example, Maria's cards, face down, are [3, 5, 6] and Andrea's are [4, 5, 7]. After calling "Even" at random, the game begins. The following table represents game play with cumulative score at the bottom. Maria's score is -2, Andrea's is +2 so Andrea wins.
You must determine the name of the winner if there is one, otherwise they tie. Return the string Andrea, Maria or Tie.
Function Description
Complete the function winner in the editor below. The function must return a string denoting the outcome of the game. Return Andrea if Andrea won, or return Maria if Maria won. If their scores are tied, return Tieinstead.
winner has the following parameter(s):
andrea: An array of n integers that denotes Andrea's array of card values.
maria: An array of n integers that denotes Maria's array of card values.
s: A string that represents the starting called out word
Constraints
- 2 ≤ n ≤ 105
- 1 ≤ a[i], m[i] ≤ 103, where 0 ≤ i < n
- String s will either the word Odd or the word Even.
Input Format For Custom Testing
Sample Case 0
Sample Input 0
3
1
2
3
3
2
1
3
Even
Sample Output 0
Maria
Explanation 0
In this game, andrea = [1, 2, 3] and maria = [2, 1, 3]. Because s = Even, the only cards flipped are at indexes 0 and 2.
- When i = 0, Andrea gets a[0] − m[0] = 1 − 2 = -1 point and Maria gets m[0] − a[0] = 2 − 1 = 1 point.
- When i = 2, Andrea gets a[2] − m[2] = 3 − 3 = 0 points and Maria gets m[2] − a[2] = 3 − 3 = 0 points.
At the end of play, Andrea's cumulative score is -1 and Maria's is 1 so Maria wins.
Sample Case 1
Sample Input 1
3
1
2
3
3
2
1
3
Odd
Sample Output 1
Andrea
Explanation 1
In this game, andrea = [1, 2, 3] and maria = [2, 1, 3]. Because s = Odd, the only indices we can choose during moves are in the set {1}. The game proceeds like so:
- When i = 1, Andrea gets a[1] − m[1] = 2 − 1 = 1 point and Maria gets m[1] − a[1] = 1 − 2 = -1 point.
Andrea ends with 1 point, and Maria with -1. Andrea wins.
#!/bin/python3 # # Complete the 'winner' function below. # # The function is expected to return a STRING. # The function accepts following parameters: # 1. INTEGER_ARRAY andrea # 2. INTEGER_ARRAY maria # 3. STRING s # def winner(andrea, maria, s): def winner(andrea,maria,s): reamariatuplelist=[] reascore=0 mariascore=0 if s=='Odd': for x in range(1,len(andrea),2): andreamariatuplelist.append((andrea[x],maria[x])) if s=='Even': for x in range(0,len(andrea),2): andreamariatuplelist.append((andrea[x],maria[x])) print (andreamariatuplelist) for pair in andreamariatuplelist: andreascore+=pair[0]-pair[1] mariascore+=pair[1]-pair[0] if andreascore>mariascore: return "Andrea" if mariascore>andreascore: return "Maria" else: return "Tie"
No comments