-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinal2016_cyrusAns.py
637 lines (518 loc) · 17 KB
/
final2016_cyrusAns.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
##### 2016 Finals
##### Wang Nian Yu. Cyrus
##### 1002176
##### 16F09
import better_exceptions
##### Question 3
def maxOccurrences(inp):
inpList = map(int,inp.split(' '))
occurrenceDict = {}
for i in inpList:
if occurrenceDict.has_key(i):
occurrenceDict[i] += 1
else:
occurrenceDict[i] = 1
allOccurrences = occurrenceDict.values()
maxOccurrences = max(allOccurrences)
occurrenceList = []
for i in occurrenceDict:
if occurrenceDict[i] == maxOccurrences:
occurrenceList.append(i)
return (sorted(occurrenceList),maxOccurrences)
print 'Question 3 Test Cases \n'
print 'test 1'
inp = '2 3 40 3 5 4 -3 3 3 2 0'
print maxOccurrences(inp)
print 'test 2'
inp = '9 30 3 9 3 2 4'
print maxOccurrences(inp)
print '\n'
##### Question 4
from libdw import sm
class RingCounter(sm.SM):
startState = 0
def getNextValues(self,state,inp):
threeBit = {0:'000',1:'001',2:'010',3:'011',4:'100',5:'101',6:'110',7:'111'}
if inp == 1:
return(0,'000')
else:
state += 1
if state == 8:
state = 0
return(0,'000')
else:
return(state,threeBit[state])
print 'Question 4 Test Cases \n'
print 'test 1'
r = RingCounter()
print r.transduce([0,0,0,0,0,0,0,0,0])
print 'test 2'
r = RingCounter()
print r.transduce([0,0,0,1,0,0,0,0,0])
print 'test 3'
r = RingCounter()
print r.transduce([0,0,0,1,0,0,1,0,0])
print '\n'
##### Question 5
class Avatar:
def __init__(self,name,hp = 100,position = (1,1)):
self.name = name
self.hp = hp
self.position = position
def getName(self):
return self.name
def setName(self,inp):
self.name = inp
def getHP(self):
return self.hp
def setHP(self,inp):
self.hp = inp
def getPosition(self):
return self.position
def setPosition(self,inp):
self.position = inp
def heal(self,inp = 1):
if inp > 0:
self.hp += inp
def attacked(self,inp = -1):
if inp < 0:
self.hp += inp
print 'Question 5 Test Cases \n'
print 'test 1: __init__'
a = Avatar('John')
print (a.name,a.hp,a.position)
print 'test 1: __init__'
a = Avatar('Jane',150,(10,10))
print (a.name,a.hp,a.position)
print 'test 3: getName(), setName()'
a = Avatar('John')
a.setName('Jude')
print a.getName()
print 'test 4: getPosition(), setPosition()'
a = Avatar('John')
a.setPosition((1,3))
print a.getPosition()
print 'test 5: getHP(), setHP()'
a = Avatar('John')
a.setHP(200)
print a.getHP()
print 'test 6: heal()'
a = Avatar('John')
a.heal(5)
print a.getHP()
print 'test 7: attacked()'
a = Avatar('John')
a.attacked(20)
print a.getHP()
print 'test 8: heal()'
a = Avatar('John')
a.heal()
print a.getHP()
print 'test 9: attacked()'
a = Avatar('John')
a.attacked()
print a.getHP()
print 'test 10: heal(), attacked() '
a = Avatar('John')
a.attacked(2)
a.heal(-2)
print a.getHP()
print '\n'
##### Question 6
from copy import deepcopy
class Map(object):
def __init__(self,world):
self.world = deepcopy(world)
def whatIsAt(self,coord):
if self.world.has_key(coord):
info = self.world[coord]
if info == 'x':
return 'Exit'
elif info == 0:
return 'Wall'
elif info > 0:
return 'Food'
elif info < 0:
return 'Enemy'
else:
return 'Empty'
def getEnemyAttack(self,coord):
if self.whatIsAt(coord) == 'Enemy':
return self.world[coord]
else:
return False
def getFoodEnergy(self,coord):
if self.whatIsAt(coord) == 'Food':
return self.world[coord]
else:
return False
def removeEnemy(self,coord):
if self.whatIsAt(coord) == 'Enemy':
del self.world[coord]
return True
else:
return False
def eatFood(self,coord):
if self.whatIsAt(coord) == 'Food':
del self.world[coord]
return True
else:
return False
def getExitPosition(self):
for i in self.world:
if self.world[i] == 'x':
return i
return None
print 'Question 6 Test Cases \n'
world={(0,0):0, (1,0):0 , (2,0):0, (3,0): 0, (4,0):0, (5,0): 0,
(0,1):0, (1,1): 2, (2,1):-3, (5,1): 0, (0,2):0, (5,2): 0, (0,3):0,
(5,3): 0, (0,4):0, (5,4): 0, (0,5):0, (1,5):0 , (2,5):0, (3,5): 0,
(4,5):'x', (5,5): 0}
print 'test 1: object instantiation'
m = Map(world)
print m.world
print 'test 2: whatIsAt'
print m.whatIsAt((1,0))
print 'test 3: whatIsAt'
print m.whatIsAt((2,1))
print 'test 4: whatIsAt'
print m.whatIsAt((1,1))
print 'test 5: getFoodEnergy'
w1 = m.getFoodEnergy((1,1))
w2 = m.getFoodEnergy((3,3))
print (w1,w2)
print 'test 6: getEnemyAttack'
w1 = m.getEnemyAttack((2,1))
w2 = m.getEnemyAttack((3,3))
print (w1,w2)
print 'test 7: removeEnemy'
w1 = m.getEnemyAttack((2,1))
w2 = m.removeEnemy((2,1))
w3 = m.getEnemyAttack((2,1))
print (w1,w2,w3)
print 'test 8: whatIsAt'
print m.whatIsAt((1,4))
print 'test 9: getFoodEnergy'
print m.getFoodEnergy((1,4))
print 'test 10: getEnemyAttack'
print m.getEnemyAttack((1,4))
print 'test 11: whatIsAt'
print m.whatIsAt((4,5))
print 'test 12: getExitPosition'
print m.getExitPosition()
print 'test 13: eatFood'
w1 = m.whatIsAt((1,1))
w2 = m.eatFood((1,1))
w3 = m.whatIsAt((1,1))
print (w1,w2,w3)
print 'test 14: test aliasing'
print m.world == world
print '\n'
##### Question 7
from operator import add
class DW2Game(sm.SM):
def __init__(self,Avatar,Map):
self.startState = (deepcopy(Avatar),deepcopy(Map))
def getNextValues(self,state,inp):
nextState = deepcopy(state)
if inp[1] == 'up':
newPosition = tuple(map(add,deepcopy(nextState[0].position),(0,-1)))
elif inp[1] == 'down':
newPosition = tuple(map(add,deepcopy(nextState[0].position),(0,1)))
elif inp[1] == 'left':
newPosition = tuple(map(add,deepcopy(nextState[0].position),(-1,0)))
elif inp[1] == 'right':
newPosition = tuple(map(add,deepcopy(nextState[0].position),(1,0)))
if inp[0] == 'move':
if nextState[1].whatIsAt(newPosition) in ['Empty','Food','Exit']:
nextState[0].position = newPosition
if nextState[1].whatIsAt(newPosition) == 'Food':
nextState[0].heal(nextState[1].getFoodEnergy(newPosition))
nextState[1].eatFood(newPosition)
elif nextState[1].whatIsAt(newPosition) == 'Enemy':
nextState[0].attacked(nextState[1].getEnemyAttack(newPosition))
elif inp[0] == 'attack':
if nextState[1].whatIsAt(newPosition) == 'Enemy':
nextState[1].removeEnemy(newPosition)
return (nextState[0],nextState[1]),(nextState[0].name,nextState[0].position,nextState[0].hp)
def done(self,state):
if state[1].whatIsAt(state[0].position) == 'Exit':
return True
else:
return False
print 'Question 7 Test Cases \n'
world2={(0,0):0, (1,0):0 , (2,0):0, (3,0): 0, (4,0):0, (5,0): 0, (0,1):0, (5,1): 0, (0,2):0, (1,2): -2, (5,2): 0, (0,3):0, (2,3): 3, (5,3): 0, (0,4):0, (5,4): 0, (0,5):0, (1,5):0, (2,5):0, (3,5): 0, (4,5):'x', (5,5): 0,}
print 'test 1'
inp = [('move','down'),('attack','down'),('move','down'),('move','down'),('move','down'),('move','right'),('move','right'),('move','right'),('move','down'),('move','down')]
av = Avatar('John')
m = Map(world2)
g = DW2Game(av,m)
print g.transduce(inp)
print 'test 2'
inp=[('move','left'),('move','right'),('move','right'),('move','right'),('move','right'),('move','down'),('move','down'),('move','down'),('move','up')]
av=Avatar('John')
m=Map(world2)
g=DW2Game(av,m)
print g.transduce(inp)
print 'test 3'
inp=[('move','right'),('move','right'),('move','right'),('move','down'),('move','left'),('move','left'),('move','left'),('attack','left'),('move','left')]
av=Avatar('John')
m=Map(world2)
g=DW2Game(av,m)
print g.transduce(inp)
print 'test 4'
inp=[('move','right'),('move','right'),('move','right'),('move','down'),('move','left'),('move','left'),('move','left'),('attack','left'),('move','left'),('move','left'),('move','down'),('move','right')]
av=Avatar('John')
m=Map(world2)
g=DW2Game(av,m)
print g.transduce(inp)
print 'test 5'
inp=[('move','right'),('move','right'),('move','right'),('move','down'),('move','left'),('move','left'),('move','left'),('attack','left'),('move','left'),('move','left'),('move','down'),('move','right'),('move','right'),('move','right'),('move','down'),('move','down'),('move','down')]
av=Avatar('John')
m=Map(world2)
g=DW2Game(av,m)
print g.transduce(inp)
print 'test 6'
av=Avatar('John')
m=Map(world2)
g=DW2Game(av,m)
g.start()
n,o=g.getNextValues(g.startState,('move','right'))
ans = g.state[0].getPosition() == n[0].getPosition()
print ans, g.state[0].getPosition(), n[0].getPosition()
print '\n'
##### Question 8
class Map(object):
def __init__(self,world):
self.world = deepcopy(world)
def whatIsAt(self,coord):
if self.world.has_key(coord):
info = self.world[coord]
if info == 'x':
return 'Exit'
elif info == 0:
return 'Wall'
elif info > 0:
return 'Food'
elif info < 0:
return 'Enemy'
else:
return 'Empty'
def getEnemyAttack(self,coord):
if self.whatIsAt(coord) == 'Enemy':
return self.world[coord]
else:
return False
def getFoodEnergy(self,coord):
if self.whatIsAt(coord) == 'Food':
return self.world[coord]
else:
return False
def removeEnemy(self,coord):
if self.whatIsAt(coord) == 'Enemy':
del self.world[coord]
return True
else:
return False
def eatFood(self,coord):
if self.whatIsAt(coord) == 'Food':
del self.world[coord]
return True
else:
return False
def getExitPosition(self):
for i in self.world:
if self.world[i] == 'x':
return i
return None
def getSearchMap(self):
world = deepcopy(self.world)
coordX = []
coordY = []
for i in world:
coordX.append(i[0])
coordY.append(i[1])
minX = min(coordX)
maxX = max(coordX)
minY = min(coordY)
maxY = max(coordY)
valueList = (world.values())
valueList.remove('x')
offset = max(valueList)
allCoords = []
for x in xrange(minX,maxX+1):
for y in xrange(minY,maxY+1):
allCoords.append((x,y))
finalDict = {}
for i in allCoords:
directionList = [tuple(map(add,i,(0,-1))),tuple(map(add,i,(1,0))),tuple(map(add,i,(0,1))),tuple(map(add,i,(-1,0)))]
directionCount = 0
positionDict = {}
for j in directionList:
if self.world.has_key(j) and self.world[j] not in ['x',0]:
positionDict[directionCount] = (j,offset - self.world[j])
elif self.world.has_key(j) and self.world[j] == 0:
positionDict[directionCount] = (j,1000)
else:
positionDict[directionCount] = (j,offset)
directionCount += 1
validPositionDict = deepcopy(positionDict)
for j in positionDict:
if positionDict[j][0][0] < minX or positionDict[j][0][0] > maxX or positionDict[j][0][1] < minY or positionDict[j][0][1] > maxY:
del validPositionDict[j]
finalDict[i] = validPositionDict
return finalDict
print 'Question 8 Test Cases \n'
world = {(0,0):0, (1,0):0 , (2,0):0, (0,1):0, (1,1):-2, (2,1): 0, (0,2):0, (1,2): 'x', (2,2): 0}
print 'test 1'
m = Map(world)
print m.getSearchMap()
world = {(0,0):0, (1,0):0 , (2,0):0, (0,1):0, (1,1):3, (2,1): 0, (0,2):0, (1,2): 'x', (2,2): 0}
print 'test 2'
m = Map(world)
print m.getSearchMap()
world = {(0,0):0, (1,0):0 , (2,0):0, (3,0): 0, (4,0):0, (5,0): 0, (0,1):0, (5,1): 0, (0,2):0, (1,2): -2, (5,2): 0, (0,3):0, (2,3): 3,(5,3): 0, (0,4):0, (5,4): 0, (0,5):0, (1,5):0 , (2,5):0, (3,5): 0, (4,5):'x', (5,5): 0}
print 'test 3'
m = Map(world)
print m.getSearchMap()
print '\n'
##### Question 9
"""
Procedures and classes for doing uniform cost search, always with
dynamic programming. Becomes A* if a heuristic is specified.
"""
from libdw import util
somewhatVerbose = False
"""If ``True``, prints a trace of the search"""
verbose = False
"""If ``True``, prints a verbose trace of the search"""
class SearchNode:
"""A node in a search tree"""
def __init__(self, action, state, parent, actionCost):
self.state = state
self.action = action
"""Action that moves from ``parent`` to ``state``"""
self.parent = parent
if self.parent:
self.cost = self.parent.cost + actionCost
"""The cost of the path from the root to ``self.state``"""
else:
self.cost = actionCost
def path(self):
""":returns: list of ``(action, state)`` pairs from root to this node"""
if self.parent == None:
return [(self.action, self.state)]
else:
return self.parent.path() + [(self.action, self.state)]
def inPath(self, s):
"""
:returns: ``True`` if state ``s`` is in the path from here to
the root
"""
if s == self.state:
return True
elif self.parent == None:
return False
else:
return self.parent.inPath(s)
def __repr__(self):
if self.parent == None:
return str(self.state)
else:
return repr(self.parent) + \
"-"+str(self.action)+"->"+str(self.state)
__str__ = __repr__
class PQ:
"""
Slow implementation of a priority queue that just finds the
minimum element for each extraction.
"""
def __init__(self):
"""Create a new empty priority queue."""
self.data = []
def push(self, item, cost):
"""Push an item onto the priority queue.
Assumes items are instances with an attribute ``cost``."""
self.data.append((cost, item))
def pop(self):
"""Returns and removes the least cost item.
Assumes items are instances with an attribute ``cost``."""
(index, cost) = util.argmaxIndex(self.data, lambda (c, x): -c)
return self.data.pop(index)[1] # just the data item
def isEmpty(self):
"""Returns ``True`` if the PQ is empty and ``False`` otherwise."""
return len(self.data) == 0
def __str__(self):
return 'PQ('+str(self.data)+')'
def search(initialState, goalTest, actions, successor,
heuristic = lambda s: 0, maxNodes = 10000):
"""
:param initialState: root of the search
:param goalTest: function from state to Boolean
:param actions: a list of possible actions
:param successor: function from state and action to next state and cost
:param heuristic: function from state to estimated cost to reach a goal;
defaults to a heuristic of 0, making this uniform cost search
:param maxNodes: kill the search after it expands this many nodes
:returns: path from initial state to a goal state as a list of
(action, state) tuples
"""
startNode = SearchNode(None, initialState, None, 0)
if goalTest(initialState):
return startNode.path()
agenda = PQ()
agenda.push(startNode, 0)
expanded = {}
count = 1
while (not agenda.isEmpty()) and maxNodes > count:
if verbose: print "agenda: ", agenda
n = agenda.pop()
if not expanded.has_key(n.state):
expanded[n.state] = True
if goalTest(n.state):
# We're done!
return n.path(),n.cost
if somewhatVerbose or verbose:
print " ", n.cost, ": expanding: ", n
for a in actions:
(newS, cost) = successor(n.state, a)
if not expanded.has_key(newS):
# We don't know the best path to this state yet
count += 1
newN = SearchNode(a, newS, n, cost)
agenda.push(newN, newN.cost + heuristic(newS))
print "Search failed after visiting ", count, " states."
return None
def findPath(Avatar,Map):
searchMap = Map.getSearchMap()
initialState = Avatar.position
goal = Map.getExitPosition()
actions = [0,1,2,3]
def goalTest(initialState):
return goal == initialState
def successor(state,action):
if searchMap[state].has_key(action):
return searchMap[state][action]
else:
return (None,100000000000000)
path = search(initialState,goalTest,actions,successor)
if path[1] < 1000:
return path
else:
return None
print 'Question 9 Test Cases \n'
world = {(0,0):0, (1,0):0, (2,0):0, (3,0): 0, (4,0):0,(5,0): 0, (0,1):0, (5,1): 0,(0,2):0, (1,2): -2, (5,2): 0, (0,3):0, (2,3): 3, (5,3): 0,(0,4):0, (5,4): 0,(0,5):0, (1,5):0, (2,5):0, (3,5): 0, (4,5):'x', (5,5): 0}
print 'test 1'
av = Avatar('John',position=(1,3))
m = Map(world)
print findPath(av,m)
world = {(0,0):0, (1,0):0, (2,0):0, (3,0): 0, (4,0):0,(5,0): 0, (0,1):0, (5,1): 0, (0,2):0, (1,2): -2, (5,2): 0,(0,3):0, (2,3): 3, (3,3):0, (5,3): 0, (0,4):0, (3,4):0,(5,4): 0, (0,5):0, (1,5):0, (2,5):0, (3,5): 0, (4,5):'x',(5,5): 0}
print 'test 2'
av = Avatar('John',position=(1,3))
m = Map(world)
print findPath(av,m)
world = {(0,0):0, (1,0):0 , (2,0):0, (3,0): 0, (4,0):0,(5,0): 0,(0,1):0, (3,1):0, (5,1): 0,(0,2):0, (1,2): -2,(3,2):0, (5,2): 0,(0,3):0, (2,3): 3, (3,3):0, (5,3):0,(0,4):0, (3,4):0, (5,4): 0,(0,5):0, (1,5):0 , (2,5):0,(3,5): 0, (4,5):'x', (5,5): 0}
print 'test 3'
av = Avatar('John',position=(1,3))
m = Map(world)
print findPath(av,m)