Guest User

Untitled

a guest
Oct 22nd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. (stack = "aaaaaaa
  2. bbbbbbb
  3. 123 nay!
  4. ccccccc
  5. 123 yeah!
  6. ddddddd
  7. yes yes
  8. yes no
  9. eeeeeee")
  10. (target = "yes")
  11. (previousLines = (CircularBuffer . new(2)))
  12. (stack . each { |line|
  13. (line . chomp!)
  14. (if (line . include? target)
  15. (break)
  16. end)
  17. (previousLines . enter line)})
  18. (printf "two lines before the target line is '%s'\n" , (previousLines . nth 1))
  19.  
  20.  
  21.  
  22. Here is for example a CircularBuffer that would do:
  23.  
  24. (class Node
  25. (attr_accessor :data,:previous,:next)
  26. (def initialize(data)
  27. (@data = data)
  28. (@previous = nil)
  29. (@next = nil)
  30. end)
  31. end)
  32.  
  33. (class CircularBuffer
  34. (def initialize(maxSize)
  35. (@maxSize = maxSize)
  36. (@size = 0)
  37. (@tail = nil)
  38. end)
  39. (def enter(data)
  40. (if (@size == @maxSize)
  41. (@tail . data= data)
  42. (@tail = (@tail . next))
  43. elsif (@size == 0)
  44. (@tail = (Node . new data))
  45. (@tail . previous= @tail)
  46. (@tail . next= @tail)
  47. (@size = 1)
  48. else
  49. (node = (Node . new data))
  50. (node . previous= (@tail . previous))
  51. (node . next= @tail)
  52. (@tail . previous . next = node)
  53. (@tail . previous = node)
  54. (@size = (@size + 1))
  55. end)
  56. end)
  57. (def nth(index)
  58. (index = (index . modulo @maxSize))
  59. (cur = (@tail . previous))
  60. (while (0 < index)
  61. (cur = (cur . previous))
  62. (index = (index - 1))
  63. end)
  64. (cur . data)
  65. end)
  66. end)
Add Comment
Please, Sign In to add comment