Turn Python trace Walk DEMO

Source: Internet
Author: User
Tags gety

46965125

#quote from ' Introduction to computation and programming #using Python, revised, MIT Press ' import Randomimport Pylab def s        Tddev (x): mean = SUM (x)/len (x) tot = 0.0 for X in X:tot + = (X-mean) **2 return (Tot/len (x)) **0.5        def CV (x): mean = SUM (x)/len (x) return StdDev (x)/mean class Location (object): Def __init__ (self, x, y):  "" "X and Y is floats" "" self.x = x self.y = y def move (self, deltax, DeltaY): "" "DeltaX And DeltaY is floats "" "Return location (self.x + deltax, Self.y + deltay) def GetX (self): retur        n self.x def GetY (self): return self.y def distfrom (self, other): Ox = other.x Oy = other.y Xdist = Self.x-ox ydist = Self.y-oy return (xdist**2 + ydist**2) **0.5 def __s Tr__ (self): return ' < ' + str (self.x) + ', ' + str (SELF.Y) + ' > ' class Field (object): Def __init __ (self): self.drUnks = {} def adddrunk (self, Drunk, loc): If Drunk in self.drunks:raise valueerror (' Duplicat E drunk ') else:self.drunks[drunk] = loc def movedrunk (self, drunk): if drunk not In Self.drunks:raise valueerror (' Drunk not in field ') xdist, ydist = Drunk.takestep () currentloc ation = Self.drunks[drunk] #use Move method of location to get new location self.drunks[drunk] = Currentloca Tion.move (Xdist, ydist) def getloc (self, drunk): If drunk not in Self.drunks:raise Valueerro R (' Drunk Not on field ') return Self.drunks[drunk] class Drunk (object): Def __init__ (self, name = None            ): "" "Assumes name is a str" "" Self.name = name def __str__ (self): if self! = None: Return self.name return ' Anonymous ' class Usualdrunk (Drunk): def takestep (self): stepchoices = [(0.0, 1.0), (0.0,-1.0), (1.0, 0.0), ( -1.0, 0.0)] return Random.choice (Stepchoices) class Colddrunk (Drunk): def takestep (self): Stepchoices = [(0.0, 1.0), (0.0, -2.0), (1.0, 0.0), ( -1.0, 0.0)] return Random.choice (stepchoices) class E Wdrunk (Drunk): def takestep (self): stepchoices = [(1.0, 0.0), ( -1.0, 0.0)] return Random.choice (stepchoic            ES) class Styleiterator (object): Def __init__ (self, styles): Self.index = 0 Self.styles = Styles            def nextstyle (self): result = Self.styles[self.index] if Self.index = = Len (self.styles)-1:    Self.index = 0 Else:self.index + = 1 return result def tracewalk (Drunkkinds, numsteps): Stylechoices = Styleiterator (' B + ', ' r^ ', ' Mo ')) F = Field () for dClass in drunkkinds:d = DClass () F. Adddrunk (d, location (0, 0)) locs = [] for s in range (numsteps): F.movedrunk (d) Locs.ap       Pend (F.getloc (d)) Xvals = [] Yvals = [] for L in Locs:xVals.append (L.getx ()) Yvals.append (L.gety ()) CurStyle = Stylechoices.nextstyle () pylab.plot (xvals, Yvals, curstyle, label = Dclass.__name  __) Pylab.title (' spots visited on Walk (' + str (numsteps) + ' steps) ') Pylab.xlabel (' Steps east/west of origin ') Pylab.ylabel (' Steps North/south of origin ') pylab.legend (loc = ' best ') tracewalk ((Usualdrunk, Colddrunk , Ewdrunk), Pylab.show ()

  

Turn Python trace Walk DEMO

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.