Guest User

Untitled

a guest
Jun 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. string = "I like parentheticals (a lot). Sometimes(when I nest them(my parentheticals) too much (like
  2. this(and this))), it get confusing."
  3.  
  4. def parentheticals_open_close_position(str)
  5. starting_parentheticals = []
  6. ending_parenthetical = []
  7. str.each_char.with_index do |char, index|
  8. if char == '('
  9. starting_parentheticals << index
  10. end
  11. if char == ')'
  12. ending_parenthetical << index
  13. end
  14. end
  15. p starting_parentheticals
  16. p ending_parenthetical
  17. hash = Hash[starting_parentheticals.zip(ending_parenthetical)]
  18. p hash
  19. end
  20. # A: nA, nB, nC
  21. # B: nA, nB, nC
  22. parentheticals_open_close_position(string)
  23.  
  24. # How I would solve by hand...
  25. # I think that I would just strike off the starting and ending at the same time since that would help me easily keep the order between the two. This is assuming the string had a lot of "tricky" to track parentheticals. If it were simple, I think I would just find the first opening one and then immediately find the closing parenthetical.
  26.  
  27.  
  28. # Time complexity
  29. # I think the time complexity is O(n) since it should take the same amount of time to given a similar string. This is an area of programming I would like to learn more about. I am not a CS grad, so I didn't get educated in Big-O notation, but I under it's important for performance and it's on my list of things to improve on.
  30.  
  31. # I think the space complexity is O(n log n), but again, I'm no expert in this area...
Add Comment
Please, Sign In to add comment