Determine the winner of a game of War
Python, 160 (155?) bytes
f=lambda x,y,z=1:f(*((x,y,z+2),(x[z:]+y[:z]+x[:z],y[z:]),(x[z:],y[z:]+x[:z]+y[:z]))[(x[z-1]>y[z-1])+(x[z-1]<y[z-1])*2])if len(y)>z<len(x)else len(x)>len(y)
This solution is theoretically valid, but it require the default python maximum recursion depth to be increased for some of the test cases.
The second solution is 5 bytes longer, but work for all the test cases.
f=lambda x,y,z=1:(f(x,y,z+2)if x[z-1]==y[z-1]else f(x[z:]+y[:z]+x[:z],y[z:])if x[z-1]>y[z-1]else f(x[z:],y[z:]+x[:z]+y[:z]))if len(y)>z<len(x)else len(x)>len(y)
Edit: Ungolfed solution 1:
def f(x,y,z=1):
if len(y)<z>len(x):
return len(x)>len(y)
else:
return f(*(
(x,y,z+2),
(x[z:],y[z:]+x[:z]+y[:z]),
(x[z:]+y[:z]+x[:z],y[z:])
)[(x[z-1]>y[z-1])+(x[z-1]<y[z-1])*2])
JavaScript (ES6), 134 bytes
f=([p,...r],[q,...s],t=[],u=[],v)=>!q||p&&(v|p==q?f(r,s,[...t,p],[...u,q],!v):p>q?f([...r,...u,q,...t,p],s):f(r,[...s,...t,p,...u,q]))
<div oninput=o.checked=f(p.value,q.value)>
Player 1's cards: <input id=p><br>
Player 2's cards: <input id=q><br>
<input id=o type="checkbox"> Player 2 loses
Return undefined
if Player 2 wins, true
otherwise. Accepts comparable iterators, usually arrays of integers or strings of hex characters. Answer is composed of over 22% of .
characters, which I think must be a record for me.
Python, 261 to 265 bytes
def f(a,b):
if a==""or b=="":return b==""
p=a[0];q=b[0];a=a[1:];b=b[1:]
if p>q:a+=q+p
if p<q:b+=p+q
while p[-1]==q[-1]:
if len(a)<2 or len(b)<2:return len(b)<2
v=a[1];w=b[1];p+=a[0:2];q+=b[0:2];a=a[2:];b=b[2:]
if v>w:a+=q+p
if v<w:b+=p+q
return f(a,b)
As posted, this is 265 bytes and it works in both Python 2 and Python 3. You can save 4 bytes in Python 2 by replacing the spaces with a single tab in the while loop.
Try it online