0
Card shark (python)
Link to the question : https://i.stack.imgur.com/zsA8v.png https://i.stack.imgur.com/Q7frr.png https://i.stack.imgur.com/nuFuM.png https://i.stack.imgur.com/4Bygj.png Can someone help me with this using Python? Any help will be appreciated
14 Respostas
+ 3
A lot work to do!
I begin with first challenge
a player gets all odd or all even cards of a stack.
reduce a stack to a triple (value odds, value even,n).
in the best case he gets always the better part of a stack.
+ 2
https://code.sololearn.com/c2Ctbq4q06v5/?ref=app
since cycling will change nothing but who gets first card, it is enough to permute stack 2 to n.
+ 1
It was a small change.
+ 1
Lets say
2
4,5,6
1,2
If p1 gets first stack, he is better by 2.
But the next is 4+6 -5 = 5 for p2 (he gets first card). so -5 for p1
1,2 is -1 for p1 because first card goes to p1.
Now in sum this sorting of stacks has worth of 2-5-1 =-4 for p1.
m is to find out to whom the first card of stack[n] goes.
+ 1
Please understand that a player either gets all even or all odd cards of a stack.
The difference sum( even)-sum(odds) is the value of a stack if player gets first card of it (index 0). Otherwise the value is to multiply by -1.
On the other hand... it doesn't matter.
Get all possible sortings of the stacks. Flatten the lists of stacks to one list l for each possible sorting.
Result = sum( l[::2])-sum(l[1::2])
Choose best result
A bit more to calculate but easier to understand.
0
Oma Falk Thanks for your trying. I'm still trying. But I have no idea how to arrange the stack of cards so that player 1 can get the highest score
0
Oma Falk But the stacks of cards (input) are provided by the system. So it does not always fulfill your requirements (all odd or all even).
0
Oma Falk But based on the question, player 1 gets the first card and player 2 gets the second card and so on
0
Oma Falk
def evalOpp(opp):
res = 0
m = 0
for element in opp:
print(element)
value, mnew = element[0], element[1] % 2
v = value * ((-1) ** m)
m = (mnew + m) % 2
res += v
return res
What does this part of code do?
0
Oma Falk
https://i.stack.imgur.com/4Bygj.png
Have a look at this.
Not player 1 get first stack and player 2 get second stack. What the question states is player 1 get first card and player 2 get second card from all of those stacks of cards.
0
Oma Falk
Can you help to modify the code so that the code get user from input and output the answer just like the sample output in the question.
# get input from user
tests = int(input())
inputs = []
for _ in range(tests):
li = int(input())
temp = []
for _ in range(li):
temp.append(input())
inputs.append(temp)
temp = []
Sample input
4
4
2 4 5
3 6 2 3
1 8
4 1 2 5 0
1
3 2 5 1
3
4 1 2 3 4
2 5 4
4 3 2 1 0
1
1 -100
0
How to make storytelling audio format