From: James Koppel Date: 2007-11-06T07:24:53+09:00 Subject: Re: [QUIZ] Editing Text (#145) (I thought I had sent in my solution yesterday, but it didn't send, since I didn't realize that Yahoo now sometimes demands a CAPTCHA.) I've been busy these past few weeks, and thus didn't get around to this quiz until yesterday (Hurrah for the extension!). Even then, I only implemented the minimum (save for the inspect/to_s method to make testing easier, which doesn't count). My solution uses a doubly-linked list of lines. Each line is guaranteed to be terminated by a newline; each newline is guaranteed to terminate a line. This means that strings that don't end in a newline will have one added. The cursor should be thought to lie between two characters rather than on a character. Because of this, insertign before and after is the same. The cursor is represented by the node containing the line that the cursor is on, and the index of the character to the right of the cursor. All insertions and deletions should be O(n), where n is the length of the line. Moving the cursor should be O(1). class ListNode attr_accessor :prev,:nxt,:val def initialize(prev=nil,nxt=nil,val="") @prev,@nxt,@val = prev,nxt,val end def to_a first = self first = first.prev while first.prev arr = [] node = first while node.nxt arr << node node = node.nxt end arr << node arr end end ###Partitions string into a linked list of lines ###For all lines, the last character is guaranteed to be a newline ###That includes the last, for simplicity's sake ### ###The cursor is between characters rather than on a character. Thus, inserting after ###is the same as inserting before class LinkedListBuffer def initialize(str="") str << "\n" unless str[-1,1] == "\n" list = str.scan(/[^\n]*?\n/).map{|s| ListNode.new(nil,nil,s)} list.each_with_index do |node, idx| node.prev = list[idx-1] unless idx == 0 node.nxt = list[idx+1] end @cur_line = list.first @line_idx = 0 end def at_end? !@cur_line.nxt and @line_idx == @cur_line.val.length-1 end def at_begin? !@cur_line.prev and @line_idx == 0 end ##The construct ""<