forked from gleventhal/SysAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordWithEnemies.py
38 lines (33 loc) · 1.04 KB
/
WordWithEnemies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/python
# Words With Enemies
# Reddit /r/dailyprogrammer challenges
import sys, re
if len(sys.argv) < 3:
print "Usage:\t%s <word one> <word two>" % (sys.argv[0])
sys.exit(1)
else:
left = sys.argv[1]
right = sys.argv[2]
def CompareWords(left, right):
'''Test the two words, removing any duplicate letters'''
lleft=list(left)
lright=list(right)
dupes=[]
right_matched_indexes=[]
print "Comparing %s with %s" % (left, right)
for li,l in enumerate(lleft):
for ri,r in enumerate(lright):
if l == r and ri not in right_matched_indexes:
dupes.append(l)
right_matched_indexes.append(ri)
break
for d in dupes:
left=re.sub(d, '', left, 1)
right=re.sub(d, '', right, 1)
if len(left) > len(right):
return 'Left wins with %s' % (left)
elif len(right) > len(left):
return 'Right wins with %s' % (right)
else:
return 'Tie!!'
print CompareWords(left, right)