code_junkie

What is a cyclic data structure good for?

Nov 13th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. >>> L = ['grail']
  2. >>> L.append(L)
  3. >>> L
  4. ['grail', [...]]
  5.  
  6. >>> L[0]
  7. 'grail'
  8. >>> L[1][0]
  9. 'grail'
  10. >>> L[1][1][0]
  11. 'grail'
  12.  
  13. V=["Boulder", "Denver", "Colorado Springs", "Pueblo", "Limon"]
  14.  
  15. E=[["Boulder", "Denver"],
  16. ["Denver", "Colorado Springs"],
  17. ["Colorado Springs", "Pueblo"],
  18. ["Denver", "Limon"],
  19. ["Colorado Springs", "Limon"]]
  20.  
  21. public class Direction
  22. {
  23. ...
  24. public IEnumerable<Direction> WithTwoNeighbors
  25. {
  26. get {
  27. yield return this;
  28. yield return this.CounterClockwise;
  29. yield return this.Clockwise;
  30. }
  31. }
  32. }
  33. ...
  34. public void TryToMove (Direction dir)
  35. {
  36. dir = dir.WithTwoNeighbors.Where (d => CanMove (d)).First ()
  37. Move (dir);
  38. }
  39.  
  40. v[end] = a;
  41. end = (end+1) % N;
  42. if (begin == end)
  43. begin = (begin+1) % N;
  44.  
  45. for (i=begin; i != end; i = (i+1) % N) {
  46. // do stuff
  47. }
  48.  
  49. chicago[0] == denver
  50. chicago[0][0] == phoenix
  51. chicago[0][0][0] == chicago
  52.  
  53. >>> L = ['foo', 'bar']
  54. >>> L.append(L)
  55. >>> L
  56. ['foo', 'bar', [...]]
  57. >>> L[0]
  58. 'foo'
  59. >>> L[1]
  60. 'bar'
  61. >>> L[2]
  62. ['foo', 'bar', [...]]
  63. >>> L[2].append('baz')
  64. >>> L
  65. ['foo', 'bar', [...], 'baz']
  66. >>> L[2]
  67. ['foo', 'bar', [...], 'baz']
  68. >>> L[2].pop()
  69. 'baz'
  70. >>> L
  71. ['foo', 'bar', [...]]
  72. >>> L[2]
  73. ['foo', 'bar', [...]]
  74.  
  75. options,start_menu,about = [],[],[]
  76.  
  77. def do_nothing(): pass
  78.  
  79. about += [
  80. {'name':"copyright by...",'action':None,'menu':about},
  81. {'name':"back",'action':do_nothing,'menu':start_menu}
  82. ]
  83. options += [
  84. {'name':"volume up",'action':volumeUp,'menu':options},
  85. {'name':"save",'action':save,'menu':start_menu},
  86. {'name':"back without save",'action':do_nothing,'menu':start_menu}
  87. ]
  88. start_menu += [
  89. {'name':"Exit",'action':f,'menu':None}, # no next menu since we quite
  90. {'name':"Options",'action':do_nothing,'menu':options},
  91. {'name':"About",'action':do_nothing,'menu':about}
  92. ]
  93.  
  94. >>> print about
  95. [{'action': None, 'menu': [...], 'name': 'copyright by...'},#etc.
  96. # see the ellipsis (...)
  97.  
  98. def menu_item_pressed(item):
  99. log("menu item '%s' pressed" % item['name'])
  100. item['action']()
  101. set_next_menu(item['menu'])
  102.  
  103. class SelfReferenceMarkerClass: pass
  104. #singleton global marker for self reference
  105. SelfReferenceMarker = SelfReferenceMarkerClass()
  106. about += [
  107. {'name':"copyright by...",'action':None,'menu':srm},
  108. {'name':"back",'action':do_nothing,'menu':start_menu}
  109. ]
  110.  
  111. def menu_item_pressed(item):
  112. item['action']()
  113. if (item['menu'] == SelfReferenceMarker):
  114. set_next_menu(get_previous_menu())
  115. else:
  116. set_next_menu(item['menu'])
  117.  
  118. class Client(object):
  119. def set_remote(self, remote_client):
  120. self.remote_client = remote_client
  121.  
  122. def send(self, msg):
  123. self.remote_client.receive(msg)
  124.  
  125. def receive(self, msg):
  126. print msg
  127.  
  128. Jill = Client()
  129. Bob = Client()
  130. Bob.set_remote(Jill)
  131. Jill.set_remote(Bob)
  132.  
  133. Bob.send("Hi, Jill!")
  134.  
  135. Jill.send("Hi, Bob!")
Add Comment
Please, Sign In to add comment