Module: Algorithmable::Cups::Primitives
- Defined in:
- lib/algorithmable/cups/primitives.rb
Instance Method Summary collapse
- #chars_is_uniq?(string) ⇒ Boolean
- #find_cycled_node(root) ⇒ Object
- #remove_duplicated_chars(chars = []) ⇒ Object
-
#replace_space_chars(string, expression = '%20') ⇒ Object
this module contains a lot of C style code.
- #reverse_string(string) ⇒ Object
Instance Method Details
#chars_is_uniq?(string) ⇒ Boolean
33 34 35 36 37 38 39 40 |
# File 'lib/algorithmable/cups/primitives.rb', line 33 def chars_is_uniq?(string) return unless string map = Array.new 127, false string.chars.each do |char| return false if map[char.ord] map[char.ord] = true end end |
#find_cycled_node(root) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/algorithmable/cups/primitives.rb', line 73 def find_cycled_node(root) return unless root.next or root.next.next slow = root fast = root until fast.next.nil? slow = slow.next fast = fast.next.next break if slow == fast end # return, no cycle return if fast.next.nil? slow = root until slow == fast slow = slow.next fast = fast.next end # at this point return value is a node which is tail pointing to. fast end |
#remove_duplicated_chars(chars = []) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/algorithmable/cups/primitives.rb', line 50 def remove_duplicated_chars(chars = []) return if chars.empty? || chars.size < 2 array_tail = 1 chars.size.times do |cur_char_index| next unless cur_char_index.nonzero? prev_char_index = 0 while prev_char_index < array_tail break if chars[cur_char_index] == chars[prev_char_index] prev_char_index += 1 end if prev_char_index == array_tail chars[array_tail] = chars[cur_char_index] array_tail += 1 end end # trim all after new tail chars[0...array_tail] end |
#replace_space_chars(string, expression = '%20') ⇒ Object
this module contains a lot of C style code. It is done intentionally. All the methods avoiding using already existent algorithms in Ruby. Algorithms/solutions should be implemented in place
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/algorithmable/cups/primitives.rb', line 8 def replace_space_chars(string, expression = '%20') return unless string space_count = 0 length = string.length replacement = expression.chars.reverse.freeze length.times { |i| space_count += 1 if string[i] == ' ' } new_length = length + space_count * 2 chars = Array.new(new_length, ?\x00) (0..length.pred).to_a.reverse_each do |i| if string[i] == ' ' replacement.each_with_index do |char, index| j = index + 1 chars[new_length - j] = char end new_length = new_length - replacement.length else new_length -= 1 chars[new_length] = string[i] end end chars.join end |
#reverse_string(string) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/algorithmable/cups/primitives.rb', line 42 def reverse_string(string) return unless string chars = string.chars reversed = '' string.size.times { reversed << chars.pop } reversed end |